Spring integration 设置LoggingHandler的通道

Spring integration 设置LoggingHandler的通道,spring-integration,spring-xd,Spring Integration,Spring Xd,我正在为我的springxd模块编写自己的单元测试(但是,我不打算使用xd.test和xd.dirt) 这是一个处理器模块,我为测试所做的是: 配置上下文: ctx = new ClassPathXmlApplicationContext("config/spring-module.xml"); 获取输入直接通道和发送(消息) 但是对于DirectChannel,我必须为它提供一个subscriber,因此我决定向输出DirectChannel编写一个日志处理程序Bean,就是这样: @Bea

我正在为我的springxd模块编写自己的单元测试(但是,我不打算使用
xd.test
xd.dirt

这是一个处理器模块,我为测试所做的是:

配置上下文:

ctx = new ClassPathXmlApplicationContext("config/spring-module.xml");
获取
输入
直接通道
发送(消息)

但是对于
DirectChannel
,我必须为它提供一个
subscriber
,因此我决定向
输出
DirectChannel
编写一个
日志处理程序
Bean
,就是这样:

@Bean
public LoggingHandler outputLogging(){
    LoggingHandler lh = new LoggingHandler("INFO");
    lh.setLoggerName("output-logging");
    lh.setShouldLogFullMessage(true);
    return lh;
}
但我的问题是:我需要
loggingHandler
作为
输出
频道的订户。因此,我必须以某种方式将其配置为
输出
。我得到的解决方案如下:

@Bean
@ServiceActivator(inputChannel = "output")
public LoggingHandler outputLogging(){
    LoggingHandler lh = new LoggingHandler("INFO");
    lh.setLoggerName("output-logging");
    lh.setShouldLogFullMessage(true);
    return lh;
}
但是,仍然存在以下方面的
例外情况

org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'org.springframework.context.support.ClassPathXmlApplicationContext@76b0bfab.output'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:81)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:231)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:154)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:102)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:105)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
at com.example.test.TestAll.allTest(TestAll.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:153)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
... 42 more
所以,我的问题是:

我是否需要使用
xml
annotation
来配置模块,我不能同时使用它们

如果可以,是否有不使用
@ServiceActivator(inputChannel=“output”)
的解决方案,它看起来像:

<int:service-activator input-channel="input" method="trans" output-channel="output">
    <bean class="com.example.LoggingHandler" init-method="init" destroy-method="destroy">
...
    </bean>
</int:service-activator>

...
实际上我不需要服务激活器

谢谢

[更新]

@Configuration
@EnableIntegration
public class TestAll {

    ConfigurableApplicationContext ctx;
    DirectChannel input;
    DirectChannel output;
    Logger logger = Logger.getLogger(TestAll.class);
    //you have to configure this before you run, point this to the file path in the main/resources
    String folderPath = "C:\\path\\to\\samples";

    @Before
    public void setUp(){
        ctx = new ClassPathXmlApplicationContext("config/spring-module.xml");
        input = ctx.getBean("input", DirectChannel.class);
        output = ctx.getBean("output", DirectChannel.class);
        logger.setLevel(Level.ALL);
        //ctx.addBeanFactoryPostProcessor(beanFactoryPostProcessor);
    }

    @Test
    public void allTest(){
        Message<?> msg = composeMsg();
        input.send(msg);
        logger.debug(output.isLoggingEnabled());
    }

    @Bean
    @ServiceActivator(inputChannel = "output")
    public LoggingHandler outputLogging(){
        LoggingHandler lh = new LoggingHandler("INFO");
        lh.setLoggerName("output-logging");
        lh.setShouldLogFullMessage(true);
        return lh;
    }

    public Message<?> composeMsg(){
   ...
        return MessageBuilder.withPayload(msgPayload).copyHeaders(msgHeader).build();
    }

