JBoss JMS Topic standalone application example
JMS Topic: It’s distribution mechanism to publishing message that has multiple subscriber. Usually in topic there is no guarantee that message will be delivered in order in which it has sent also no guarantee that each message will be processed once. As it has multiple subscriber means topic does not know all the subscribers and destination will be unknown. Once condition is that when topic is publishing message then subscriber (Client) should be active otherwise you will have to make durable subscription and it’s not mandatory to acknowledge messages process by the subscriber/client. In this demo you will see how to create Topic standalone application and JBoss JMS service.
Note: We will be using jboss-6.1.0.Final version for this demo.
- Configure JMS destination on JBoss.
- Go to jboss-6.1.0.Final\server\default\deploy\hornetq\hornetq-jms.xml and below topic:
- Create maven project as below:
- pom.xml:
<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> <groupId>com.javahonk</groupId> <artifactId>JBossJMSTopic</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>JBossJMSTopic</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
- JBossJMSTopicExample.java:
package com.javahonk; import java.util.Properties; import java.util.Scanner; import javax.jms.*; import javax.naming.Context; public class JBossJMSTopicExample implements MessageListener { public static void main(String[] args) throws Exception { new JBossJMSTopicExample().sendJMSMessageOnTopci(); } public static Context getInitialContext() throws javax.naming.NamingException { Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); p.put(Context.URL_PKG_PREFIXES, " org.jboss.naming:org.jnp.interfaces"); p.put(Context.PROVIDER_URL, "jnp://localhost:1099"); return new javax.naming.InitialContext(p); } public void sendJMSMessageOnTopci() throws Exception { String destinationName = "javahonk/topic"; Context ic = null; ConnectionFactory cf = null; Connection connection = null; try { ic = getInitialContext(); cf = (ConnectionFactory) ic.lookup("/ConnectionFactory"); Topic topic = (Topic) ic.lookup(destinationName); connection = cf.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer publisher = session.createProducer(topic); MessageConsumer subscriber = session.createConsumer(topic); subscriber.setMessageListener(this); connection.start(); TextMessage message = session.createTextMessage("JBoss JMS topic example"); publisher.send(message); Scanner scanner = new Scanner(System.in); System.out.print("JMS Server started and listening. Type any Key and enter to exit"); scanner.next(); } finally { if (ic != null) { try { ic.close(); } catch (Exception e) { throw e; } } try { if (connection != null) { connection.close(); } } catch (JMSException jmse) { System.out.println("Could not close connection " + connection + " exception was " + jmse); } } } public synchronized void onMessage(Message message) { TextMessage text = (TextMessage) message; String strMessage = null; try { strMessage = text.getText(); } catch (JMSException e) { e.printStackTrace(); } System.out.println("Message received: " + strMessage); } protected boolean isQueueExample() { return true; } }
- Important: If you are running as standalone application you need below Jars in your class path which you will find in “jboss-6.1.0.Final\common\lib” folder:
- To keep process simple add all JBoss Jars in your class path. Right click project –> Properties –> Add Library
- Now start the JBoss server. Go to jboss-6.1.0.Final\bin –> Double click run.bat to start the server. Once server is started run the queue. Right click JBossJMSQueue.java –> Run As –> Java Application you will see below output on console:
For more information please visit oracle tutorial here