zoukankan      html  css  js  c++  java
  • 配置监听器使项目启动时创建消费者

    1、web.xml中注册监听器
    <listener>
    <listener-class>com.activemq.common.InitComponent</listener-class>
    </listener>
    2、InitComponent实现ServletContextListener,ApplicationContextAware接口,重写contextInitialized(ServletContextEvent servletContextEvent)方法。

    特别注意,如果使用spring管理activemq,要修改配置与实际生产一致。这种方式会在项目启动时创建消费者,若项目做了负载均衡,多个Tomcat启动项目,会造成有多个消费者客户端。

    @Component
    public class InitComponent implements ServletContextListener,ApplicationContextAware{

    private static ApplicationContext applicationContext;
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    InitComponent.applicationContext=applicationContext;
    }
    /**
    * 程序运行时即初始化activemq消费组件
    */
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://132.252.3.22:61616");
    Connection connection;
    Session session;
    Destination destination;
    MessageConsumer messageConsumer;
    try {
    connection = factory.createConnection();
    connection.start();
    session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
    destination = session.createTopic("ZHXJ_QUEUE"); // 创建连接的消息队列
    messageConsumer = session.createConsumer(destination);// 创建消息消费者
    messageConsumer.setMessageListener(new StaffMsgListener());
    } catch (JMSException e) {
    e.printStackTrace();
    }
    }
    }

  • 相关阅读:
    Jabref安装及使用教程
    均方误差与均方根误差
    费马大定理
    JabRef设置
    申请内存时底层发生了什么?
    什么是Pipeline?
    基于三维点云场景的语义及实例分割
    华为 MateView 系列显示器新品
    C#-WinForm跨线程修改UI界面
    C# 代码生成二维码方法及代码示例(QRCoder)
  • 原文地址:https://www.cnblogs.com/xyhero/p/9404595.html
Copyright © 2011-2022 走看看