Spring Integration轮询HTTP / 1.1 Web服务text / xml

时间:2015-10-15 22:52:26

标签: spring-integration

我正在尝试使用spring xd使用HTTP / 1.1协议从Web服务中提取text / xml响应。目标是将xml响应转换为json并插入mongodb。这是我的spring-module.xml

<beans>
        <int-http:outbound-gateway id='HTTPGateway'
              request-channel='input'
              url-expression="'${protocol}://${host}'+'/${context}'"
              http-method='GET'
              expected-response-type='${expectedresponsetype}'
              charset='UTF-8'
              reply-timeout='${replytimeout}'
              reply-channel='output'>    
         </int-http:outbound-gateway>

         <int:channel id="input"/>

        <int:transformer input-channel="to.json" output-channel="output">
            <bean class="com.modules.JsonMapper"/>
        </int:transformer>

        <int:channel id="output"/>


    </beans>

和我的context.xml

<beans>    
    <context:property-placeholder properties-ref="props"/>
    <util:properties id="props">
        <prop key="expectedresponsetype">text/xml</prop>
        <prop key="replytimeout">5000</prop>
        <prop key="host">www.ctabustracker.com</prop>
        <prop key="protocol">http</prop>
        <prop key="context">bustime/api/v2/getvehicles?key=key%26vid=1</prop>   
    </util:properties>

    <import resource="classpath:config/spring-module.xml"/>

    <!-- Override direct channel with a queue channel so the test will block until a message is received -->
    <int:channel id="output">
        <int:queue/>
    </int:channel>
</beans>

和我的测试类

import org.springframework.messaging.Message;
import org.springframework.messaging.PollableChannel;
//import org.springframework.integration.channel.PollableChannel;
//import org.springframework.integration.core.Message;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UrlStreamTest {

    @Autowired
    PollableChannel output;

    @Autowired
    ConfigurableApplicationContext applicationContext;

    Logger log = Logger.getLogger(UrlStreamTest.class);

    @Test
    public void test() {
        applicationContext.start();
        System.out.println("About to receive message");
        Message message = output.receive(10000);
        System.out.println("Received message: " + message);
        assertNotNull(message);
        //log.debug(message.getPayload());
        assertTrue(message.getPayload() instanceof String);
    }
}

当我运行测试时,我收到的消息为空但我没有收到任何例外。我已经配置了log4j,下面是输出的片段

