Paul Luchin
Paul Luchin

Reputation: 63

Wildfly jms configuration

I'm having troubles with migration from old JBoss to Wildfly 14 (or some other latest version).
Currently I'm stuck with JMS configuration.
Here is the configuration:

In my class I have the following fields

private static final String JMS_CONNECTION_FACTORY_JNDI_NAME = "java:/ConnectionFactory";
@Resource(mappedName=JMS_CONNECTION_FACTORY_JNDI_NAME)
ConnectionFactory factory;

and in this class I have the following method:

public void openJmsSession() {
    try {
        connection = factory.createConnection();
        Context jndiContext = getInitialContext();
        queue =(Queue) jndiContext.lookup(JMS_MAIL_QUEUE_NAME);
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    } catch (NamingException e) {
        logger.error("Naming exception during opening JMS session", e);
    } catch (JMSException e) {
        logger.error("JMS exception during opening JMS session", e);
    }
}

Line connection = factory.createConnection(); throws NPE because factory is null.

Upvotes: 1

Views: 4093

Answers (3)

Paul Luchin
Paul Luchin

Reputation: 63

The following approaches worked for me:

  • Solution via EJB
    To do this via EJB you should mark your bean with @Stateless and @LocalBean annotations. After adding this your bean will become a container-managed object and you can rely on it. Also while initial context creation you shouldn't specify any properties. Initial context creation should look like:
    private static Context getInitialContext() throws NamingException { return new InitialContext(); }
  • Solution without EJB or other magic

Add wildfly-ejb-client-bom, wildfly-jms-client-bom, wildfly-naming to your maven dependencies. In your client code use full JNDI names of JMS queues and connection factories. For example:

  1. JMS queue: before queue/MySuperJMSQueue, after: java:/jms/queue/MySuperJMSQueue
  2. JMS connection factory: before ConnectionFactory, after: java:/ConnectionFactory

Change your configuration file (in my case it's standalone-full.xml) by adding new JMS queue via the following line:

<jms-queue name="MySuperJMSQueue" entries="java:/jms/queue/MySuperJMSQueue"/>

Change parameters which have been used for initial context instantiation:

private static Context getInitialContext() throws NamingException {
    Properties props = new Properties();
    props.put( Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
    props.put( Context.URL_PKG_PREFIXES, "org.jboss.as.naming.interfaces:org.jboss.ejb.client.naming");
    return new InitialContext(props);
}

After this manipulations you can do work with JMS. Just the example of opening JMS session:

        Context jndiContext = getInitialContext();
        ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup(JMS_CONNECTION_FACTORY_JNDI_NAME);
        connection = factory.createConnection();
        queue =(Queue) jndiContext.lookup(JMS_QUEUE_NAME);
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

For more details check the doc which is located by the following path: <WildFly 14 home folder>\bin\client\README-EJB-JMS.txt

Upvotes: 0

TacheDeChoco
TacheDeChoco

Reputation: 3913

In case you just need to access a JMS queue, there is probably a shorter way to do it (using the simplified api of JMS 2.0):

Config:

<jms-queue name="esb.inbound.test" entries="java:/jms/queue/esb.inbound.test"/>

Code:

@Resource(mappedName="java:/jms/queue/esb.inbound.test")
private Queue inboundQueue;

Upvotes: 0

TacheDeChoco
TacheDeChoco

Reputation: 3913

Having the following config:

<connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>

I'm using this code:

@Resource(mappedName = "java:/ConnectionFactory")
private static ConnectionFactory connectionFactory;

Hope this helps.

Upvotes: 1

Related Questions