Injected JMS ConnectionFactory is null in simple JMS example using ActiveMQ Artemis with OpenLiberty

151 views Asked by At

I am trying to create a simple JMS example application using ActiveMQ Artemis but I am having issues with my Connection Factory that returns null. Below I am giving you my code, so far, as well as the error stack trace.

I have searched across many questions but didn't managed to fix my issue. I think that I am doing something wrong either with my server.xml or my Configuration class or both them.

Any suggestions as to what I am missing?

server.xml

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

    <featureManager>
        <feature>jakartaee-9.1</feature>
    </featureManager>

    <jmsConnectionFactory jndiName="jms/ArtemisConnectionFactory">
        <properties.activemq
                brokerURL="tcp://localhost:61616"
                userName="admin"
                password="admin"/>
    </jmsConnectionFactory>


    <jmsQueue id="jms/ArtemisQueue" jndiName="jms/ArtemisQueue">
        <properties.activemq queueName="Test_Queue"/>
    </jmsQueue>

    <basicRegistry id="basic" realm="BasicRealm"/>

    <httpEndpoint id="defaultHttpEndpoint"
                  httpPort="9080"
                  httpsPort="9443" />

    <applicationManager autoExpand="true"/>

    <ssl id="defaultSSLConfig" trustDefaultCerts="true" />
</server>

JMS Configuration Class

package com.demo.rest;

import jakarta.annotation.Resource;
import jakarta.enterprise.context.ApplicationScoped;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;

@ApplicationScoped
public class JMSConfig {

    @Resource(lookup = "jms/ArtemisConnectionFactory")
    private ConnectionFactory connectionFactory;

    @Resource(lookup = "jms/ArtemisQueue")
    private Queue queue;

    public ConnectionFactory getConnectionFactory() {
        return connectionFactory;
    }

    public Queue getQueue() {
        return queue;
    }
}

Message Producer

package com.demo.rest;

import jakarta.enterprise.context.ApplicationScoped;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import jakarta.inject.Inject;

@ApplicationScoped
public class MessageProducer {

    @Inject
    private JMSConfig jmsConfig;

    public MessageProducer() {
    }

    public void sendMessage(String text) throws JMSException {
        try (Connection connection = jmsConfig.getConnectionFactory().createConnection();
             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
             javax.jms.MessageProducer producer = session.createProducer(jmsConfig.getQueue())) {

            TextMessage message = session.createTextMessage(text);
            producer.send(message);
        }
    }
}

Message Consumer

package com.demo.rest;

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

import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.util.logging.Level;
import java.util.logging.Logger;

@MessageDriven(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/ArtemisQueue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
public class MessageConsumerMDB implements MessageListener {

    private static final Logger LOGGER = Logger.getLogger(MessageConsumerMDB.class.getName());

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                String content = ((TextMessage) message).getText();
                LOGGER.log(Level.INFO, "Received message: " + content);
            } catch (JMSException e) {
                LOGGER.log(Level.SEVERE, "Error reading message", e);
            }
        } else {
            LOGGER.log(Level.WARNING, "Received non-text message");
        }
    }
}
[ERROR   ] CWOWB1000E: A CDI error has occurred: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jms/ArtemisConnectionFactory reference.  The exception message was: CWNEN1004E: The server was unable to find the jms/ArtemisConnectionFactory default binding with the javax.jms.ConnectionFactory type for the java:comp/env/jms/ArtemisConnectionFactory reference.
[ERROR   ] CWOWB1000E: A CDI error has occurred: CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/jms/ArtemisConnectionFactory reference.  The exception message was: CWNEN1004E: The server was unable to find the jms/ArtemisConnectionFactory default binding with the javax.jms.ConnectionFactory type for the java:comp/env/jms/ArtemisConnectionFactory reference.
[ERROR   ] SRVE0777E: Exception thrown by application class 'org.jboss.resteasy.core.ExceptionHandler.handleApplicationException:105'
org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException: Cannot invoke "javax.jms.ConnectionFactory.createConnection()" because the return value of "com.demo.rest.JMSConfig.getConnectionFactory()" is null
    at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:105)
    at [internal classes]
0

There are 0 answers