In this article we would show how to create a Topic in JBoss AS7 which uses HornetQ, in this example we would use CLI command to create a Topic as well to deploy an EAR file which would consists of both a message sender as well as an MDB which would work as the consumer to consume the messages by the sender code.
We would be using the standalone mode for this article and in JBoss AS 7.1.0.CR1 version we have a different xml file called standalone-full.xml which has the all the HornetQ related configuration, but it has been said that the same would be integrated in the standalone.xml file in coming JBoss AS 7.1 version.
Before creating a Topic lets create an MDB and a Topic sender program which would be used to send and receive the messages.
Steps to create an MDB & Topic Sender
- Create a folder called MDB-3.
- In “MDB-3” folder create three folders called MDB3.jar, META-INF, TopicSender.war
- In “MDB3.jar” folder create MyMDB.java file and copy the below code in it and compile it as well.
- In “META-INF” folder create application.xml file and copy the below code in it.
- In “TopicSender.war” folder create WEB-INF folder which has classes folder and web.xml inside it and copy the below code in “web.xml”.
- Then In “classes” folder create TopicSenderServlet.java file and copy the below code in it and compile it as well
- Once you have done with all this you now just have to create an EAR file out of this, for that you need to be in the MDB-3 folder and run the below command (make sure you have setted the java/bin in your PATH). Note: you would have to give the (.) dot at last as well
import javax.jms.Message; import javax.jms.TextMessage; import javax.jms.JMSException; import javax.jms.MessageListener; import javax.ejb.MessageDriven; import javax.ejb.ActivationConfigProperty; @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"), @ActivationConfigProperty(propertyName="destination", propertyValue="topic/MyTopic"), //************************* RECONNECT PARAMERTERS ***************************** @ActivationConfigProperty(propertyName="reconnectAttempts", propertyValue="60"), @ActivationConfigProperty(propertyName="reconnectInterval", propertyValue="10") }) public class MyMDB implements MessageListener{ public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("===> MyMDB Received: "+ textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <application version="5" 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/application_5.xsd"> <module> <ejb>MDB3.jar</ejb> </module> <module> <web> <web-uri>TopicSender.war</web-uri> <context-root>TopicSender</context-root> </web> </module> </application>
<web-app> <servlet> <servlet-name>TopicSenderServlet</servlet-name> <servlet-class>TopicSenderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TopicSenderServlet</servlet-name> <url-pattern>/TopicSenderServlet</url-pattern> </servlet-mapping> </web-app>
import java.io.*; import java.util.*; import javax.jms.*; import javax.naming.*; import javax.transaction.*; import javax.servlet.http.*; import javax.rmi.PortableRemoteObject; import javax.servlet.ServletException; public class TopicSenderServlet extends HttpServlet { static PrintWriter out; public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { out=response.getWriter(); InitialContext ic = getInitialContext(); init(ic, TOPIC_NAME); sendMsg(this); } catch(Exception e) { e.printStackTrace(); } } //*************** Connection Factory JNDI name ************************* public final static String CONN_FACTORY="/ConnectionFactory"; //*************** Topic JNDI name ************************* public final static String TOPIC_NAME="topic/MyTopic"; protected TopicConnectionFactory tconFactory; protected TopicConnection tcon; protected TopicSession tsession; protected static TopicPublisher tpublisher; protected Topic topic; protected static TextMessage msg; public void init(Context ctx, String topicName) throws NamingException, JMSException { tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup(CONN_FACTORY),TopicConnectionFactory.class); tcon = tconFactory.createTopicConnection(); tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class); tpublisher = tsession.createPublisher(topic); msg = tsession.createTextMessage(); tcon.start(); } private static void sendMsg(TopicSenderServlet ts) throws IOException, JMSException { boolean readFlag=true; out.println("<HTML>"); out.println("<BODY>"); out.println("<CENTER>"); out.println("<H1>Topic Sender Servlet</H1>"); out.println("Following Messages has been sent !!!<br>"); out.println("===================================="); for(int j=1;j<=3;j++) { msg.setText(""+j); // Messages tpublisher.publish(msg); // Messages sent out.println("<BR>Message Sent = "+j); } out.println("<BR>===================================="); out.println("</CENTER>"); out.println("</BODY>"); out.println("</HTML>"); } private static InitialContext getInitialContext() throws NamingException { return new InitialContext(); } }
MDB-3] jar -cvf MDB-3.ear .
Steps for creating a Topic in JBoss AS 7
- Now we would have to start our JBoss AS 7 with standalone-full.xml using -c parameter during the start-up of JBoss
- Once the JBoss server is up and running properly then you can run the below CLI command with using the a Topic-setup.cli file which would make sure that a new Topic MyTopic is been created and it would also deploy the MDB-3.ear to the server.
bin] ./standalone.sh -c standalone-full.xml
Create a file called Topic-setup.cli in the bin folder of JBoss and copy the below content
connect add-jms-topic --name=MyTopic --entries=topic/MyTopic deploy /home/urs/JMS/MDB-3/MDB-3.ear
Run the below command
bin] ./jboss-admin.sh --file=Topic-setup.cli
Testing
Once you can successfully created a Topic and deployed the application using the above CLI command now its time for testing our newly created Topic
- Open a browser and hit the below URL (I am supposing that you are running JBoss on default port and IP) and that would send 3 messages to the Topic which can be seen on the browser itself
- After hitting the above URL you would see the below messages been received by the MDB on the running JBoss server prompt
http://localhost:8080/TopicSender/TopicSenderServlet
14:32:48,295 INFO [stdout] (Thread-1 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 1 14:32:48,295 INFO [stdout] (Thread-0 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 2 14:32:48,296 INFO [stdout] (Thread-2 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 3
November 26th, 2013 on 10:59 pm
Hi,
I am currently in the phase of migrating my Clustered Application from Jboss eap 5.1 to Jboss eap 6.1 (Jboss AS 7.1). The jboss eap 6.1 uses HornetQ as its JMS system as compared to Jboss eap 5.1 which used JBoss MQ as JMS system. My application has the requirement of using global and local JMS topics(distributed and non-distributed topics). On jboss eap 5.1, there is a configuration file in the messaging folder called ‘xxx-destination-service.xml’ which provided a boolean attribute called ‘Clustered’ defined for each of the JMS topics. This xml file and the attribute is no longer supported in jboss eap 6.1. Can anyone provide me pointers on how this can be done in jboss eap 6.1.
I am Clustering my application in Domain Mode on Jboss eap 6.1 . So please tell what configuration chnages need to be done in my domain.xml or any other xml file to achieve this.
TIA for your help.
Ajit