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/

In point-to-point messaging system there are only one sender(sends message) and one receiver(receives message sent by sender). In this model we have to use queue for sender to send the message and later receiver will receive the message from that queue.

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 Sender which will sends message to the queue.

package in.webtuts.jms.queue;

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 MsgSender {

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

    public MsgSender() 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/queue/test");
        this.connection = connectionFactory.createConnection("user", "kolkata");
        connection.start();
        this.session = connection
                .createSession(false, Session.AUTO_ACKNOWLEDGE);
        this.messageProducer = session.createProducer(destination);
    }

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

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

    /**
     * @param args
     * @throws JMSException
     * @throws NamingException
     */
    public static void main(String[] args) throws JMSException, NamingException {
        MsgSender msgSender = new MsgSender();
        msgSender.sendMessage("Sending Message.");
        msgSender.closeConnection();
    }

}

 

Step 5. We will create a Message Receiver which will receive message from the queue.

package in.webtuts.jms.queue;

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 MsgReceiver {

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

    public MsgReceiver() 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/queue/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 {
        MsgReceiver msgReceiver = new MsgReceiver();
        String msg = msgReceiver.receiveMessage();
        System.out.println("MsgReceiver");
        System.out.println("-----------");
        System.out.println(msg);
        msgReceiver.closeConnection();
    }

}

 

Step 5. Now how did we get those ConnectionFactory and Destination(Queue) 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/queue/test” instead of “queue/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 6. Now run MsgSender.java or MsgReceiver.java in any order and look at the console output. You won’t get any output at MsgSender.java console but you will see below output at MsgReceiver.java console

MsgReceiver
-----------
Sending 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

Leave a Reply

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