Skip to content
Advertisement

Starting and Stopping an InboundChannelAdapter manually

I’m trying to start an InboundChannelAdapter manually using a @Scheduled function. I think I’m setting up the message payload wrong but I’m not sure what to have it as. Here’s the code:

@EnableScheduling
@EnableBinding(Source.class)
public class KafkaSource {

    @Autowired
    @Qualifier(Source.OUTPUT)
    private MessageChannel controlBusChannel;

    @Scheduled(cron="0 27 0 * * *", zone="US/Eastern")
    public void run() {
        System.out.println("REACHED");
        Message controlMessage = MessageBuilder.withPayload("@'source.input.inboundChannelAdapter'.start()").build();
        controlBusChannel.send(controlMessage);
    }

    @InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1"), autoStartup = "false") 
    public AgentCreated consumeAndSendMessage() {
         // UNIMPORTANT CODE
    }
}

I’m getting this error:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method transform(java.lang.String) cannot be found on type com.company.transformer.SpecialNotesTransformer$$EnhancerBySpringCGLIB$$c10d699d

The error occurs at the controlBusChannel.send() line and the transformer in the error is the first one that runs after the source. How do I frame the message payload properly? The channel is created automatically by the Source.class in Spring Cloud Stream library.

Advertisement

Answer

You are using an outdated API.

The annotation-based configuration model has been long deprecated in favor of functional programming model, so EnableBinding, StreamListener etc are on their way out.

For you case you can simply use Supplier with StreamBridge. See this section for more details. And then you can do programmatic start/stop binding using the available lifecycle features of spring-cloud-stream.

Now, that doesn’t mean your other problem will be resolved, but without a full stack trace and sufficient detail (e.g., version of frameworks you are using) it is difficult to say.

Advertisement