我启动了消费者和生产者,但是MDB没有收到消息。
我在另一个应用程序中使用了类似的代码,并且可以正常工作。在此应用程序中,永远不会调用mdb,而且我也不知道这是配置问题(我不知道要更改什么)或代码。
MDB:
public class changeRistMDB implements MessageListener {
@Override
public void onMessage(Message message) {
try {
RistDTO order = message.getBody(RistDTO.class);
System.out.println("Expensive order receivede: " + order);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
消费者:
public class ChangeConsumer {
public static void main (String [] args ) throws NamingException {
Context jndiContext = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactoryRist");
Destination topic = (Destination) jndiContext.lookup("jms/javaee7/TopicRist");
System.out.println("\n Infinite loop. Waiting...");
try (JMSContext jmsContext = connectionFactory.createContext()) {
while(true) {
RistDTO order = jmsContext.createConsumer(topic).receiveBody(RistDTO.class);
System.out.println("Order received: " + order);
}
}
}
}
生产者:
public class ChangeProducer {
public static void main (String [] args ) throws NamingException {
RistDTO order = new RistDTO("1",-200);
Context jndiContext = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/javaee7/ConnectionFactoryRist");
Destination topic = (Destination) jndiContext.lookup("jms/javaee7/TopicRist");
try ( JMSContext jmsContext = connectionFactory.createContext()) {
jmsContext.createProducer().send(topic, order);
}
}
}
生产者和消费者工作,但MDB没有收到。