我配置了独立的 hornetq 并启动了它。创建了一个发送者类和一个 MDB 接收者类。当我使用发送者类本身中的方法 messageConsumer.setMessageListener(listener) 向消费者类注册我的发送者类时,它工作正常。但是当我在 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 {
}
}