gpt4 book ai didi

java - 如何在两个 servlet 之间通过 apacheMQ 发送消息?

转载 作者:行者123 更新时间:2023-11-28 23:31:17 26 4
gpt4 key购买 nike

我正在尝试将 apacheMQ 集成到我在 Tomcat 上运行的 Web 应用程序中。我找到了一些关于本地集成的教程(我不打算进行全局集成)但我不确定如何进行。对于初学者来说,所有教程似乎都很困惑。

能否请您看一下我的代码并建议我是否在正确的道路上实现它?我的意图是从一个 servlet 发送一些消息(将其添加到队列中),然后从具有不同 servlet 的队列中读取这些消息。

此代码基于THIS TUTORIAL .

处理消息的类:

public class Messenger {

public static void sendMessage(String msg) {
// configure the broker
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"vm://localhost");

// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();

// Create a Session
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");

// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

// Create a messages
TextMessage message = session.createTextMessage(msg);

// Tell the producer to send the message
System.out.println("Sent message: " + message.getText());
producer.send(message);

// Clean up
session.close();
connection.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void readQueue() {
try {

// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"vm://localhost");

// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();

// Create a Session
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");

// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);

// Wait for a message
Message message = consumer.receive(1000);

if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}

consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}

}

目前,我在控制台中打印所有内容就足够了,因为此示例仅用于测试和调试目的。

发送和接收 servlet:

public class SendMessageServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");

Messenger.sendMessage("Test message");


}

}

public class ReadQueueServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");

Messenger.readQueue();

}

}

访问ReadQueueServlet的结果只是“Received: null”。

我不确定在这种情况下是否应该为我的 Web 应用程序创建一个 context.xml,但我也试过了。我将以下内容放在 WEB-INF/META-INF/context.xml 中:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource auth="Container"
name="jms/ConnectionFactory"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="vm://localhost?brokerConfig=xbean:activemq.xml"
brokerName="MyActiveMQBroker"/>

<Resource auth="Container"
name="jms/FooQueue"
type="org.apache.activemq.command.ActiveMQQueue"
description="JMS queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="FOO.QUEUE"/>
</Context>

感谢任何帮助。

最佳答案

The result of accessing the ReadQueueServlet is just "Received: null".

这是因为队列中没有消息。

        // Wait for a message
Message message = consumer.receive(1000);

if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}

如果您看到 documentation接收方法

Returns: the next message produced for this message consumer, or null if the timeout expires or this message consumer is concurrently closed

所以在时间到期后你得到空值。您的代码进入 else block 并打印为 null 的消息。

关于java - 如何在两个 servlet 之间通过 apacheMQ 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30000033/

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