JMS MessageListener not working on Liberty

1k Views Asked by At

I working on Liberty 18.0.0.2 with JavaEE 8 full profile .
I configured JMS 2 on server.xml with this content :

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
  <featureManager>
    <feature>javaee-8.0</feature>
    <feature>localConnector-1.0</feature>
    <feature>wasJmsServer-1.0</feature>
    <feature>wasJmsClient-2.0</feature>
  </featureManager>
  <basicRegistry id="basic" realm="BasicRealm" />
  <httpSession securityIntegrationEnabled="false" />
  <httpEndpoint id="defaultHttpEndpoint" httpPort="8080" httpsPort="9443" />
  <applicationManager autoExpand="true" />
  <applicationMonitor updateTrigger="mbean" />
  <messagingEngine>
    <queue id="simpleQueue" />
  </messagingEngine>
  <jmsActivationSpec id="jms/simpleQueue">
    <properties.wasJms destinationRef="java:app/simpleQueue" />
  </jmsActivationSpec>
</server>     

now I wrote simple codes for test JMS on Liberty Application server :

@Stateless
public class MessageSender {

    @Inject
    private JMSContext context;

    @Resource(lookup = "java:app/simpleQueue")
    private Queue queue;

    public void sendMessage(String message) {
        context.createProducer().send(queue, "hello");
    }
}

@Path("/messenger")
public class Messenger {

    @Inject
    private MessageSender messageSender;

    @Path("/send")
    @GET
    public Response send() {
        messageSender.sendMessage("Hello Mahdi");
        return Response.ok("ok").build();
    }
}

@MessageDriven(
        name = "simpleQueue",
        mappedName = "java:app/simpleQueue",
        activationConfig = {
                @ActivationConfigProperty(propertyName = "destinationType",
                        propertyValue = "javax.jms.Queue"),
                @ActivationConfigProperty(propertyName = "destination",
                        propertyValue = "app/simpleQueue")
        })
public class MessageReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {
        System.out.println(message);
    }
}   

Can you explain me why MessageListener not work ?
What's mistake ?
I search in google and found some examples , but can not understand how can fix this problem ! .

1

There are 1 best solutions below

5
On BEST ANSWER

You didn't provide any error messages, so more or less guessing here

You are missing queue definition:

<jmsQueue jndiName="java:app/simpleQueue" id="simpleQueueJms">
    <properties.wasJms queueName="simpleQueue"/>
</jmsQueue>

Your MDB should be defined like this:

@MessageDriven(name="SimpleMDB")
public class MessageReceiver implements MessageListener 

And active spec like this:

<jmsActivationSpec id="yourAppName/SimpleMDB">
    <properties.wasJms destinationRef="simpleQueueJms" destinationType="javax.jms.Queue"/>
</jmsActivationSpec>

Update by request in comments.

Application name depends on your application structure and deployment descriptors. You may find details here - Deploying message-driven beans, but I'm quoting relevant part for reference:

The ID value must be in the format of application name/module name/bean name where:

  • application name is the name of the application that is deployed (for example, JMSSample). The application name applies only if the bean is packaged within an EAR file. The application defaults to the base name of the EAR file with no file name extension unless specified by the application.xml deployment descriptor.
  • module name is the name of the module in which the bean is packaged. In a stand-alone ejb-jar file or WAR file, the defaults to the base name of the module with any file name extension removed. In an EAR file, the defaults to the path name of the module with any file name extension removed, but with any directory names included. The default can be overridden by using the module-name element of ejb-jar.xml (for ejb-jar files) or web.xml (for WAR files).

  • bean name is the ejb-name of the enterprise bean. For enterprise beans defined through annotation, the bean name defaults to the unqualified name of the session bean class, unless specified in the contents of the name() attribute of the MessageDriven annotation. For enterprise beans defined through ejb-jar.xml, it is specified in the deployment descriptor element.