Cleiton Ribeiro
Cleiton Ribeiro

Reputation: 409

MessageDrivenBean isnt handle messages [Wildfly]

Im trying to develop a "Message Driven Bean" to handle all the local ActiveMQ messages, but it's the first time that i try to do something like this.

The most part of the material that i found explain how to write a MDB using JBOSS server, in this case there's a xml file with some queue information, but in all wildfly tutorials there's no mention to any kind of configuration like that.

I have the following scenario:

  1. A simple java project like message producer

  2. An ActiveMQ instance running local

  3. An EJB project deployed into Wildfly 10

My producer project is able to send messages to ActiveMQ queue, this part its working,but my EJB project just have a single class called TestMDBHandle with @MessageDriven annotation. Is this enough to receive my queue messages? Because the MDB isnt working, i imagine must be a kind of configuration or property in EJB to specify the host of the message-broker.

My message producer:

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.InitialContext;
import javax.naming.NamingException;


public class MessageSender {

    public static void main(String args[]) throws NamingException, JMSException {
        MessageSender sender = new MessageSender();
        sender.sender();
    }

    public void sender() throws NamingException, JMSException {

        InitialContext jndi = null;
        Session session = null;
        Connection connection = null;

        try {
            jndi = new InitialContext();
            ConnectionFactory factory = (ConnectionFactory)jndi.lookup("connectionFactory");
            connection = factory.createConnection();
            connection.start();

            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Destination destination = (Destination)jndi.lookup("MyQueue");

            MessageProducer producer = session.createProducer(destination);

            TextMessage mensagem = session.createTextMessage("Eu enviei uma mensagem!");

            producer.send(mensagem);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
            connection.close();
            jndi.close();
        }

    }
}

My jms properties located inside my producer project

java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=tcp://localhost:61616
connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactory
queue.MyQueue=jms/myqueue

Finally, my ejb project have this single class, without any kind of property file or xml.

package br.com.jms.mdb;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(name = "meuHandler", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/myqueue") })
public class Teste implements MessageListener {

    @Resource
    private MessageDrivenContext mdctx;

    public Teste() {

    }

    @Override
    public void onMessage(Message message) {
        TextMessage objectMessage = null;
        try {
            objectMessage = (TextMessage)message;
            System.out.println("Achei a mensagem : " + objectMessage.getText().toString());
        }catch(JMSException e) {
            e.printStackTrace();
        }
    }

}

Upvotes: 0

Views: 464

Answers (1)

OnlyEM
OnlyEM

Reputation: 1

Maybe you can provide a little more information such as the xml file with the queue information and the annotation properties of the MDB? Because it sounds you are heading in the right direction. The two main things:

You have to specify the exact queue that the MDB is listening to, for example through the properties of the @MessageDriven annotation (such as "name", "mappedName", "activationConfig"). And of course override the onMessage() method to process the messages.

You also have to make sure that this specific queue is available as a resource for your application. You have to provide jms configuration for this, which also defines the resource type (Queue or Topic). From your question I can't tell which of these steps you have (partly) completed.

Upvotes: 0

Related Questions