2015-05-04 76 views
1

簡單的基於消息的RPC很容易創建。服務器端導出服務,客戶端使用代理。基於消息的RPC與主題

什麼是最好的方法,使多個repliers同樣的事情?

我想發送來自客戶端的請求。然後客戶端等待收到所有(可能超時)回覆。

回答

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] 
+0

嘿加里,感謝您的回答。我會嘗試,但你能給我一個小例子嗎?我從來沒有使用聚合器:) – Smoothi

+0

非常感謝。我會在下週嘗試一下,並告訴你這個解決方案是否能解決我的問題。 :) – Smoothi