[英]Spring Integration: send object in json to 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);
}
我的主班需要这个循环吗? 这是可行的,但我想我可以简化一下。
刚刚完成。 这是使用jms和topic在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" />
您可以通过实现相同
<inbound-channel-adapter channel="requestChannel" expression="@dc">
<poller fixed-delay="1000"/>
</inbound-channel-adapter>
这样,您就不再需要<payload-type-router>
和<gateway>
了,因为您总是发送需要的东西。
从另一面看:您会更简单吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.