基于消息的RPC与主题

时间:2015-05-04 14:17:06

标签: spring jms spring-integration messaging spring-jms

基于消息的简单RPC非常容易创建。服务器端导出服务,客户端使用代理。

使用多个replier做同样的事情的最佳方法是什么?

我想从客户端发送请求。然后客户端等待收到所有(可能有超时)回复。

1 个答案:

答案 0 :(得分:1)

您可以使用具有适当关联和释放策略的aggregator(以及组超时)。

修改

这是使用JMS主题的版本......

<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory">
        <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="vm://localhost"/>
        </bean>
    </property>
    <property name="sessionCacheSize" value="10"/>
</bean>

<bean id="requestTopic" class="org.apache.activemq.command.ActiveMQTopic">
    <constructor-arg value="topic.demo"/>
</bean>

<bean id="replyQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="queue.reply"/>
</bean>

<int-stream:stdin-channel-adapter id="stdin" channel="stdinToJmsoutChannel"/>

<int:channel id="stdinToJmsoutChannel"/>

<int:chain input-channel="stdinToJmsoutChannel">
    <int:header-enricher>
        <int:header name="jms_replyTo" ref="replyQueue" />
    </int:header-enricher>
    <int-jms:outbound-channel-adapter destination="requestTopic" />
</int:chain>

<int-jms:message-driven-channel-adapter channel="jmsReplyChannel" 
    destination="replyQueue"/>

<int:channel id="jmsReplyChannel" />

<int:chain input-channel="jmsReplyChannel">
    <int:aggregator group-timeout="5000" expire-groups-upon-timeout="false"
        send-partial-result-on-expiry="true"
        discard-channel="logLateArrivers"
        correlation-strategy-expression="headers['jms_correlationId']"
        release-strategy-expression="size() == 2"/>
    <int-stream:stdout-channel-adapter append-newline="true"/>
</int:chain>

<int:logging-channel-adapter id="logLateArrivers" />

<!-- Subscribers -->

<int-jms:inbound-gateway request-channel="upcase" request-destination="requestTopic" />

<int-jms:inbound-gateway request-channel="upcase" request-destination="requestTopic" />

<int:transformer input-channel="upcase" expression="payload.toUpperCase()" />

在控制台中输入请求:

Please type something and hit <enter>

foo
[FOO, FOO]
bar
[BAR, BAR]
baz
[BAZ, BAZ]