Skip to content

Commit

Permalink
Camel on EAP - JMS (MDB) rhtconsulting#16
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Tinsley committed Nov 30, 2016
1 parent b71eef0 commit 8280dd3
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 0 deletions.
50 changes: 50 additions & 0 deletions eap/jms_mdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Route Deployment Camel EAP JMS MDB example
====================================
This example deploys two basic Camel routes using the Camel Subsystem in EAP. A Message Driven Bean (MDB) is used to move the message to the Consumer route.

### Requirements:
* JBoss Fuse 6.2.1
* JBoss EAP 6.4.0
* Maven 3.0 or Greater (http://maven.apache.org/)
* Java 8
* ActiveMQ Resource Adapter (http://activemq.apache.org/resource-adapter.html)

Building
-----------------------
To build the project.

mvn clean install

This will build the war including the dependencies.

Building and Deploying to JBoss EAP
-----------------------

To start up EAP browse to your EAP install directory. Then run

/bin/standalone.sh

This will bring up EAP. Once you see logging like this, EAP is up:

11:08:55,464 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.4.0.GA started in 10870ms -
Started 151 of 189 services (56 services are lazy, passive or on-demand)

If you do not already have a user set up for the JBoss Management console you can set one up buy running `$EAP_HOME/bin/add-user.sh` in a separate window. It will walk you through the process. Select 'Management user' when given the option. One this is done and EAP is up, navigate to `http://localhost:9990` and login with your newly created user.

### To Deploy your war:

From the management console navigate to the Runtime tab and select 'Management Deployments' on the left hand side. Once here, select 'Add' and browse to your war file. You can either use the one in your .m2 directory or the one in `fuse-quickstarts/eap/jms_mdb/target`. After choosing the war file, click the 'En/Disable' button to start it.

Alternatively you can deploy your code using the jboss-as-maven-plugin. To do so go into `eap/parent/pom.xml` and change the configuration of the `jboss-as-maven-plugin` to use your management user's `username` and `password` and switch `<skip>` to `false`. Then run:

mvn clean install


Results
-----------------------
Once you have the route started you should be able to look in jboss/standalone/log/server.log and see the following logging:

22:13:25,901 INFO [producer] (Camel (jms-mdb) thread #0 - timer://myTimer) Created Message: Sample AMQ Message
22:13:25,985 INFO [com.redhat.consulting.fusequickstarts.eap.jms.mdb.MessageDrivenBean] (Thread-2 (HornetQ-client-global-threads-1255435117)) Message rec'd: Sample AMQ Message
22:13:26,024 INFO [consumer] (Thread-2 (HornetQ-client-global-threads-1255435117)) Received Message: Sample AMQ Message

78 changes: 78 additions & 0 deletions eap/jms_mdb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.redhat.consulting.fusequickstarts.eap</groupId>
<artifactId>eap-parent</artifactId>
<version>6.2.1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>eap-jms-mdb</artifactId>
<packaging>war</packaging>
<name>Fuse Quickstart :: EAP :: JMS MDB</name>

<!-- Any dependencies needed by your project -->
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.ejb3</groupId>
<artifactId>jboss-ejb3-ext-api</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.redhat.consulting.fusequickstarts.eap.jms.mdb;

import javax.ejb.Startup;
import javax.enterprise.context.ApplicationScoped;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;

/*
* This consumer route receives messages sent from the Producer Template in the MDB
*/

@Startup
@ApplicationScoped
@ContextName("jms-mdb")
public class ConsumerRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
from("direct:consumer")
.routeId("consumer")
.log("Received Message: ${body.text}");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.redhat.consulting.fusequickstarts.eap.jms.mdb;

import javax.annotation.PostConstruct;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.ResourceAdapter;

/*
* This MDB uses a producer template to send messages from the queue to the consumer end point.
*/

@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue1"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class MessageDrivenBean implements MessageListener {

Logger logger = Logger.getLogger(MessageDrivenBean.class.getName());;
CamelContext context;
ProducerTemplate template;

@PostConstruct
public void initialize() throws NamingException{
InitialContext ic = new InitialContext();
context = (CamelContext) ic.lookup("java:jboss/camel/context/jms-mdb");
template = context.createProducerTemplate();
}

public void onMessage(Message message) {
try {
String text = ((TextMessage) message).getText();
logger.info("Message rec'd: " + text);

template.sendBody("direct:consumer",message);

} catch (JMSException e) {
throw new EJBException("Error in JMS operation", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.redhat.consulting.fusequickstarts.eap.jms.mdb;

import javax.annotation.Resource;
import javax.ejb.Startup;
import javax.enterprise.context.ApplicationScoped;
import javax.jms.ConnectionFactory;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;
import org.apache.camel.component.jms.JmsComponent;

/*
* This route runs based on a timer. It creates a message with
* a simple text body and sends it to a queue.
*/

@Startup
@ApplicationScoped
@ContextName("jms-mdb")
public class ProducerRoute extends RouteBuilder {
@Resource(mappedName ="java:/ConnectionFactory")
private ConnectionFactory connectionFactory;

@Override
public void configure() throws Exception {
JmsComponent component = new JmsComponent();
component.setConnectionFactory(connectionFactory);

getContext().addComponent("jms", component);

from("timer://myTimer?fixedRate=true&period=5000")
.routeId("producer")
.setBody().simple("Sample JMS Message")
.log("Created Message: ${body}")
.to("jms:queue:queue1");

}

}
6 changes: 6 additions & 0 deletions eap/jms_mdb/src/main/webapp/WEB-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

<!-- This file is intentionally left blank. It is required by the container to activate CDI. -->

</beans>
3 changes: 3 additions & 0 deletions eap/jms_mdb/src/main/webapp/WEB-INF/jboss-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<jboss-web>
<context-root>eap-route-deployment</context-root>
</jboss-web>
1 change: 1 addition & 0 deletions eap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>rest_dsl</module>
<module>jpa</module>
<module>amq_mdb</module>
<module>jms_mdb</module>
<module>jaxrs_proxy</module>
</modules>
</project>

0 comments on commit 8280dd3

Please sign in to comment.