This example will take you through step by step on practical implementation how you can configure a JMS Client using JBoss Application Server 7.1.1. I won’t discuss theoretical part here but if you need to know JMS related various keywords then you can go through the tutorial at https://www.jeejava.com/configure-jms-client-using-glassfish-3/

Publish/Subscribe Messaging System

  • In a publish/subscribe (pub/sub) messaging system, clients address messages to a topic.
  • Each message may have multiple consumers.
  • Publishers and subscribers have a timing dependency. A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.


Prerequisites

JBoss Application Server 7.1.1
JDK 1.6
Eclipse
Jar – jboss-client.jar (located at <JBOSS_HOME>\bin\client)

We will see now step by step implementation of JMS client

Step 1. Run the JBoss application server using the below command from command prompt. Open command prompt, navigate to <JBOSS_HOME>\bin and execute the below command. The below command will run the application server. You can stop the server by pressing “Ctrl+C” and then type “Terminate batch job (Y/N)? y”

domain.bat

 

Once the server is up and running you will see snippets of output at the bottom similar to the below

[org.jboss.as.deployment.connector] (MSC service thread 1-8) JBAS010401: Bound JCA ConnectionFactory [java:/JmsXA]
[org.jboss.as.messaging] (MSC service thread 1-1) JBAS011601: Bound messaging object to jndi name java:/topic/test
[org.jboss.as.messaging] (MSC service thread 1-1) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/topic/test
[org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-3) JBoss Web Services - Stack CXF Server 4.0.2.GA
[org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-7) JBoss Web Services - Stack CXF Server 4.0.2.GA
[org.jboss.as.remoting] (MSC service thread 1-3) JBAS017100: Listening on 127.0.0.1/127.0.0.1:4597
[org.jboss.as.remoting] (MSC service thread 1-5) JBAS017100: Listening on 127.0.0.1/127.0.0.1:4447
[org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 11014ms - Started 164 of 240 services (75 services are passive or on-demand)
[org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 11216ms - Started 164 of 240 services (75 services are passive or on-demand)

 

Step 2. Now we need to create a user for authentication or running the application. So we will create a guest user. So open command prompt, navigate to <JBOSS_HOME>\bin and execute the below command.

add-user.bat

 

Enter the below inputs as shown in the screen-shot. You can give any username and password as per your choice. I have given here usernameuser and passwordkolkata

JMS Client using JBoss 7

Step 3. Now create a java project in Eclipse or any Java based IDE. Configure Eclipse using jboss-client.jar (located at <JBOSS_HOME>\bin\client) which is required for JMS application.

Step 4. We will create Message Publisher which will publish messages to the topic for Subscribers who will receive those messages from the topic.

package in.webtuts.jms.topic;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MsgPublisher {

    ConnectionFactory connectionFactory;
    Connection connection;
    Session session;
    Destination destination;
    MessageProducer messageProducer;

    public MsgPublisher() throws JMSException, NamingException {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.jboss.naming.remote.client.InitialContextFactory");
        properties.setProperty(Context.PROVIDER_URL, "remote://localhost:4447");
        properties.put("jboss.naming.client.ejb.context", true);
        properties.put(Context.SECURITY_PRINCIPAL, "user");
        properties.put(Context.SECURITY_CREDENTIALS, "kolkata");
        InitialContext initialContext = new InitialContext(properties);
        this.connectionFactory = (ConnectionFactory) initialContext
                .lookup("jms/RemoteConnectionFactory");
        this.destination = (Destination) initialContext
                .lookup("jms/topic/test");
        this.connection = connectionFactory.createConnection("user", "kolkata");
        connection.start();
        this.session = connection
                .createSession(false, Session.AUTO_ACKNOWLEDGE);
        this.messageProducer = session.createProducer(destination);
    }

    private void publishMessage(String msg) throws JMSException {
        TextMessage textMessage = session.createTextMessage(msg);
        messageProducer.send(textMessage);
    }

    private void closeConnection() throws JMSException {
        connection.close();
    }

    /**
     * @param args
     * @throws NamingException
     * @throws JMSException
     */
    public static void main(String[] args) throws JMSException, NamingException {
        MsgPublisher msgPublisher = new MsgPublisher();
        msgPublisher.publishMessage("Publishing Message.");
        msgPublisher.closeConnection();
    }

}

 

Step 5. We will create Message Subscribers which will receive message from the topic.

package in.webtuts.jms.topic;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MsgSubscriberOne {

ConnectionFactory connectionFactory;
Connection connection;
Session session;
Destination destination;
MessageConsumer messageConsumer;

public MsgSubscriberOne() throws NamingException, JMSException {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
properties.setProperty(Context.PROVIDER_URL, "remote://localhost:4447");
properties.put("jboss.naming.client.ejb.context", true);
properties.put(Context.SECURITY_PRINCIPAL, "user");
properties.put(Context.SECURITY_CREDENTIALS, "kolkata");
InitialContext initialContext = new InitialContext(properties);
this.connectionFactory = (ConnectionFactory) initialContext
.lookup("jms/RemoteConnectionFactory");
this.destination = (Destination) initialContext
.lookup("jms/topic/test");
this.connection = connectionFactory.createConnection("user", "kolkata");
connection.start();
this.session = connection
.createSession(false, Session.AUTO_ACKNOWLEDGE);
this.messageConsumer = session.createConsumer(destination);
}

private String receiveMessage() throws JMSException {
TextMessage textMessage = (TextMessage) messageConsumer.receive();
return textMessage.getText();
}

private void closeConnection() throws JMSException {
connection.close();
}

/**
* @param args
* @throws JMSException
* @throws NamingException
*/
public static void main(String[] args) throws NamingException, JMSException {
MsgSubscriberOne msgSubscriber = new MsgSubscriberOne();
String msg = msgSubscriber.receiveMessage();
System.out.println("SubscriberOne");
System.out.println("-------------");
System.out.println(msg);
msgSubscriber.closeConnection();
}

}
package in.webtuts.jms.topic;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MsgSubscriberTwo {

ConnectionFactory connectionFactory;
Connection connection;
Session session;
Destination destination;
MessageConsumer messageConsumer;

public MsgSubscriberTwo() throws NamingException, JMSException {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
properties.setProperty(Context.PROVIDER_URL, "remote://localhost:4447");
properties.put("jboss.naming.client.ejb.context", true);
properties.put(Context.SECURITY_PRINCIPAL, "user");
properties.put(Context.SECURITY_CREDENTIALS, "kolkata");
InitialContext initialContext = new InitialContext(properties);
this.connectionFactory = (ConnectionFactory) initialContext
.lookup("jms/RemoteConnectionFactory");
this.destination = (Destination) initialContext
.lookup("jms/topic/test");
this.connection = connectionFactory.createConnection("user", "kolkata");
connection.start();
this.session = connection
.createSession(false, Session.AUTO_ACKNOWLEDGE);
this.messageConsumer = session.createConsumer(destination);
}

private String receiveMessage() throws JMSException {
TextMessage textMessage = (TextMessage) messageConsumer.receive();
return textMessage.getText();
}

private void closeConnection() throws JMSException {
connection.close();
}

/**
* @param args
* @throws JMSException
* @throws NamingException
*/
public static void main(String[] args) throws NamingException, JMSException {
MsgSubscriberTwo msgSubscriber = new MsgSubscriberTwo();
String msg = msgSubscriber.receiveMessage();
System.out.println("SubscriberTwo");
System.out.println("-------------");
System.out.println(msg);
msgSubscriber.closeConnection();
}

}

 

Step 6. Now how did we get those ConnectionFactory and Destination(Topic) names in the above java codes. Navigate to the location <JBOSS_HOME>\standalone\configuration and open the xml file standalone-full.xml and look for tags <jms-connection-factories> and <jms-destinations>. You can also create and use your own ConnectionFactory or Destination. Also note that I have written “jms/RemoteConnectionFactory” instead of “RemoteConnectionFactory” and “jms/topic/test” instead of “topic/test” otherwise it won’t work.

Excert is given below from standalone-full.xml

<jms-connection-factories>
<connection-factory name="InVmConnectionFactory">
<connectors>
<connector-ref connector-name="in-vm"/>
</connectors>
<entries>
<entry name="java:/ConnectionFactory"/>
</entries>
</connection-factory>
<connection-factory name="RemoteConnectionFactory">
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="RemoteConnectionFactory"/>
<entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
</entries>
</connection-factory>
<pooled-connection-factory name="hornetq-ra">
<transaction mode="xa"/>
<connectors>
<connector-ref connector-name="in-vm"/>
</connectors>
<entries>
<entry name="java:/JmsXA"/>
</entries>
</pooled-connection-factory>
</jms-connection-factories>
<jms-destinations>
<jms-queue name="testQueue">
<entry name="queue/test"/>
<entry name="java:jboss/exported/jms/queue/test"/>
</jms-queue>
<jms-topic name="testTopic">
<entry name="topic/test"/>
<entry name="java:jboss/exported/jms/topic/test"/>
</jms-topic>
</jms-destinations>

 

Step 7. Now run MsgSubscriberOne.java and MsgSubscriberTwo.java then run MsgPublisher.java in order to get the output in the Subscriber’s console. You won’t get any output at MsgPublisher.java console but you will see below output at MsgSubscriberOne.java and MsgSubscriberTwo.java consoles

SubscriberOne
-------------
Publishing Message.
SubscriberTwo
-------------
Publishing Message.

 

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

0 thoughts on “JMS Client using JBoss 7 – Publish/Subscribe Messaging

Leave a Reply

Your email address will not be published. Required fields are marked *