2

I am new to Spring framework and Spring Integration. I'm starting using spring boot with AWS service. I tried to use Channel adaptor and Service activator to get the message from SQS queue and send to another service inside the application periodically using poller.

@configuration
public class AppConfig 
{

  @Bean
  @InboundChannelAdapter( value = "inputChannel", poller = @Poller(fixedDelay = "1000"))
  public Message inboundAdaptor ()
  {
   //get message from SQS queue
   System.out.println("get message");
   return message;
  }

  @Bean
  @ServiceActivator(inputChannel = "inputChannel")
  public String msgActivator( Message message)
  {
   //call another service and pass message body to that service
   System.out.println("This is message body" + messageBody);
   return messageBody;
  }

I expected that by doing above, the actions inside InboundChannelAdaptor will be called periodically due to poller and pass the message information to my service using ServiceActivator automatically as long as I have message in the SQS queue.

I tested them with System.out.println( ) to show what I've got. However, System.out.println( ) printed only once for each method. Does that mean poller just polled only once and stop or I cannot test the periodical call with System.out.println( )?

Any suggestion on the right way to implement this workflow is appreciated.

bumble_bee_tuna
  • 3,533
  • 7
  • 43
  • 83
Pat
  • 23
  • 3

1 Answers1

1

When using @InboundChannelAdapter on a @Bean, the bean must be of type MessageSource. Similarly, for @ServiceActivator on a bean, it must be a MessageHandler.

For POJO methods like yours, the annotated methods must be methods in a @Bean...

@Bean
public MyBean myBean() {
    return new MyBean();
}

@MessageEndpoint
public static class MyBean {

    @InboundChannelAdapter( value = "inputChannel", poller = @Poller(fixedDelay = "1000"))
    public Message inboundAdaptor () {
        //get message from SQS queue
        System.out.println("get message");
        return message;
    }

    @ServiceActivator(inputChannel = "inputChannel", outputChannel="out")
    public String msgActivator( Message message) {
        //call another service and pass message body to that service
        System.out.println("This is message body" + messageBody);
        return messageBody;
    }

}

You also need an output channel when returning a reply from the service.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179