MDB Listener不听hornetq

时间:2013-09-13 04:47:42

标签: jboss hornetq

我配置了独立的hornetq并启动了它。创建了一个发送方类和一个MDB接收方类。 当我使用该方法向消费者类注册我的发件人类时, messageConsumer.setMessageListener(listener)在sender类本身,它工作正常。 但是当我在jboss应用服务器中部署我的MDB接收器(.war文件!)时,它不会监听队列消息。

发件人类:

**package com.mdas.sender;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import com.mdas.receiver.TestQueueListnerMDB;
import com.mdas.vo.JmsVO;
public class JMSSender  {
    static Logger logger = Logger.getLogger(JMSSender.class);
    // *************** Connection Factory JNDI name *************************
    public String connectionFactory;
    // *************** Queue JNDI name *************************
    public  String queueName;
    protected  ConnectionFactory tconFactory;
    protected  Connection tcon;
    protected  Session session;
    protected  MessageProducer producer;
    protected  Queue queue;
    protected MessageConsumer messageConsumer;
    protected TestQueueListnerMDB listener;
    protected InitialContext ic;

    public JMSSender(InitialContext ic, String connectionFactory, String queueName){
        this.ic = ic;
        this.connectionFactory = connectionFactory;
        this.queueName = queueName;

    }
    public void sendJms(JmsVO jmsVO) throws Exception
    {
        System.out.println("Message put in jms destination");
        ObjectMessage objectMessage  =  session.createObjectMessage();
        objectMessage.setObject(jmsVO);
        producer.send(objectMessage);
    }
    public void init() throws NamingException,
    JMSException
    {
        System.out.println("0");
        tconFactory = (ConnectionFactory) ic.lookup(connectionFactory);
        System.out.println("1");
        tcon = tconFactory.createConnection();
        System.out.println("2");
        session = tcon.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        System.out.println("3");
        queue = (Queue) ic.lookup(queueName);
        System.out.println("4");
        producer = session.createProducer(queue);
        System.out.println("5");
        /*messageConsumer = session.createConsumer(queue);
        listener = new TestQueueListnerMDB();
        messageConsumer.setMessageListener(listener);*/
        tcon.start();

    }
    public void closeQueueConnections(){
        System.out.println("<<<< start closeQueueConnections >>>>>");
        try {
            producer.close();
            //messageConsumer.close();
            session.close();
            tcon.close();
            System.out.println("<<<< end closeQueueConnections successfully >>>>>");
        } catch (Exception e) {
            logger.error("Error in closeQueueConnections()", e);
        }
    }
}**

接收者类:

package com.mdas.receiver;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.ResourceAdapter;

import com.mdas.vo.JmsVO;



@MessageDriven(
        activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/TestQueue"),
                @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), 
                @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "100"),
                @ActivationConfigProperty(propertyName = "hostName", propertyValue = "localhost"),
                @ActivationConfigProperty(propertyName = "port", propertyValue = "5455")
                })
       @TransactionManagement(value = TransactionManagementType.CONTAINER)
       @TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED)
       @ResourceAdapter("hornetq-ra.rar")
public class TestQueueListnerMDB implements MessageListener, MessageDrivenBean{

private static final long serialVersionUID = 1L;

    static Logger logger = Logger.getLogger(TestQueueListnerMDB.class);

        public TestQueueListnerMDB() {
            logger.info("Snmp MDB Created :: " + this);
            System.out.println("Snmp MDB Created :: " + this);
        }

         public void onMessage(Message message) {
             try
             {
            System.out.println("Entered in onMessage::: ");
            logger.info("Trap Received In Processor ::: ");

            ObjectMessage objectMessage  =  (ObjectMessage)message;
            JmsVO received = (JmsVO)objectMessage.getObject();
            System.out.println(received.getText());
            message.acknowledge();

             }
             catch (Exception e)
             {
                 logger.error("Error in receiving alarm from queue", e);

             }finally{

             }

        }

        public void ejbRemove() throws EJBException {
            logger.info("QueueListnerMDB is being removed");
        }

        public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) throws EJBException {

        }
}

1 个答案:

答案 0 :(得分:0)

你的问题没有很好地阐述,但让我试试......

为了给你一个正确的答案,我需要更多的信息,你的服务器在哪里(它是远程的),以及你正在使用的版本。

通常,您只需指定远程服务器即可。你有关于HornetQ文档的一些信息:

http://docs.jboss.org/hornetq/2.2.2.Final/user-manual/en/html/appserver-integration.html#d0e8389

如果您向我提供有关正在发生的事情的更多信息(错误,版本),我可能会尝试给您更好的答案。