[2015-10-15 16:09:16.007] log4j - ???? DEBUG [main] --- InjectionMetadata: Processing injected element of bean 'com.bosch.test.UrlStreamTest': AutowiredFieldElement for org.springframework.messaging.PollableChannel com.bosch.test.UrlStreamTest.output
[2015-10-15 16:09:16.009] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'output'
[2015-10-15 16:09:16.009] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'nullChannel'
[2015-10-15 16:09:16.010] log4j - ???? DEBUG [main] --- AutowiredAnnotationBeanPostProcessor: Autowiring by type from bean name 'com.bosch.test.UrlStreamTest' to bean named 'output'
[2015-10-15 16:09:16.011] log4j - ???? DEBUG [main] --- InjectionMetadata: Processing injected element of bean 'com.bosch.test.UrlStreamTest': AutowiredFieldElement for org.springframework.context.ConfigurableApplicationContext com.bosch.test.UrlStreamTest.applicationContext
[2015-10-15 16:09:16.011] log4j - ???? DEBUG [main] --- AutowiredAnnotationBeanPostProcessor: Autowiring by type from bean name 'com.bosch.test.UrlStreamTest' to bean named 'org.springframework.context.support.GenericApplicationContext@4641439f'
[2015-10-15 16:09:16.013] log4j - ???? DEBUG [main] --- ProfileValueUtils: Retrieved @ProfileValueSourceConfiguration [null] for test class [class com.bosch.test.UrlStreamTest].
[2015-10-15 16:09:16.014] log4j - ???? DEBUG [main] --- ProfileValueUtils: Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [class com.bosch.test.UrlStreamTest].
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'HTTPGateway'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.ConsumerEndpointFactoryBean#0'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean '_org.springframework.integration.errorLogger'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'lifecycleProcessor'
[2015-10-15 16:09:16.020] log4j - ????  INFO [main] --- DefaultLifecycleProcessor: Starting beans in phase 0
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultLifecycleProcessor: Starting bean 'integrationHeaderChannelRegistry' of type [class org.springframework.integration.channel.DefaultHeaderChannelRegistry]
[2015-10-15 16:09:16.023] log4j - ???? DEBUG [main] --- DefaultLifecycleProcessor: Successfully started bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:16.023] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.IdGeneratorConfigurer#0'
About to receive message
Received message: null
[2015-10-15 16:09:26.045] log4j - ???? DEBUG [main] --- SpringMethodRoadie: Test method [public void com.bosch.test.UrlStreamTest.test()] threw exception [java.lang.AssertionError: ].
[2015-10-15 16:09:26.047] log4j - ???? DEBUG [main] --- DirtiesContextTestExecutionListener: After test method: context [[TestContext@450faed3 testClass = UrlStreamTest, locations = array<String>['classpath:/com/bosch/test/UrlStreamTest-context.xml'], testInstance = com.bosch.test.UrlStreamTest@89ec211, testMethod = test@UrlStreamTest, testException = java.lang.AssertionError: ]], dirtiesContext [false].
[2015-10-15 16:09:26.053] log4j - ????  INFO [Thread-0] --- GenericApplicationContext: Closing org.springframework.context.support.GenericApplicationContext@4641439f: startup date [Thu Oct 15 16:09:15 MDT 2015]; root of context hierarchy
[2015-10-15 16:09:26.054] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.IdGeneratorConfigurer#0'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'HTTPGateway'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.ConsumerEndpointFactoryBean#0'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean '_org.springframework.integration.errorLogger'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'lifecycleProcessor'
[2015-10-15 16:09:26.057] log4j - ????  INFO [Thread-0] --- DefaultLifecycleProcessor: Stopping beans in phase 0
[2015-10-15 16:09:26.058] log4j - ???? DEBUG [Thread-0] --- DefaultLifecycleProcessor: Stopping bean 'integrationHeaderChannelRegistry' of type [class org.springframework.integration.channel.DefaultHeaderChannelRegistry]
[2015-10-15 16:09:26.058] log4j - ???? DEBUG [Thread-0] --- DefaultLifecycleProcessor: Successfully stopped bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:26.058] log4j - ???? DEBUG [Thread-0] --- DefaultLifecycleProcessor: Asking bean 'HTTPGateway' of type [class org.springframework.integration.config.ConsumerEndpointFactoryBean] to stop

我无法找到有关消息为空的原因的任何线索,如果没有json转换器,它也为null。我可以在浏览器中点击该网址并获得回复。我假设一旦我收到消息,我就必须创建一个入站通道适配器,就像在这篇文章中一样 Spring Integration: Poll HTTP 但是到目前为止我还没有收到任何回复。

为org.springframework.integration启用TRACE日志记录后,我在sysout语句之间看到了这个

About to receive message
[2015-10-16 09:57:54.748] log4j - ???? TRACE [main] --- QueueChannel: preReceive on channel 'output'
[2015-10-16 09:58:04.750] log4j - ???? TRACE [main] --- QueueChannel: postReceive on channel 'output', message is null
Received message: null

1 个答案:

答案 0 :(得分:0)

您将请求发送到网址的哪个位置?它不在您的测试中。您的所有测试都是启动上下文并等待接收()消息。正如我所说,当消息被发送(直接输入或通过http适配器)时,您将看到记录的消息(预先发送在频道&#39;输入&#39;等)。您的信息流有一段时间&#39;来源但你的测试没有发送任何数据。

http网关是请求/响应 - 您必须通过向输入通道发送消息来向其发送请求。 @Autowire输入和

input.send(new GenericMessage<String>(""));