Configuring JMS client libraries (WildFly):

If you are running a Remote JMS Client, all you need in your Maven pom.xml is including the wildfly-jms-client-bom with the correct version of WildFly in it:

<dependencies>
		<dependency>
			<groupId>org.wildfly</groupId>
			<artifactId>wildfly-jms-client-bom</artifactId>
			<version>${version.wildfly}</version>
			<type>pom</type>
		</dependency>
</dependencies>

For example, if running WildFly 10, set as Property:

<properties>
		<version.wildfly>10.0.0.Final</version.wildfly>
</properties>

On the other hand, if you are using InVM local Clients (such as MDBs), you need to include the jboss-jms-api in your project:

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.wildfly.bom</groupId>
			<artifactId>jboss-javaee-7.0-with-all</artifactId>
			<version>8.0.0.Final</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>   
    </dependencies>


</dependencyManagement>

. . . . 
 
<dependencies>	 

	<dependency>
		<groupId>org.jboss.spec.javax.jms</groupId>
		<artifactId>jboss-jms-api_2.0_spec</artifactId>
		<scope>provided</scope>
	</dependency>

</dependencies>

And here are some examples for configuring JMS queues and destinations:

JBoss JMS 1.0 Queue standalone application:

package sample;
import java.util.Properties;
import java.util.Scanner;

import javax.jms.*;

import javax.naming.Context;

public class QueueExample implements MessageListener  
{

    public void example() throws Exception
    {
        String destinationName = "queue/queueA";

        Context ic = null;
        ConnectionFactory cf = null;
        Connection connection =  null;

        try {         
            ic = getInitialContext();

            cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
            Queue queue = (Queue)ic.lookup(destinationName);

            connection = cf.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer publisher = session.createProducer(queue);
            MessageConsumer subscriber = session.createConsumer(queue);

            subscriber.setMessageListener(this);
            connection.start();

            TextMessage message = session.createTextMessage("Hello!");
            publisher.send(message);

            Scanner keyIn = new Scanner(System.in);

            System.out.print("JMS Server listening. Type a Key + CR to exit\n");
            keyIn.next();

        }
        finally
        {         
            if(ic != null)
            {

                try
                { ic.close(); }
                catch(Exception e)
                {
                    throw e;
                }
            }

            closeConnection(connection);
        }
    }
    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);
    }

    private void closeConnection(Connection con)
    {      
        try
        {
            if (con != null)
            {
                con.close();
            }         
        }
        catch(JMSException jmse)
        {
            System.out.println("Could not close connection " + con +" exception was " + jmse);
        }
    }

    protected boolean isQueueExample() {
        return true;
    }

    public static void main(String[] args) throws Exception
    { new QueueExample().example(); }

    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);
    }  
}

JBoss JMS 1.0 Topic standalone application:

package sample;

import java.util.Properties;
import java.util.Scanner;

import javax.jms.*;
import javax.naming.Context;


public class TopicExample implements MessageListener  
{

    public void example() throws Exception {
        String destinationName = "topic/topicA";

        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("Hello!");
            publisher.send(message);

            Scanner keyIn = new Scanner(System.in);

            System.out.print("JMS Server listening. Type a Key + CR to exit\n");
            keyIn.next();

        }
        finally
        {   
            if(ic != null)
            {
                try
                {
                    ic.close();
                }
                catch(Exception e)  {
                    throw e;
                }
            }

            closeConnection(connection);
        }
    }
    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);
    }

    private void closeConnection(Connection con) {      
        try {
            if (con != null) {
                con.close();
            }         
        }
        catch(JMSException jmse)
        {
            System.out.println("Could not close connection " + con +" exception was " + jmse);
        }
    }

    protected boolean isQueueExample()
    {
        return true;
    }

    public static void main(String[] args) throws Exception {
        new TopicExample().example();
    }
    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);
    }  
}

JBoss MDB 3.0 Queue:

package com.sample;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;

@MessageDriven(name = "MessageMDBSample", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/queueA"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

public class DummyMDBean implements MessageListener {

    public void onMessage(Message message) {

        System.out.println("Message received!");

    }

}

Running WildFly ? Continue Reading the following tutorials:

JMS 2.0 tutorial on Wildfly AS

How to code a remote JMS client for WildFly

 

0
0
0
s2smodern

Related articles available on mastertheboss.com

How to configure a Queue in JBoss ?

This article has been moved here: JBoss JMS configuration

How to create a Queue with Jmx Console ?

  Bring up the JMX Console in your browser and look for the sect

JBoss JMS Queue example

The following article shows how to create a simple JMS Queue Prod

JBoss JMS Topic example

The following article shows how to create a simple JMS Topic Publ

JBoss HornetQ simple tutorial

HornetQ is an open source project to build a multi-protocol, embe

How do I configure a Queue/Topic to work in a cluster?

  JBoss AS 5 Just set the Clustered attribute to "true" in your