【问题标题】:set channel for LoggingHandler为 LoggingHandler 设置通道
【发布时间】:2016-05-24 15:42:04
【问题描述】:

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

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

配置上下文:

ctx = new ClassPathXmlApplicationContext("config/spring-module.xml");

获取input DirectChannlsend(Message)

input = ctx.getBean("input", DirectChannel.class);

input.send(msg);

但是对于DirectChannel,我必须提供一个subscriber,所以我决定写一个LoggingHandler Beanouput DirectChannel,就是这样:

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

但我的问题是:我需要loggingHandler 成为output 频道的订阅者。所以我必须以某种方式使用output 对其进行配置。我有一个类似的解决方案:

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

但是仍然有一个Exception 用于:

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

所以,我的问题是:

我是否需要使用xmlannotation 配置模块,我不能同时使用它们?

如果可以的话,有没有不使用@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();
    }
}

【问题讨论】:

    标签: spring-integration spring-xd


    【解决方案1】:

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

    我什至认为你使用@ServiceActivator 配置是正确的。

    只有您在@EnableIntegration@Configuration 旁边缺少的问题。

    另一方面,我最好将@BridgeFrom("output") 上的QueueChannel @Bean 设置为assert 处理器结果测试结束。 但无论如何你都需要@EnableIntegration

    更新

    好的。感谢您提供更新的代码。现在我明白了。您的TestAll @Configuration 类未注册到ApplicationContext 的问题。因此没有人会处理@EnableIntegration 并注册你的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();
            }
        }
    
    }
    

    【讨论】:

    • 感谢 Artern,我想我会试试 @BridgeForm。但实际上,我的测试类中同时注释了@EnableIntegration@Configuration。我也在考虑在运行时添加bean。无论如何,谢谢你的工作。我用我的代码更新了这个问题,希望它能帮助你找出我的代码有什么问题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2021-11-05
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多