2015年7月11日星期六

Fuse_011:Fuse 快速上手之十一:camel-amq

环境:JBoss Fuse 6.2.0

1. 学习重点
(1)学习使用 camel-amq 组件连接本地的 A-MQ broker。
(2)在 Camel route 中使用 JMS。

2. camel-context.xml 
(1)设计图

(2)源码

<?xml version="1.0" encoding="UTF-8"?>
<!--
    JBoss, Home of Professional Open Source
    Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.

    Licensed 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.
-->
<!--
   This is the OSGi Blueprint XML file defining the Camel context and routes.  Because the file is in the
   OSGI-INF/blueprint directory inside our JAR, it will be automatically activated as soon as the bundle is installed.

   The root element for any OSGi Blueprint file is 'blueprint' - you also see the namespace definitions for both the Blueprint
   and the Camel namespaces.
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <!--
      The namespace for the camelContext element in Blueprint is 'http://camel.apache.org/schema/blueprint'. Additionally,
      we can also define namespace prefixes we want to use them in the XPath expressions in our CBR.

      While it is not required to assign id's to the <camelContext/> and <route/> elements, it is a good idea
      to set those for runtime management purposes (logging, JMX MBeans, ...)
    -->
    <camelContext id="jms-example-context" xmlns="http://camel.apache.org/schema/blueprint" xmlns:order="http://fabric8.com/examples/order/v7">
  <route id="file-to-jms-route">
    <from uri="file:work/jms/input"/>
    <log message="Receiving order ${file:name}"/>
    <to uri="amq:incomingOrders"/>
  </route>
  <route id="jms-cbr-route">
    <from uri="amq:incomingOrders"/>
    <choice>
      <when>
        <xpath>/order:order/order:customer/order:country = 'UK'</xpath>
        <log message="Sending order ${file:name} to the UK"/>
        <to uri="file:work/jms/output/uk"/>
        <log message="Done processing ${file:name}"/>
      </when>
      <when>
        <xpath>/order:order/order:customer/order:country = 'US'</xpath>
        <log message="Sending order ${file:name} to the US"/>
        <to uri="file:work/jms/output/us"/>
      </when>
      <otherwise>
        <log message="Sending order ${file:name} to another country"/>
        <to uri="file:work/jms/output/others"/>
      </otherwise>
    </choice>
  </route>
</camelContext>

</blueprint>

3. 编译、部署到 fabric、卸载
(1)cd /Users/maping/Redhat/fuse/jboss-fuse-6.2.0.redhat-133/quickstarts/camel-amq
(2)mvn clean install
(3)./fuse
(4)fabric:create --wait-for-provisioning
(5)mvn fabric8:deploy
(6)mq-create --kind StandAlone --group mygroup mybroker
(7)container-create-child --profile mq-broker-mygroup.mybroker root mybroker
(8)fabric:container-create-child --profile quickstarts-camel.amq --profile mq-client-mygroup root mychild
(9)fabric:container-list
(10)cp /Users/maping/Redhat/fuse/jboss-fuse-6.2.0.redhat-133/quickstarts/camel-amq/src/main/fabric8/data/* /Users/maping/Redhat/fuse/jboss-fuse-6.2.0.redhat-133/instances/mychild/work/jms/input
(11)cd /Users/maping/Redhat/fuse/jboss-fuse-6.2.0.redhat-133/instances/mychild/work/jms/output
(12)fabric:container-connect mychild
(13)log:display | grep order1
(14)与 child container 断开连接:control + D
(15)fabric:container-stop mychild
(16)fabric:container-delete mychild
(17)fabric:container-stop mybroker
(18)fabric:container-delete mybroker
 
4.  mvn fabric8:deploy 执行失败问题
执行 mvn fabric8:deploy 时,会抛出如下异常:
[ERROR] Failed to execute goal io.fabric8:fabric8-maven-plugin:1.2.0.redhat-133:deploy (default-cli) on project beginner-camel-log: Execution default-cli of goal io.fabric8:fabric8-maven-plugin:1.2.0.redhat-133:deploy failed: An API incompatibility was encountered while executing io.fabric8:fabric8-maven-plugin:1.2.0.redhat-133:deploy: java.lang.NoSuchMethodError: org.eclipse.aether.spi.connector.Transfer.setState(Lorg/eclipse/aether/spi/connector/Transfer$State;)Lorg/eclipse/aether/spi/connector/Transfer;
 经查,这是因为 fabric8:deploy 不支持 maven 3.2.5 以上版本,换成 maven 3.2.1后,异常消失。

没有评论: