带主题的基于消息的 RPC

messagebased RPC with topic

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

最好的方法是什么,让多个回复者做同样的事情?

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

您可以使用具有适当关联和发布策略(以及组超时)的 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]