diff --git a/eap/jms_mdb/README.md b/eap/jms_mdb/README.md new file mode 100644 index 0000000..3e5da05 --- /dev/null +++ b/eap/jms_mdb/README.md @@ -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 `` 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 + diff --git a/eap/jms_mdb/pom.xml b/eap/jms_mdb/pom.xml new file mode 100644 index 0000000..4cba23d --- /dev/null +++ b/eap/jms_mdb/pom.xml @@ -0,0 +1,78 @@ + + 4.0.0 + + com.redhat.consulting.fusequickstarts.eap + eap-parent + 6.2.1 + ../parent/pom.xml + + eap-jms-mdb + war + Fuse Quickstart :: EAP :: JMS MDB + + + + + org.slf4j + slf4j-log4j12 + + + org.slf4j + slf4j-api + + + org.apache.camel + camel-core + + + org.apache.camel + camel-cdi + + + org.apache.camel + camel-jms + + + javax.enterprise + cdi-api + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.2_spec + + + org.jboss.spec.javax.servlet + jboss-servlet-api_3.1_spec + + + org.apache.activemq + activemq-camel + + + org.apache.camel + camel-test + + + org.jboss.ejb3 + jboss-ejb3-ext-api + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.apache.maven.plugins + maven-war-plugin + + + org.jboss.as.plugins + jboss-as-maven-plugin + + + + diff --git a/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/ConsumerRoute.java b/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/ConsumerRoute.java new file mode 100644 index 0000000..78a0292 --- /dev/null +++ b/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/ConsumerRoute.java @@ -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}"); + } + +} \ No newline at end of file diff --git a/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/MessageDrivenBean.java b/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/MessageDrivenBean.java new file mode 100644 index 0000000..7fe87b2 --- /dev/null +++ b/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/MessageDrivenBean.java @@ -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); + } + } + +} \ No newline at end of file diff --git a/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/ProducerRoute.java b/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/ProducerRoute.java new file mode 100644 index 0000000..bd802d6 --- /dev/null +++ b/eap/jms_mdb/src/main/java/com/redhat/consulting/fusequickstarts/eap/jms/mdb/ProducerRoute.java @@ -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"); + + } + +} \ No newline at end of file diff --git a/eap/jms_mdb/src/main/webapp/WEB-INF/beans.xml b/eap/jms_mdb/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..465c712 --- /dev/null +++ b/eap/jms_mdb/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/eap/jms_mdb/src/main/webapp/WEB-INF/jboss-web.xml b/eap/jms_mdb/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000..0b4d7fa --- /dev/null +++ b/eap/jms_mdb/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,3 @@ + + eap-route-deployment + \ No newline at end of file diff --git a/eap/pom.xml b/eap/pom.xml index bf90fa7..df55895 100644 --- a/eap/pom.xml +++ b/eap/pom.xml @@ -18,6 +18,7 @@ rest_dsl jpa amq_mdb + jms_mdb jaxrs_proxy