2013-01-22 135 views
2

現在是一個100%新JMS和駱駝春天,駱駝,JMS集成

這是該方案

我有一個春天的Web應用程序,JMS(ActiveMQ的),駱駝

Web應用程序需要通過異步方式將信息發送給JSM。 即向駱駝發送信息後,網站不應等待迴應。網頁應該繼續。

而且,我的應用程序應該監聽隊列中隊列中的任何響應。一旦收到任何響應,就必須調用特定的bean。

這可能嗎?

這是配置我的客戶

駱駝語境:

<!-- START SNIPPET: e1 --> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:camel="http://camel.apache.org/schema/spring" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> 
    <!-- END SNIPPET: e1 --> 

    <!-- START SNIPPET: e2 --> 
    <camel:camelContext id="camel-client"> 
    <camel:template id="camelTemplate"/> 
    </camel:camelContext> 
    <!-- END SNIPPET: e2 --> 

    <!-- START SNIPPET: e3 --> 
    <!-- Camel JMSProducer to be able to send messages to a remote Active MQ server --> 
    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"> 
    <property name="brokerURL" value="tcp://localhost:61610"/> 
    </bean> 
    <!-- END SNIPPET: e3 --> 

</beans> 

駱駝代碼:

ApplicationContext context = new ClassPathXmlApplicationContext("camel-client.xml"); 

// get the camel template for Spring template style sending of messages (= producer) 
ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class); 

System.out.println("Invoking the multiply with 22"); 
// as opposed to the CamelClientRemoting example we need to define the service URI in this java code 
int response = (Integer)camelTemplate.sendBody("jms:queue:numbers", ExchangePattern.InOut, 22); 
System.out.println("... the result is: " + response); 

這是工作好。但問題是,這是同步的。

我希望這是異步的。

怎麼辦呢

+0

是的,這是可能的。 –

+0

在這種情況下,它怎麼可能?更新我的問題 – madhairsilence

回答

0

答案很簡單

int response = (Integer)camelTemplate.sendBody("jms:queue:numbers", ExchangePattern.InOut, 22); 

這應該是

int response = (Integer)camelTemplate.sendBody("jms:queue:numbers",22); 

只是不提ExchangePattern和你做。

道德:請仔細閱讀說明文件。

0

你不會需要做的駱駝Remoting的例子所有的東西(我懷疑你有開始了)。

所以,你想通過一個隊列進行一些處理。只是讓我不要誤解這一點:

Web Request -> JMS queue -> Camel -> Bean -> Done 

創建路線:

<camel:camelContext id="camel-client"> 
    <camel:template id="camelTemplate"/> 
    <camel:route> 
     <camel:from uri="jms:queue:myQueue"/> 
     <camel:bean ref="someBean"/> 
    </camel:route> 
</camel:camelContext> 

然後在你的代碼,該消息只是火: camelTemplate.asyncSendBody(「JMS:隊列:myQueue中「,ExchangePattern.InOnly,someData);

+0

camelTemplate.asyncSendBody(「jms:queue:myQueue」,ExchangePattern.InOnly,someData); 這很有道理,但是什麼時候才能找回我的數據。邏輯會從它離開的地方繼續嗎? – madhairsilence

+0

那麼,說你做的ExchangePattern。相反,asyncSendBody將會返回一個'Future'對象來稍後獲取你的返回數據。但是,你說網頁會繼續加載(我認爲,完成?)。那麼,你什麼時候想回讀迴應? –

+0

這是場景,頁面加載 - >用戶操作 - >將數據發送給駱駝 - >用戶與站點衝突 - >駱駝/ jms響應 - >系統更新app/db中的某些內容 - >用戶彈出 – madhairsilence