gpt4 book ai didi

java - 如何在企业java应用程序中发送jms回复

转载 作者:行者123 更新时间:2023-12-01 04:19:36 25 4
gpt4 key购买 nike

大家好,我必须开发一个应用程序,我必须在两个企业应用程序之间建立一座桥梁,并且我正在使用 JMS 来建立这两个应用程序之间的通信。在这里,我使用消息驱动 Bean 将 JMS 消息从一个应用程序发送到另一个应用程序。当我的一个应用程序可以将消息发送给另一个应用程序但无法将回复发送回请求者应用程序时,我感到震惊。

private void sendJMSMessageToMyTestQueue(Message messageData) {
try {
context.createProducer().send(myTestQueue, messageData);
System.out.println("\tTime: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + messageData.getJMSMessageID());
System.out.println("\tCorrel. ID: " + messageData.getJMSCorrelationID());
System.out.println("\tReply to: " + messageData.getJMSReplyTo());
//System.out.println("\tContents: " + messageData.getText());
} catch (JMSException ex) {
Logger.getLogger(HomeLoanJMS.class.getName()).log(Level.SEVERE, null, ex);
}
}



private void recieveSync() {

Message msg = context.createConsumer(myTestQueue).receive();
if (msg instanceof TextMessage) {
try {
TextMessage tms = (TextMessage) msg;
System.out.println("Received reply ");
System.out.println("Received reply ");
System.out.println("\tTime: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + tms.getJMSMessageID());
System.out.println("\tCorrel. ID: " + tms.getJMSCorrelationID());
System.out.println("\tReply to: " + tms.getJMSReplyTo());
System.out.println("\tContents: " + tms.getText());
System.out.println("Coming Reply: ---->>>>" + tms.getText());

} catch (JMSException ex) {
Logger.getLogger(HomeLoanJMS.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

@Override
public void sendMessage(String message) {
try {
TextMessage msg = context.createTextMessage(message);
// msg.setJMSType("Textmsg");
msg.setText(message);
msg.setJMSReplyTo(myTestQueue); //setting reply destination
sendJMSMessageToMyTestQueue(msg);
System.out.println("after reply");
// recieveSync();
} catch (JMSException ex) {
Logger.getLogger(HomeLoanJMS.class.getName()).log(Level.SEVERE, null, ex);
}
}

但是当回复者收到此消息时,我确实打印了 getReplyTo 目的地,但是当我构建回复并将其发回时,它将目标值设置为 null 并且回复永远不会执行。回复者代码如下

@MessageDriven(activationConfig = { 
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
, @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/myTestQueue")
})
public class TestingMDB implements MessageListener {
@EJB
private SavingBeanRemote savingBean;
@Resource(mappedName = "jms/myTestQueue")
private Queue myTestQueue;
@Inject
@JMSConnectionFactory("jms/myTestQueueConnectionFactory")
private JMSContext context;

public TestingMDB() {
}

@Override
public void onMessage(Message message) {
try {
if (message instanceof MapMessage) {
MapMessage mmsg = (MapMessage) message;
String id = mmsg.getString("messageType");
System.out.println(id);
String cid = mmsg.getString("C_ID");
System.out.println(cid);

} else if (message instanceof TextMessage && (message.getJMSReplyTo() != null)) {
TextMessage tm = (TextMessage) message;
System.out.println("Received request");
System.out.println("\tTime: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + tm.getJMSMessageID());
System.out.println("\tCorrel. ID: " + tm.getJMSCorrelationID());
System.out.println("\tReply to: " + tm.getJMSReplyTo());
System.out.println("\tContents: " + tm.getText());
String contents = tm.getText();

Double balance = savingBean.getBalance("c0566664", "a4016692");
String text = (String)balance.toString();
Destination replyDestination = (Destination)message.getJMSReplyTo();

TextMessage replyMessage = context.createTextMessage();
replyMessage.setText(text);
replyMessage.setJMSCorrelationID(tm.getJMSMessageID());
context.createProducer().send(replyDestination, replyMessage);
System.out.println("Sent reply");
System.out.println("\tTime: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + replyMessage.getJMSMessageID());
System.out.println("\tCorrel. ID: " + replyMessage.getJMSCorrelationID());
System.out.println("\tReply to: " + replyMessage.getJMSReplyTo());
System.out.println("\tContents: " + replyMessage.getText());
TextMessage tms = context.createTextMessage("Reply from Client");

for (int i = 0; i < 2; i++) {
try {
try {
System.out.println(message.equals(tm));

Thread.sleep(1000);
System.out.println(tm.getText());
System.out.println(tm.getText());

} catch (InterruptedException ex) {
Logger.getLogger(TestingMDB.class.getName()).log(Level.SEVERE, null, ex);
}

} catch (JMSException ex) {
Logger.getLogger(TestingMDB.class.getName()).log(Level.SEVERE, null, ex);
}

}

} else {
System.out.println("error");
}
} catch (JMSException ex) {
Logger.getLogger(TestingMDB.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

所以我可以发送消息但无法收到回复,这是怎么回事?请帮助我我错过了什么或做错了什么。谢谢

最佳答案

看来您对请求消息和回复消息使用了相同的队列 (myTestQueue)。这很糟糕,因为您的接收者可能正在接收回复消息,而不是发送者/请求者。

一般来说,在使用 JMS 进行请求/回复时,使用 javax.jms.QueueRequestor 应该比通过发送请求消息然后尝试接收请求消息进行“手动”实现更好。

关于java - 如何在企业java应用程序中发送jms回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19076209/

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