Spring 集成:将 json 中的对象发送到 jms

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" />

在我的 Main class 中,我正在这样做:

    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 class 中需要这个循环吗? 这可行,但我想我可以简化它。

你可以用

达到同样的效果
<inbound-channel-adapter channel="requestChannel" expression="@dc">
   <poller fixed-delay="1000"/>
</inbound-channel-adapter>

这样你就不再需要 <payload-type-router><gateway> 因为你总是发送你需要的东西。

另一边:会不会像你问的那么简单?..

刚刚完成。 这是使用 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" />