【问题标题】:Spring Integration: send object in json to jmsSpring Integration:将json中的对象发送到jms
【发布时间】:2015-09-16 02:02:16
【问题描述】:

我有点困惑,我需要澄清一下:

我正在尝试转换 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);
    }

我的 Main 类中是否需要这个循环? 这是可行的,但我想我可以简化它。

【问题讨论】:

  • 什么让您感到困惑/需要澄清?是不是像你想的那样工作?

标签: java json jms spring-integration


【解决方案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" />

【讨论】:

    【解决方案2】:

    您也可以使用

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

    这样你就不再需要&lt;payload-type-router&gt;&lt;gateway&gt;,因为你总是发送你需要的东西。

    从另一面看:像你问的那样简单吗?..

    【讨论】:

    • 谢谢。这要简单得多。
    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2015-11-11
    • 2015-07-08
    • 2014-10-20
    相关资源
    最近更新 更多