2015-06-29 33 views
1

我有點困惑,我需要一些澄清:Spring集成:在JSON對象發送給JMS

我想在JSON來變換對象,並使用JMS每秒發送。

這裏是我的context.xml:

<bean id="connectionFactory" 
    class="org.springframework.jms.connection.CachingConnectionFactory"> 
    <property name="targetConnectionFactory"> 
     <bean class="org.apache.activemq.ActiveMQConnectionFactory"> 
      <property name="brokerURL" value="tcp://localhost:61616" /> 
     </bean> 
    </property> 
    <property name="sessionCacheSize" value="10" /> 
</bean> 
<bean id="requestTopic" class="org.apache.activemq.command.ActiveMQTopic"> 
    <constructor-arg value="testconf" /> 
</bean> 

<bean id="confbean" class="demo.DeviceConfiguration"> 
    <property name="id" value="THERMO_001" /> 
    <property name="name" value="thermometer" /> 
</bean> 

<int:channel id="deadChannel"/> 
<int:channel id="outboundChannel"/> 
<int:channel id="objectToJsonChannel" /> 
<int:channel id="outJmsChannel" /> 
<int:channel id="requestChannel"/> 

<int:gateway id="gateway" 
    default-request-timeout="5000" 
    default-reply-timeout="5000" 
    default-request-channel="requestChannel" 
    service-interface="demo.ServiceConfGateway"> 
</int:gateway> 

<int:payload-type-router input-channel="requestChannel" default-output-channel="deadChannel"> 
    <int:mapping type="demo.DeviceConfiguration" channel="objectToJsonChannel"/> 
</int:payload-type-router> 

<int:object-to-json-transformer input-channel="objectToJsonChannel" output-channel="outJmsChannel" /> 

<int-jms:outbound-channel-adapter id="jmsout" channel="outJmsChannel" destination="requestTopic" /> 

在我的主類,我這樣做:

SpringApplication.run(DemoJmsApplication.class, args); 
    ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml"); 
    final DeviceConfiguration dc = ctx.getBean(DeviceConfiguration.class); 
    final ServiceConfGateway service = ctx.getBean(ServiceConfGateway.class); 

    while (true) { 
     service.send(dc); 
     Thread.sleep(1000); 
    } 

我需要這個循環中,我的主類? 這是行得通的,但我想我可以簡化它。

+1

什麼是混淆你/需要澄清?它是不是像你想要的那樣工作? – zapl

回答

0

可以達到同樣的用

<inbound-channel-adapter channel="requestChannel" expression="@dc"> 
    <poller fixed-delay="1000"/> 
</inbound-channel-adapter> 

有了,你不需要<payload-type-router><gateway>了,因爲你總是給你所需要的。

從其他方面來看:會不會更簡單?

+0

謝謝。這很簡單。 – Ludo

1

只是爲了完成。 這裏有一個簡單的方法來發送使用JMS主題對象每一秒在JSON:

<bean id="connectionFactory" 
    class="org.springframework.jms.connection.CachingConnectionFactory"> 
    <property name="targetConnectionFactory"> 
     <bean class="org.apache.activemq.ActiveMQConnectionFactory"> 
      <property name="brokerURL" value="tcp://localhost:61616" /> 
     </bean> 
    </property> 
    <property name="sessionCacheSize" value="10" /> 
</bean> 
<bean id="requestTopic" class="org.apache.activemq.command.ActiveMQTopic"> 
    <constructor-arg value="testconf" /> 
</bean> 

<bean id="confbean" class="demo.DeviceConfiguration"> 
    <property name="id" value="THERMO_001" /> 
    <property name="name" value="thermometer" /> 
</bean> 

<int:channel id="outJmsChannel" /> 
<int:channel id="requestChannel"/> 

<int:inbound-channel-adapter channel="requestChannel" expression="@confbean"> 
    <int:poller fixed-delay="1000"/> 
</int:inbound-channel-adapter> 

<int:object-to-json-transformer input-channel="requestChannel" output-channel="outJmsChannel" /> 

<int-jms:outbound-channel-adapter id="jmsout" channel="outJmsChannel" destination="requestTopic" />