简体   繁体   中英

Add manually mappedName in Message Driven Bean at runtime

I need to make simple Message Driven Bean that will listen on dynamically added queue locataions. I have tried few ways to implement this, but none of them worked. I have appplication that uses esb and java message queues, and I'm trying to read queue location from config file, during the runtime, and thus tell my message driven bean what is the queue on which to listen. I am not either sure that this is possible.

I also tried to implement message listener, but because I have to use ejb module, and ejb module does not support main method, it requires his own container (like message driven bean), I don't know what to use instead of main method to achive the same goal. I am not able to use session beans because I need to achieve asynchronous communication between client and service.

I also tried to use client application (although it is not one of the options), but maven project does not support debug and run functions for this type of application in netbeans.

Does anyone know any solution for this problem, or at least have some idea?

This may not be the best solution, but it is possible to receive and process JMS messages asynchronously with a Stateful Session Bean doing something like this:

package com.example.statefuljms;

import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Stateful;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

@Stateful
@Local(MessageReceiverLocal.class)
public class MessageReceiver implements MessageReceiverLocal, MessageListener {
    @Resource(mappedName = "ConnectionFactory")
    private ConnectionFactory connectionFactory;

    private QueueConnection connection;

    @Override
    public void start(String queueName) throws JMSException, NamingException {
        Context initialContext = new InitialContext();

        connection = (QueueConnection) connectionFactory.createConnection();
        QueueSession session = (QueueSession) connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);
        Queue queue = (Queue) initialContext.lookup(queueName);
        QueueReceiver receiver = session.createReceiver(queue);
        receiver.setMessageListener(this);
        connection.start();
    }

    @Remove
    @Override
    public void stop() throws JMSException {
        connection.stop();
        connection.close();
    }

    @Override
    public void onMessage(Message message) {
        // handle message here
    }

}

Use a Singleton to test:

package com.example.statefuljms;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.jms.JMSException;
import javax.naming.NamingException;

@Startup
@Singleton
public class Test {
    @EJB
    private MessageReceiverLocal messageReceiver;

    @PostConstruct
    public void run() {
        messageReceiver.start("/queue/myQueue");
    }

    @PreDestroy
    public void cleanup() {
        messageReceiver.stop();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM