基于消息的RPC主题



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

用多个重架制作同一件事的最好方法是什么?

我想从客户端发送请求。然后,客户等待所有(也许与超时)收到答复。

您可以使用具有适当关联和发布策略(以及组超时)的聚合器。

编辑:

这是使用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]

最新更新