如何在Apache Camel中使用AMQP代替JMS?

时间:2011-01-05 22:28:23

标签: amqp apache-camel

所有的问候 我正在使用具有以下配置的JMS在客户端和客户端之间发送消息。服务器,我需要用AMQP替换jms,我需要一些指南,所以请指教:

客户端的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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-2.0.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring-1.4.0.xsd
        http://cxf.apache.org/transports/camel http://cxf.apache.org/transports/camel.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-camel.xml" />

    <!--  Directly defined ConnectionFactory  -->
    <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <!--  By defining a JmsComponent called jms you can use the JMS transport in routes
        You may define several of these components if you have different JMS servers for
        different services. They can be distinguished in the routes by their id 
    -->
    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <property name="useMessageIDAsCorrelationID" value="true" />
    </bean>

    <!-- Add the camel transport to CXF. So you can define endpoint in the form camel://direct:<EndpointName>
         in cxf client and server endpoints. These endpoints are then available in camel 
         as direct:<EndpointName> -->
    <bean class="org.apache.camel.component.cxf.transport.CamelTransportFactory">
        <property name="bus" ref="cxf" />
        <property name="camelContext" ref="camelContext" />
        <property name="transportIds">
            <list>
                <value>http://cxf.apache.org/transports/camel</value>
            </list>
        </property>
    </bean>

    <!-- Define a cxf endpoint based on client stub generated from a wsdl.
        It is important to provide serviceName and endpointName so the wsdl is not needed
        at runtime. As far as I know the serviceName and endpointName do not have to have a special
        convention but it is good practice to use the service namespace and Service Interface name in the names
     -->
    <client id="CustomerService" xmlns="http://cxf.apache.org/jaxws" xmlns:customer="http://customerservice.example.com/"
        serviceName="customer:CustomerServiceService" endpointName="customer:CustomerServiceEndpoint"
        address="camel://direct:CustomerService" serviceClass="com.example.customerservice.CustomerService">
        <features>
            <!-- Enables logging of SOAP messages. -->
            <!-- <logging xmlns="http://cxf.apache.org/core" /> -->
        </features>
    </client>

    <!-- This context defines a route for each endpoint. For client endpoints you route from
        the cxf endpoint to the jms queue or topic. For servers you route from the jms queue or 
        topic to the cxf endpoint -->
    <camelContext id="camelContext" xmlns="http://activemq.apache.org/camel/schema/spring">
        <route>
            <from uri="direct:CustomerService" />
            <to uri="jms://queue:CustomerService" />
        </route>
    </camelContext>

</beans>

服务器-的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring-1.4.0.xsd
        http://cxf.apache.org/transports/camel http://cxf.apache.org/transports/camel.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-camel.xml" />

    <!--  Directly defined ConnectionFactory  -->
    <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <!--  By defining a JmsComponent called jms you can use the JMS transport in routes
        You may define several of these components if you have different JMS servers for
        different services. They can be distinguished in the routes by their id 
    -->
    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <property name="useMessageIDAsCorrelationID" value="true" />
    </bean>

    <!-- Add the camel transport to CXF. So you can define endpoint in the form camel://direct:<EndpointName>
         in cxf client and server endpoints. These endpoints are then available in camel 
         as direct:<EndpointName> -->
    <bean class="org.apache.camel.component.cxf.transport.CamelTransportFactory">
        <property name="bus" ref="cxf" />
        <property name="camelContext" ref="camelContext" />
        <property name="transportIds">
            <list>
                <value>http://cxf.apache.org/transports/camel</value>
            </list>
        </property>
    </bean>

    <!-- Define a cxf endpoint based on client stub generated from a wsdl.
        It is important to provide serviceName and endpointName so the wsdl is not needed
        at runtime. As far as I know the serviceName and endpointName do not have to have a special
        convention but it is good practice to use the service namespace and Service Interface name in the names
     -->
    <endpoint 
        xmlns="http://cxf.apache.org/jaxws"
        xmlns:customer="http://customerservice.example.com/"
        id="CustomerService" 
        address="camel://direct:CustomerService" 
        serviceName="customer:CustomerServiceService"
        endpointName="customer:CustomerServiceEndpoint" 
        implementor="com.example.customerservice.impl.CustomerServiceImpl">
        <features>
            <!-- Enables logging of SOAP messages. -->
            <logging xmlns="http://cxf.apache.org/core" />
        </features>
    </endpoint>

    <!-- This context defines a route for each endpoint. For client endpoints you route from
        the cxf endpoint to the jms queue or topic. For servers you route from the jms queue or 
        topic to the cxf endpoint -->
    <camelContext id="camelContext" trace="true" xmlns="http://activemq.apache.org/camel/schema/spring">
        <route>
            <from uri="jms://queue:CustomerService" />
            <to uri="direct:CustomerService" />
        </route>
    </camelContext>
</beans>

1 个答案:

答案 0 :(得分:1)

你只需要替换

<!--  Directly defined ConnectionFactory  -->
<bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616" />
</bean>

<!--  By defining a JmsComponent called jms you can use the JMS transport in routes
    You may define several of these components if you have different JMS servers for
    different services. They can be distinguished in the routes by their id 
-->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
    <property name="connectionFactory" ref="jmsConnectionFactory" />
    <property name="useMessageIDAsCorrelationID" value="true" />
</bean>

<!--  Directly defined ConnectionFactory  -->
<bean id="jmsConnectionFactory" class="org.apache.qpid.client.AMQConnectionFactory">
    <constructor-arg index="0" type="java.lang.String" value="amqp://guest:guest@/test?brokerlist='vm://:1'" />
</bean>

<!--  By defining a JmsComponent called jms you can use the JMS transport in routes
    You may define several of these components if you have different JMS servers for
    different services. They can be distinguished in the routes by their id 
-->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
    <property name="connectionFactory" ref="jmsConnectionFactory" />
    <property name="useMessageIDAsCorrelationID" value="true" />
</bean>

`