通道适配器和服务激活器

Channel adaptor and Service activator

我是 Spring 框架和 Spring 集成的新手。我开始使用 spring boot 和 AWS 服务。我尝试使用通道适配器和服务激活器从 SQS 队列获取消息并使用轮询器定期发送到应用程序内的另一个服务。

@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;
  }

我预计通过上面的操作,InboundChannelAdaptor 中的操作将由于轮询器而定期调用,并使用 ServiceActivator 自动将消息信息传递到我的服务,只要我在SQS queue

我用 System.out.println( ) 测试了它们,以展示我所拥有的。但是,System.out.println( ) 每种方法只打印一次。这是否意味着轮询器仅轮询一次并停止,或者我无法使用 System.out.println( ) 测试定期调用?

如有任何关于正确实施此工作流程的建议,我们将不胜感激。

@Bean 上使用 @InboundChannelAdapter 时,bean 必须是 MessageSource 类型。同样,对于 bean 上的 @ServiceActivator,它必须是 MessageHandler.

对于像您这样的 POJO 方法,带注释的方法必须是 @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;
    }

}

从服务返回回复时,您还需要一个输出通道。