簡體   English   中英

Spring,Camel,JMS集成

[英]Spring , Camel, JMS Integration

是100%JMS和Camel的新手嗎

這是場景

我有一個Spring Web應用程序,一個JMS(ActiveMQ),Camel

Web應用程序需要通過駱駝以異步方式將信息發送到JSM。 例如,在將信息發送給Camel之后,網站不應等待響應。 該網頁應繼續。

並且,我的應用程序應偵聽隊列中隊列中的任何響應。 一旦收到任何響應,就必須調用特定的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);

這很好。 但是問題是,這是同步的。

我希望這是異步的。

怎么做

您無需在Camel 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:queue:myQueue”,ExchangePattern.InOnly,someData);

答案很簡單

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

這應該是

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

只要不提及ExchangePattern ,您就完成了。

道德:請正確閱讀文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM