`

在Karaf中部署Camel-AMP使用jms的记录

阅读更多

本文记录是在  Karaf 环境下使用 camel-amp 的过程,使用的 Apollo 作为  jms 服务器。

 

package example;

import java.util.Date;

public class Show {
   
    public Show(){
        //
    }
   
    public String time(){
        return (new Date()).toString();
    }
   
    public void print(String msg){
        System.out.println("-->pring("+msg+")");
    }
}

 

使用 camel-spring方式:

在 resources目录下建立 META-INF/spring/camel-spring.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"
       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">
 
 
  <bean id="jms-factory" class="org.apache.qpid.amqp_1_0.jms.impl.ConnectionFactoryImpl" 
          factory-method="createFromURL">  
            <constructor-arg type="java.lang.String"
                value="amqp://admin:admin@localhost:61613" />
            <property name="topicPrefix" value="topic://" />
            <property name="queuePrefix" value="queue://" />
             
  </bean>
     
  <bean id="amqp" class="org.apache.camel.component.amqp.AMQPComponent">
     <property name="connectionFactory" ref="jms-factory"/> 
  </bean>
  
  
   
  <bean id="show" class="example.Show"/> 
  
  <camelContext id="camel-test-amqp" xmlns="http://camel.apache.org/schema/spring"> 
        <route>
            <from uri="timer:fire?period=5s"/>
            <to uri="bean:show?method=time"/> 
            <setBody>
                <simple>现在时间:${body}</simple>
            </setBody>
            <to uri="bean:show?method=print(${body})"/> 
            <to uri="amqp:topic:time"/>  
        </route>  
  </camelContext>
 
   <camelContext xmlns="http://camel.apache.org/schema/spring">  
         <route>
            <from uri="amqp:topic:systm"/>
            <setBody>
                <simple> 从[topic:systm]收到,body=${body}</simple>
            </setBody>
            <to uri="bean:show?method=print(${body})"/> 
            <to uri="log:topicTest"/>
        </route>  
       
        <route>
            <from uri="amqp:topic:time"/>
            <setBody>
                <simple> 从[topic:time]收到msg=${body}</simple>
            </setBody>
            <to uri="bean:show?method=print(${body})"/> 
            <to uri="log:topicTest"/>
        </route>  
    
        <route>
            <from uri="amqp:queue:time"/>
            <setBody>
                <simple> *****收到Queue ,${body}</simple>
            </setBody>
            <to uri="bean:show?method=print(${body})"/> 
            <to uri="log:topicTest"/>
        </route> 

  </camelContext> 


</beans>

或者用 osgi/blueprint 方式,在 resources 目录下建立:

OSGI-INF/bluepring/camel-route.xml 文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
           
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
   
    <bean id="show" class="example.Show"/> 
   
    <bean id="jms-factory" class="org.apache.qpid.amqp_1_0.jms.impl.ConnectionFactoryImpl" 
          factory-method="createFromURL">  
             <argument value="amqp://admin:admin@localhost:61613"/>
            <property name="topicPrefix" value="topic://" />
            <property name="queuePrefix" value="queue://" />
    </bean>

    <bean id="amqp" class="org.apache.camel.component.amqp.AMQPComponent">
       <property name="connectionFactory" ref="jms-factory"/> 
    </bean>
 
    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route>
            <from uri="timer:fire?period=1s"/>
            <to uri="bean:show?method=time"/> 
            <setBody>
                <simple>现在时间:${body}</simple>
            </setBody>
         
            <to uri="bean:show?method=print(${body})"/>
         
            <to uri="amqp:topic:time"/> 
             
            <to uri="log:topicTest"/> 
        </route> 
       
    </camelContext>
   
    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route>
            <from uri="amqp:topic:time"/>
            <setBody>
                <simple> *****收到 ,${body}</simple>
            </setBody>
            <to uri="bean:show?method=print(${body})"/> 
            <to uri="log:topicTest"/>
        </route>  
    </camelContext> 
</blueprint>

打包成一个 jar 文件, 如: amqp-test-1.0.jar 。

从 maven 库中下载一下三个 jar , 文件, 分别为:

qpid-amqp-1-0-common-0.32.jar

qpid-amqp-1-0-client-jms-0.32.jar

qpid-amqp-1-0-client-0.32.jar

geronimo-jms_1.1_spec-1.1.1.jar

 

将上述 5 个 jar 复制到  deploy 目录下,完成在  karaf 环境的部署,就完成了简单的 amqp - jms 的接收和发送,记得请安装 apollo , 我使用的是: apache-apollo-1.7.1

 

 

 

分享到:
评论
Global site tag (gtag.js) - Google Analytics