【问题标题】:Spring , Camel, JMS IntegrationSpring、Camel、JMS 集成
【发布时间】:2013-01-06 02:19:09
【问题描述】:

我 100% 是 JMS 和 Camel 的新手

这是场景

我有一个 Spring Web 应用程序、一个 JMS (ActiveMQ)、Camel

Web 应用程序需要通过 Camel 以异步方式向 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);

这很好用。但问题是,这是同步的。

我希望这是异步的。

怎么做

【问题讨论】:

  • 那样的话,怎么可能呢?更新我的问题

标签: spring jms apache-camel


【解决方案1】:

您不需要在 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);

【讨论】:

  • camelTemplate.asyncSendBody("jms:queue:myQueue", ExchangePattern.InOnly, someData);这很有道理,但我什么时候才能取回我的数据。逻辑会从它离开的地方继续吗?
  • 好吧,假设您使用 ExchangePattern.InOut,asyncSendBody 将返回一个 Future 对象,以便稍后获取您的返回数据。但是,您说网页将继续加载(我假设,完成?)。那么,您想什么时候回读回复?
  • 这是场景,页面加载 -> 用户操作 -> 向骆驼发送数据 -> 用户继续访问网站 -> 骆驼/jms 响应 -> 系统更新 app/db 中的某些内容 - > 用户弹出一个窗口
  • 当然。但是camel/jms 只会做部分,而不是弹出的东西,因为这听起来像网络推送技术。
  • 是的!弹出是 UI 部分
【解决方案2】:

答案很简单

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

这应该是

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

只要不提ExchangePattern就可以了。

道德:请正确阅读文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2023-03-05
    • 2015-11-01
    • 2013-02-04
    • 1970-01-01
    相关资源
    最近更新 更多