スタンドアロンの hornetq を設定して起動しました。1 つのセンダー クラスと 1 つの 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 {
}
}