From 98538ca52985a01c41aa78ce2879099517f31d7c Mon Sep 17 00:00:00 2001 From: John Ament Date: Sun, 6 Mar 2011 09:38:01 -0500 Subject: [PATCH] Added an example project that demonstrates how you can publish REST messages via JMS Events. Demonstrates the Mapping Interfaces. --- examples/seam-jms-example-rest/pom.xml | 109 ++++++++++++++++++ examples/seam-jms-example-rest/readme.md | 22 ++++ .../jms/examples/rest/MappingInterface.java | 40 +++++++ .../jms/examples/rest/MessageObserver.java | 43 +++++++ .../examples/rest/MessageSendingResource.java | 103 +++++++++++++++++ .../jms/examples/rest/QueueHandlerMDB.java | 66 +++++++++++ .../src/main/webapp/WEB-INF/beans.xml | 0 .../src/main/webapp/WEB-INF/web.xml | 22 ++++ pom.xml | 1 + 9 files changed, 406 insertions(+) create mode 100644 examples/seam-jms-example-rest/pom.xml create mode 100644 examples/seam-jms-example-rest/readme.md create mode 100644 examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MappingInterface.java create mode 100644 examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageObserver.java create mode 100644 examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageSendingResource.java create mode 100644 examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/QueueHandlerMDB.java create mode 100644 examples/seam-jms-example-rest/src/main/webapp/WEB-INF/beans.xml create mode 100644 examples/seam-jms-example-rest/src/main/webapp/WEB-INF/web.xml diff --git a/examples/seam-jms-example-rest/pom.xml b/examples/seam-jms-example-rest/pom.xml new file mode 100644 index 0000000..e293364 --- /dev/null +++ b/examples/seam-jms-example-rest/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + org.jboss.seam.jms.examples + seam-jms-rest-publishing + war + 3.0.0-SNAPSHOT + Seam JMS Rest PUblishing Example + + + java.net2 + Repository hosting the jee6 artifacts + http://download.java.net/maven/2 + + + jboss + jboss-public + http://repository.jboss.org/nexus/content/groups/public + + + + + javax + javaee-api + 6.0 + provided + + + + junit + junit + 4.8.1 + test + + + + org.jboss.seam.jms + seam-jms-impl + 3.0.0-SNAPSHOT + + + slf4j-api + org.slf4j + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-war-plugin + 2.1-beta-1 + + false + + + + JMSManagingApp + + + + endorsed + + + sun.boot.class.path + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + + + ${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path} + + + + + javax + javaee-endorsed-api + 6.0 + + + + + + + + + + diff --git a/examples/seam-jms-example-rest/readme.md b/examples/seam-jms-example-rest/readme.md new file mode 100644 index 0000000..68d116f --- /dev/null +++ b/examples/seam-jms-example-rest/readme.md @@ -0,0 +1,22 @@ +#sample-jms-app + +This application is deployed as a web application and shows off the + +##Building +* Execute mvn clean install + +##Deployment +Currently, the deployment has only been tested on JBoss AS 6 + +* You need to the following JMS Topics (in server/*/deploy/hornetq/hornetq-jms.xml) + + + +* Copy the war file to server/*/deploy/ + +* Generate a rest request, e.g. rest-resources/pub/12345 +This publishes a Long type to the JMS Topic to jms/LongT4. +It is observed in an event, published using JMS APIs + +* Generate a rest request, e.g. rest-resources/msg/hello_world! +This publishes via an event, observed in an MDB. diff --git a/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MappingInterface.java b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MappingInterface.java new file mode 100644 index 0000000..dd2e328 --- /dev/null +++ b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MappingInterface.java @@ -0,0 +1,40 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.seam.jms.examples.rest; + +import javax.enterprise.event.Observes; +import javax.jms.Topic; +import org.jboss.seam.jms.annotations.JmsDestination; +import org.jboss.seam.jms.annotations.Routing; +import org.jboss.seam.jms.bridge.RouteType; + +/** + * + * @author johnament + */ +public interface MappingInterface { + @Routing(RouteType.EGRESS) + public void mapLongsToTopic(@Observes String s, @JmsDestination(jndiName="jms/LongT2") Topic t); + + @Routing(RouteType.INGRESS) + public void mapsObjsToTop(@Observes Long l, @JmsDestination(jndiName="jms/LongT4") Topic t); +} diff --git a/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageObserver.java b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageObserver.java new file mode 100644 index 0000000..97078ca --- /dev/null +++ b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageObserver.java @@ -0,0 +1,43 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.seam.jms.examples.rest; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import org.jboss.logging.Logger; + +/** + * + * @author johnament + */ +@ApplicationScoped +public class MessageObserver { + + Logger logger = Logger.getLogger(MessageObserver.class); + public void observeString(@Observes String s) { + logger.info("Just observing a string here. "+s); + } + + public void observeLong(@Observes Long l) { + logger.info("Just observing a Long here. "+l); + } +} diff --git a/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageSendingResource.java b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageSendingResource.java new file mode 100644 index 0000000..a6ee071 --- /dev/null +++ b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/MessageSendingResource.java @@ -0,0 +1,103 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.seam.jms.examples.rest; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.inject.Inject; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.Session; +import javax.jms.Topic; +import javax.jms.TopicPublisher; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import org.jboss.logging.Logger; +import org.jboss.seam.jms.annotations.JmsDestination; +import org.jboss.seam.jms.bridge.RouteBuilder; + +/** + * + * @author johnament + */ +@RequestScoped +@Path("/req") +public class MessageSendingResource { + //@Resource(mappedName="jms/LongT3") Topic t; + //@Inject IngressMessageListener iml; + //@Inject Session session; + @Resource(mappedName="/ConnectionFactory") ConnectionFactory connectionFactory; + @Inject @JmsDestination(jndiName="jms/LongT2") TopicPublisher tp; + @Inject @JmsDestination(jndiName="jms/LongT4") Topic t4; + @Inject RouteBuilder rb; + @Inject Event dataEvent; + @Inject MessageObserver mo; + @Inject Logger logger; + @Inject Connection connection; + + @PostConstruct + public void init() throws Exception{ + //connection.start(); + } + @GET + @Path("/msg/{data}") + @Produces("text/plain") + public String sendData(@PathParam("data") String data) { + return sendObjData(data); + } + + private String sendObjData(String s) { + try{ + dataEvent.fire(s); + } catch (Exception e) { + logger.error("Exception",e); + } + return s; + } + + @GET + @Path("/pub/{data}") + @Produces("text/plain") + public String sendL4(@PathParam("data") Long data) throws Exception { + Connection conn = connectionFactory.createConnection(); + logger.infof("Received a long %s",data); + Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + conn.start(); + session.createProducer(t4).send(session.createObjectMessage(data)); + //session.close(); + conn.close(); + return data.toString(); + } + + + @GET + @Path("/long/{data}") + @Produces("text/plain") + public String sendData(@PathParam("data") Long data) { + String s = data+""; + return sendObjData(s); + } +} diff --git a/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/QueueHandlerMDB.java b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/QueueHandlerMDB.java new file mode 100644 index 0000000..f8950b8 --- /dev/null +++ b/examples/seam-jms-example-rest/src/main/java/org/jboss/seam/jms/examples/rest/QueueHandlerMDB.java @@ -0,0 +1,66 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, Red Hat, Inc., and individual contributors + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.seam.jms.examples.rest; + +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; +import javax.jms.TextMessage; +import org.jboss.logging.Logger; + +/** + * + * @author johnament + */ +@MessageDriven(name="T4_MDB", activationConfig = +{ +@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"), +@ActivationConfigProperty(propertyName="Destination", propertyValue="jms/LongT2") +}) +public class QueueHandlerMDB implements MessageListener{ + + Logger logger = Logger.getLogger(QueueHandlerMDB.class); + @Override + public void onMessage(Message message) { + logger.info("Handling onMessage"); + if(message instanceof TextMessage) { + TextMessage tm = (TextMessage)message; + try{ + logger.info("Received text: "+tm.getText()); + } catch (JMSException e) { + logger.error("JMSException",e); + } + } else if(message instanceof ObjectMessage) { + ObjectMessage om = (ObjectMessage)message; + try{ + Object o = om.getObject(); + logger.info(o.getClass().getCanonicalName()); + logger.info("Object Data: "+o.toString()); + } catch (JMSException e) { + logger.error("JMSException",e); + } + } + } +} diff --git a/examples/seam-jms-example-rest/src/main/webapp/WEB-INF/beans.xml b/examples/seam-jms-example-rest/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..e69de29 diff --git a/examples/seam-jms-example-rest/src/main/webapp/WEB-INF/web.xml b/examples/seam-jms-example-rest/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..83d6433 --- /dev/null +++ b/examples/seam-jms-example-rest/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + + + Resteasy + + org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher + + + + + Resteasy + /* + + + resteasy.scan + true + + diff --git a/pom.xml b/pom.xml index 1b303c5..f01d7fd 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ dist docs/reference + examples/seam-jms-example-rest