gpt4 book ai didi

java - JMS如何使用JMS MessageListener获取多条消息

转载 作者:行者123 更新时间:2023-12-01 10:44:56 29 4
gpt4 key购买 nike

我正在尝试从客户端向消费者发送消息。消费者期望许多消息类型,例如预订或确认。因此,当我收到消息时,我试图查看该消息是否是预订或确认的实例。到目前为止, 的实例不起作用,我看不到该对象是预订还是确认。此外,我的 JMSMessageListener 似乎不是执行该场景的最佳方法。场景是:客户端向代理发送预订,代理将消息转发给不同的消费者,代理从消费者获取确认并发回确认。任何想法为什么实例不起作用以及对于这种情况,这是为多个目的的代理实现 JMSMessageListener 的好方法吗?

//JMS Message Listener
public class JMSAgentMessageListener implements MessageListener {

private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination BookingQueue = null;
private Destination consolidatorQueue1 = null;
private Destination consolidatorQueue2 = null;
private MessageConsumer consumer = null;
private Destination agentConfirmQueue = null;
private static MessageProducer producer = null;
final static Logger logger = Logger.getLogger(TravelAgent.class);
private Destination customerConfirmQueue = null;

@Override
public void onMessage(Message message) {
try {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "tcp://localhost:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

if (message instanceof Booking) {

Booking booking = (Booking) message;
logger.info("# Received order for " + booking.getCustomer());
customerConfirmQueue = message.getJMSReplyTo();

logger.info("# The travel agent forwards booking orders to the airfare consolidators");
ObjectMessage messageToConsilator1 = session . createObjectMessage ( booking ) ;
agentConfirmQueue = session.createQueue("AgentConfirmQueue");
consolidatorQueue1 = session.createQueue("ConsolidatorQueue1");
messageToConsilator1.setJMSReplyTo(agentConfirmQueue);
messageToConsilator1.setJMSDestination(consolidatorQueue1);

producer = session.createProducer(consolidatorQueue1);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(messageToConsilator1);

}else if(message instanceof Confirmation){
logger.info("# The travel agent recieved booking orders to the airfare consolidators");
Confirmation confirmation = (Confirmation) message;
logger.info(confirmation.getMessage()+"received");
logger.info("# The travel agent notfiying the customers");

ObjectMessage messageToClient = session . createObjectMessage ( confirmation ) ;
customerConfirmQueue = session.createQueue("customerConfirmQueue");

producer = session.createProducer(customerConfirmQueue);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(messageToClient);
}
producer.close();
session.close();
connection.close();

} catch (JMSException e) {
logger.error("couldnt get the message");
}


}
}

最佳答案

Message 不在类继承树的层次结构中。 JMS提供了可以传播的不同类型的消息,文本、对象等,它们都继承自Message。您可以将这些类型称为 on 的实例。一旦 Message 被转换为 ObjectMessage,您就可以调用 getObject() 并根据您的类型进行转换/检查 instanceof

我建议限制可以在某个主题上发送的消息类型,因为它会导致一些更困惑的代码。如果可能的话,我尝试通过以不必要的方式重构代码来避免使用 instanceof 。为了减少对 instanceof 的需求,我会为每种要传达的信息类型创建一个主题,或者以从同一父级继承或实现相同接口(interface)的方式开发您的类。

关于java - JMS如何使用JMS MessageListener获取多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242853/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com