gpt4 book ai didi

java - 如何实现 ServletContextListener 来监听一个消息队列? (Tomcat, ActiveMQ)

转载 作者:行者123 更新时间:2023-11-28 22:28:36 25 4
gpt4 key购买 nike

我很高兴改进我在 Apache Tomcat 上运行的网络应用程序。添加了一个 ActiveMQ JMS 服务器来发送和接收消息。

我添加了一个 ServletContextListener 来持续收听我的消息。但是,我不确定这是否适合生产。欢迎任何建议。

网络.xml

<listener>
<listener-class>com.test.JMSContextListener</listener-class>
</listener>

听众:

public class JMSContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent arg0) {
JMSConnector.startListening();
}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
//Nothing
}
}

连接:

public class JMSConnector {

static Context context;
static QueueConnectionFactory factory;

static {
try {
context = new InitialContext();
factory = (QueueConnectionFactory) context.lookup("java:comp/env/jms/ConnectionFactory");
} catch (NamingException ex) {
//TODO
}
}

public static void startListening() {
try {
Connection connection = factory.createConnection();
Queue queue = (javax.jms.Queue) context.lookup("java:comp/env/jms/Queue");
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(queue);

//This MessageListener will do stuff with the message
MessageListenerImpl messageListener = new MessageListenerImpl();
consumer.setMessageListener(messageListener);
connection.start();

// Start connection or nothing will happen!!!
connection.start();
} catch (JMSException ex) {
//TODO
} catch (NamingException ex) {
//TODO
}
}
}

这是一种推荐的方法还是应该改进?

热烈感谢所有帮助。谢谢。

最佳答案

我添加了一个带有线程的改进解决方案。

仍然欢迎额外的帮助:-)

网络.xml

<listener>
<listener-class>com.test.JMSContextListener</listener-class>
</listener>

听众:

public class JMSContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent arg0) {
Thread thread = new Thread(new JMSConnector());
thread.start();
}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
//Nothing
}
}

连接:

public class JMSConnector implements Runnable {
public void run() {
try {
Context context = new InitialContext();
QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("java:comp/env/jms/ConnectionFactory");
Connection connection = factory.createConnection();
Queue queue = (javax.jms.Queue) context.lookup("java:comp/env/jms/serverQueue");
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(queue);

//This MessageListener will do stuff with the message
MessageListenerImpl messageListener = new MessageListenerImpl();
consumer.setMessageListener(messageListener);
connection.start();

// Start connection or nothing will happen!!!
connection.start();
} catch (JMSException ex) {
//TODO
} catch (NamingException ex) {
//TODO
}
}
}

这是一种推荐的方法还是应该改进?

热烈感谢所有帮助。谢谢。

关于java - 如何实现 ServletContextListener 来监听一个消息队列? (Tomcat, ActiveMQ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671383/

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