如何在java dsl中点击消息通道?

时间:2015-07-10 18:52:38

标签: spring-integration

我在Spring集成中尝试Java dsl。我正试图窃听一个频道。但是得到错误,

@ContextConfiguration
@EnableIntegration
@IntegrationComponentScan
public class DemoApplication {

  public static void main(String[] args) {

    ApplicationContext ctx = new AnnotationConfigApplicationContext(DemoApplication.class);
    CustomGtwy service = ctx.getBean(CustomGtwy.class);
    service.pushMessage("Manoj");

  }

  @Bean
  public MessageChannel loggerChannel(){
      return MessageChannels.direct().get();
  }

  @Bean
  public MessageChannel pushAssetIdChannel() {
       return MessageChannels.direct()
                  .interceptor(new WireTap(loggerChannel()))
                  .get();
  }

  @Bean
  public IntegrationFlow pushAssetIdFlow() {
      return IntegrationFlows.from("pushAssetIdChannel")
          .handle(new GenericHandler() {
            @Override
            public String handle(Object arg0, Map arg1) {
              // TODO Auto-generated method stub
              return "Success";
            }})
          .get();
  }

  @MessagingGateway
  public interface CustomGtwy{
      @Gateway(requestChannel="pushAssetIdChannel")
    String pushMessage(String s);
  }

  @Bean
  public IntegrationFlow logger(){
      return IntegrationFlows.from("loggerChannel").handle(new GenericHandler() {
        @Override
        public String handle(Object arg0, Map arg1) {
          // TODO Auto-generated method stub
          return "Success";
        }}).channel("nullChannel").get();
  }
}

在上面的代码中,如果我尝试将消息放入pushAssetIdChannel,我得到Dispatcher has no subscribers for channel 'unknown.channel.name'

如果拦截器不在那里,它就可以了。

1 个答案:

答案 0 :(得分:1)

不确定您的案例会发生什么,但对我来说最新的1.0.2版本有效:

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class SO31348246Tests {

    @Autowired
    private MessageChannel pushAssetIdChannel;

    @Test
    public void testIt() {
        this.pushAssetIdChannel.send(new GenericMessage<>("foo"));
    }

    @Configuration
    @EnableIntegration
    public static class ContextConfiguration {

        @Bean
        public MessageChannel loggerChannel() {
            return MessageChannels.direct().get();
        }

        @Bean
        public MessageChannel pushAssetIdChannel() {
            return MessageChannels.direct()
                    .interceptor(new WireTap(loggerChannel()))
                    .get();
        }

        @Bean
        public IntegrationFlow pushAssetIdFlow() {
            return IntegrationFlows.from("pushAssetIdChannel")
                    .handle(System.out::println)
                    .get();
        }

        @Bean
        public IntegrationFlow logger() {
            return IntegrationFlows.from("loggerChannel")
                    .handle((p, h) -> {
                        System.out.println(p);
                        return p;
                    })
                    .channel("nullChannel")
                    .get();
        }

    }

}

我在日志中看到SOUT

<强>更新

@ContextConfiguration修改为正常@Configuration注释后,您的课程适合我:

如果没有最后一个,框架会将您的DemoApplication视为精简配置,只是因为您有@Bean个方法,但它不像那样做完整一,并且不代理它以允许bean method reference使用loggerChannel()构建器中的WireTap。 因此,使用lite,我们jsut调用该方法并获取一个新的MessageChannel对象,但它不是应用程序上下文中的bean。这就是你最终获得Dispatcher has no subscribers的原因。