Skip to content

Commit

Permalink
Added an example project that demonstrates how you can publish REST m…
Browse files Browse the repository at this point in the history
…essages via JMS Events. Demonstrates the Mapping Interfaces.
  • Loading branch information
johnament committed Mar 6, 2011
1 parent 7157f53 commit 98538ca
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 0 deletions.
109 changes: 109 additions & 0 deletions examples/seam-jms-example-rest/pom.xml
@@ -0,0 +1,109 @@

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jboss.seam.jms.examples</groupId>
<artifactId>seam-jms-rest-publishing</artifactId>
<packaging>war</packaging>
<version>3.0.0-SNAPSHOT</version>
<name>Seam JMS Rest PUblishing Example</name>
<repositories>
<repository>
<id>java.net2</id>
<name>Repository hosting the jee6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>jboss</id>
<name>jboss-public</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.seam.jms</groupId>
<artifactId>seam-jms-impl</artifactId>
<version>3.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<finalName>JMSManagingApp</finalName>
</build>
<profiles>
<profile>
<id>endorsed</id>
<activation>
<property>
<name>sun.boot.class.path</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<!-- javaee6 contains upgrades of APIs contained within the JDK itself.
As such these need to be placed on the bootclasspath, rather than classpath of the
compiler.
If you don't make use of these new updated API, you can delete the profile.
On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
<compilerArguments>
<bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>

22 changes: 22 additions & 0 deletions 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)
<topic name="LongT4"><entry name="jms/LongT4"/></topic>
<topic name="LongT2"><entry name="jms/LongT2"/></topic>

* 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.
@@ -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);
}
@@ -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);
}
}
@@ -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<String> 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);
}
}
@@ -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);
}
}
}
}
Empty file.

0 comments on commit 98538ca

Please sign in to comment.