    @After
    public void tearDown(){
        ctx.close();
    }
}
@配置
@使能集成
公共类测试{
可配置应用程序上下文ctx;
直接通道输入;
直接通道输出;
Logger=Logger.getLogger(TestAll.class);
//您必须在运行之前进行配置,将其指向main/resources中的文件路径
String folderPath=“C:\\path\\to\\samples”;
@以前
公共作废设置(){
ctx=newclasspathXMLApplicationContext(“config/spring module.xml”);
input=ctx.getBean(“input”,DirectChannel.class);
output=ctx.getBean(“output”,DirectChannel.class);
logger.setLevel(Level.ALL);
//ctx.addBeanFactoryPostProcessor(beanFactoryPostProcessor);
}
@试验
公共测试(){
消息msg=composeMsg();
输入。发送(msg);
debug(output.isLoggingEnabled());
}
@豆子
@ServiceActivator(inputChannel=“输出”)
公共日志处理程序outputLogging(){
LoggingHandler lh=新LoggingHandler(“信息”);
lh.setLoggerName(“输出日志”);
lh.setShouldLogFullMessage(真);
返回左侧;
}
公共消息composeMsg(){
...
返回MessageBuilder.withPayload(msgPayload).copyHeaders(msgHeader.build();
}
@之后
公共无效拆卸(){
ctx.close();
}
}

由于它是测试用例,我认为您只需导入真正的模块配置并提供一些测试工具,就可以让它工作并完成任务

我甚至认为您使用的是
@ServiceActivator
配置

只有在
@启用集成
以及
@配置
中缺少的问题

另一方面,我最好在
队列通道
@Bean
上设置
@BridgeFrom(“输出”)
,以
断言测试结束时的处理器结果。
但无论如何,您都需要
@EnableIntegration

更新

嗯。谢谢你的更新代码。现在我明白了。您的
TestAll
@Configuration
类未向
ApplicationContext
注册的问题。因此,没有人会处理
@enableeintegration
并注册您的
LoggingHandler
@Bean
等等

试试这个:

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

    @Autowired
    @Qualifier("input")
    private MessageChannel inputChannel;

    @Autowired
    private PollableChannel resultChannel;

    @Test
    public void allTest(){
        Message<?> msg = composeMsg();
        input.send(msg);
        Message<?> result = resultChannel.receive(10000);
        assertNotNull(result);
        // Other asserts
    }

    @Configuration
    @EnableIntegration
    @ImportResource("classpath:config/spring-module.xml")
    public static class ContextConfiguration {

        @BridgeFrom("output")
        public PollableChannel resultChannel() {
             return new QueueChannel();
        }
    }

}
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@肮脏的环境
公共类测试{
@自动连线
@限定符(“输入”)
专用消息通道输入通道;
@自动连线
私有可轮询通道结果通道;
@试验
公共测试(){
消息msg=composeMsg();
输入。发送(msg);
消息结果=结果通道接收(10000);
assertNotNull(结果);
//其他主张
}
@配置
@使能集成
@ImportResource(“classpath:config/spring module.xml”)
公共静态类上下文配置{
@BridgeFrom(“输出”)
公共可轮询频道结果频道(){
返回新的队列通道();
}
}
}

谢谢阿尔特恩,我想我会试试
@BridgeForm
。但实际上,我的测试类中同时添加了
@enableeintegration
@Configuration
。我还考虑在运行时添加
bean
。无论如何,谢谢你的工作。我用我的代码更新了这个问题,希望它能帮助你发现我的代码有什么问题。
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class TestAll {

    @Autowired
    @Qualifier("input")
    private MessageChannel inputChannel;

    @Autowired
    private PollableChannel resultChannel;

    @Test
    public void allTest(){
        Message<?> msg = composeMsg();
        input.send(msg);
        Message<?> result = resultChannel.receive(10000);
        assertNotNull(result);
        // Other asserts
    }

    @Configuration
    @EnableIntegration
    @ImportResource("classpath:config/spring-module.xml")
    public static class ContextConfiguration {

        @BridgeFrom("output")
        public PollableChannel resultChannel() {
             return new QueueChannel();
        }
    }

}