`
xkorey
  • 浏览: 151016 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

ActiveMQ+Camel+Spring+jms Demo(一)

阅读更多
俗话说的好:温故而知新。这就是我发布到这里的原因。

ActiveMQ+Camel+Spring 简单整合

官网我就不列了,我做的这个demo只是跑通了而已,还有很多需要完善的地方。

待续……

欢迎拍砖


简单描述下我的这个demo:web界面有2个请求,一个是向mq发送数字消息,一个是文本消息。
并将响应的结果回显到web。

不完美的地方:没有用2台电脑来测试mq请求和应答。测了回补上,待续……


pom文件就不贴了。


就贴比较关键的配置,其实这些都是从官方example中抄过来的。

camel-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:broker="http://activemq.apache.org/schema/core"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
  <context:component-scan base-package="com.activeMqCamelSpring.demo"/>

  <context:property-placeholder location="classpath:camel.properties"
                                ignore-resource-not-found="true"/>

  <camel:camelContext id="camel-server">
    <camel:package>com.activeMqCamelSpring.demo.server</camel:package>
    <camel:jmxAgent id="agent" createConnector="true"/>
  </camel:camelContext>
    <camel:camelContext id="camel-client">
        <camel:template id="camelTemplate"/>
    </camel:camelContext>

  <broker:broker useJmx="true" persistent="false" brokerName="myBroker">
    <broker:transportConnectors>
      <broker:transportConnector name="vm" uri="vm://myBroker"/>
      <broker:transportConnector name="tcp" uri="tcp://localhost:${tcp.port}"/>
    </broker:transportConnectors>
  </broker:broker>

  <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="brokerURL" value="vm://myBroker"/>
  </bean>

</beans>


camel.properties 就一句:
tcp.port=61610


web.xml中
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:camel-server.xml,
            classpath:spring-base.xml
        </param-value>
    </context-param>


ServerRoutes中也很简单
public class ServerRoutes extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("jms:queue:numbers").to("multiplier");
        from("jms:queue:strings").to("textplier");
    }

}


multiplier 类
@Service(value = "multiplier")
public class Treble {

    public int multiply(final int originalNumber) {
        return originalNumber * 3;
    }

}

textplier类
@Service(value = "textplier")
public class TextPlier {

    public String say(final String text){
        return "activeMQ with camel for :"+text;
    }

}


还有一个service

@Service
public class CamleService {

    @Autowired
    AbstractApplicationContext context;

    public int sendHelloWorldNumToCamel(){
        ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
        int response = (Integer)camelTemplate.sendBody("jms:queue:numbers", ExchangePattern.InOut, 22);
        return response;
    }

    public String sendHelloWorldTextToCamel(String text){
        ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
        String response = (String)camelTemplate.sendBody("jms:queue:strings", ExchangePattern.InOut, text);
        return response;
    }


}


运行效果截图




附件是camel官方的example和我的demo打包


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics