2014年3月8日星期六

Red Hat JBoss Fuse - Sending message to queue in ActiveMQ via Camel

We will need to install 3 softwares before hand.

A. Fuse
B. FuseIDE
C. Maven

To get JBoss Fuse, logon to JBoss Customer Portal, download the beta version. Or Visit the Fuse.org .


Unzip the file to install JBoss Fuse. (A best practice for installing the software is not to have space or any special characters in the path)


open up users.properties file under $FUSE_INSTALLATION_PATH/etc
by taking out # before admin setting, we have create a user admin, with password admin that has the right of an administrator.


Fuse IDE is the development tool, it's very helpful, it has various examples for you to get started. So we download it from JBoss Customer Portal, or Visit the Fuse.org for it.  Depends on your own operating system, download the version that apply to you.

Install the IDE by choosing the installation path and basically just click next, next to complete the installation.



Another must have to do this demo is maven. I am sure almost everyone has it now. I will explain and show why we needed it later.

For this demo, there are 2 major step.

A.For the running environment, we are going to setup a Fabric management environment with JBoss Fuse. Create and start a ActiveMQ broker (Master-Slave), so we can put message into the queue using Camel. And create a container called "demo" for running the camel route.
B. Create a Camel Project. Within the project we are going to create 2 routes.
  1. Sends a message into a queue called "demo" every 5 secs.
  2. Listen on the demo queue, as soon as it receives message, print it out.





1.Go to $FUSE_INSTALLATION_PATH/bin,run ./fuse (For Windows run fuse.bat) After fuse got started, create a fabric management enviorment by type in the following command.

JBossFuse:karaf@root> fabric:create --zookeeper-password admin 


2. To access the new management console for JBoss Fuse 6.1 . Access http://localhost:8181 in your browser . Logon with admin/admin (ID/PWD)。

After logon, checkout the top right had corner, choose Fabric under Container.



3. Click MQ on the top menu, you will be able to see the embedded broker for this root container. But we are not going to use this one. What we are going to do is to setup a ActiveMQ Broker to handle the messaging.


Name the Broker : "demoBroker", choose MasterSlave as the backup strategy. And Click on the Create Broker. 

Since only the config of the broker has been created, you will see 0 and red show on the screen for our demoBroker. Click on the number "0" to create containers to run it.


Everything is very straight forward to create a container. Set container's name and because we choose MasterSlave that has minimum number of 2 instance, therefore we will create 2 containers here.


2 containers are created. 



To view the detail of each container, click on the icon on the right-hand side of the container.



Under ActiveMQ, All the details of the broker will be shown on the right-hand side of the console. Look for "Open wire url" as we are going to need later.





4. In your FUSE IDE, create a Fuse Project .


One of the great thing about FUSE IDE, is that you don't need to find all the dependency on your own, many sample archetype are ready done for you to reference it!  Choose activemq for this demo.

Group ID: org.blogdemo 
Artifact ID: camel-amq



delete the default example, we are going to create a new one.



Under src/main/resources/META-INF/spring , create a Camel XML file.




5.  Configure the ActiveMQ broker URL we previously get from the console on ActiveMQComponent. And don't forget also to put in the ID/PWD.



  
  
  
 


6.To create the first route,



Set an endpoint by dragging it on to the canvas.


By using a camel component timer, we can set this route to be executed in every 5 sec.



Drag a log component on to the canvas.

Click the log component on the canvas, you will be able to enter the log message in the properties tag below.




Under Transformation, drag setBody component into canvas. This sets the message to send into the queue.


Drag another log component to log the message content by setting the following content below.
printing body => ${body}



Create another endpoint pointing to the queue. Set the Uri to activemq:queue:demo


the activemq:queue:demo looks quite familiar? Yes, it's the bean that we set at the very beginning of camel config xml.

Connect all the components together by dragging the arrow from one component to the next one.


Save. We have completed the first route.


7. Right click on the xml file, select Run AS -> Local Camel Context (Without tests) to run the xml.


You will be able to see the content we set in the log.


The camel xml is as follows: (Please not the port is depended your activemq open wire URI)



 
  
  
  
 
  
    
        
        
        
            大家好
        
        
        
    





8. Next step is to create the second route. This one is much easier. It simple listen to the demo queue. and print every message that sent into it.



Again, go to src/main/resources/META-INF/spring and create another Camel XML file.


name : camelListener.xml 


Add the Broker bean and it's URL and ID/PWD into camelListener.xml.


Drag Endpoint to canvas, under Properties -> Detail set the uri to activemq:queue:demo so this endpoint will listen to the queue that data was sent.


Add log to print out the content of the message received. Set the following to the message.
msg body => ${body}



Connect both components together by dragging the arrow from first component to the second one.


Done, and remember to save.


The whole Camel xml is as follows.


  
  
  
 
  
    
        
        
    






9. Right click on the camelListner.xml file, select Run AS -> Local Camel Context (Without tests) to run the xml.


You will be able to see the message we sent to the queue from log.




10. Deploying Camel project into fabric. To do this, we are going to build it with maven then deploy on to Profile through maven.


There are several ways to deploy onto Fabric Profile. This time we are going to install as a OSGi Bundle. For that, please replace your current pom.xml to following pom.xml.




  4.0.0

  org.blogdemo
  camel-amq
  bundle
  1.0.0-SNAPSHOT

  A Camel Spring Route
  http://www.myorganization.org

  
    UTF-8
    UTF-8
  

  
    
      release.fusesource.org
      FuseSource Release Repository
      http://repo.fusesource.com/nexus/content/repositories/releases
      
        false
      
      
        true
      
    
    
      snapshot.fusesource.org
      FuseSource Snapshot Repository
      http://repo.fusesource.com/nexus/content/repositories/snapshots
      
        true
      
      
        false
      
    
  

  
    
      release.fusesource.org
      FuseSource Release Repository
      http://repo.fusesource.com/nexus/content/repositories/releases
      
        false
      
      
        true
      
    
    
      snapshot.fusesource.org
      FuseSource Snapshot Repository
      http://repo.fusesource.com/nexus/content/repositories/snapshots
      
        true
      
      
        false
      
      
  

  
    
      org.apache.camel
      camel-core
      2.10.0.fuse-71-047
    
    
      org.apache.camel
      camel-spring
      2.10.0.fuse-71-047
    

    
    
      org.slf4j
      slf4j-api
      1.6.6
    
    
      org.slf4j
      slf4j-log4j12
      1.6.6
    
    
      log4j
      log4j
      1.2.17
    

    
    
      org.apache.camel
      camel-test-spring
      2.10.0.fuse-71-047
      test
    

 
    
      org.apache.activemq
      activemq-core
      5.7.0.fuse-71-047
    
    
      org.apache.activemq
      activemq-camel
      5.7.0.fuse-71-047
    
  

  
    install

    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        2.5.1
        
          1.6
          1.6
        
      
      
        org.apache.maven.plugins
        maven-resources-plugin
        2.4.3
        
          UTF-8
        
      

      
      
        org.apache.camel
        camel-maven-plugin
        2.10.0.fuse-71-047
      
      
  org.apache.felix
  maven-bundle-plugin
  true
  2.4.0
  
   
    ${project.groupId}.${project.artifactId}
    ${project.name}
    ${project.version}
   
  
 
    
  

  




For pom.xml nothing much has changed, only we ask it to package as a bundle.

<packaging>bundle</packaging

And add the bundle plugin. 

<plugin>
   <groupId>org.apache.felix</groupId>
   <artifactId>maven-bundle-plugin</artifactId>
   <extensions>true</extensions>
   <version>2.4.0</version>
   <configuration>
      <instructions>
         <Bundle-SymbolicName>
            ${project.groupId}.${project.artifactId}
         </Bundle-SymbolicName>
         <Bundle-Name>${project.name}</Bundle-Name>
         <Bundle-Version>${project.version}</Bundle-Version>
         </instructions>
      </configuration>
</plugin>

Go to the project directory, execute mvn clean install




11. In Fuse prompt add a profile blogdemoP
JBossFuse:karaf@root> profile-create blogdemoP

Deploy the camel-amq project we just created onto the Profile via maven.
JBossFuse:karaf@root> profile-edit -b mvn:org.blogdemo/camel-amq/1.0.0-SNAPSHOT blogdemoP

12. We are going to create a container in Fuse Console.  Then add the profile we just created onto the container.  Click on "+Container".
Name the new container : demo, on the button left corner, add full profile.

Add blogdemoP Profile too. Then click on "Create And Start Container"
在左邊下方,安裝剛剛建立的blogdemoP Profile, 然後按下左上方的 "Create And Start Container"


After demo container started, click on the detail icon on the right hand side.

A Popup window or tab will display the status and config of the chosen container. Click on Logs to see what's going on.


That's all.
===============================================

===============================================
In the Fuse Console,

In the demo container ,Click on Camel,  with left menu Routes -> Attributes you can see how many times the route has been executed.

Click on the route if you want to see more specific details on the route. Like how many times each component has been executed.

As with the Diagram Tab, It will display all the endpoint and route in a very grasphical way.

Configuration of each route can be seen by clicking on the route on the diagram itself.



ActiveMQ details are also shown on the console. Go to Master Broker's container detail by clicking on the icon. 

On the left-hand menu choose queue, under Attributes, you will able see the stats on each queue.