Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ActiveMQ和Spring的JMS独立使用者_Java_Spring_Tomcat_Activemq - Fatal编程技术网

Java 使用ActiveMQ和Spring的JMS独立使用者

Java 使用ActiveMQ和Spring的JMS独立使用者,java,spring,tomcat,activemq,Java,Spring,Tomcat,Activemq,过去,我将JMS消费者应用程序部署为在Tomcat(Windows box)下部署的SpringWebapps。然后,这些消费者将在同一Tomcat实例下与我的其他Web应用程序一起运行。然而,随着我使用的消费者数量的增加,我意识到这正变成一场维护噩梦 我的解决方案是将这些webapps转换为作为jar部署的“main-method”独立应用程序。事实上,我能够成功地将它们打包在一起,以尽可能多地重用资源(DAO、依赖项等) 以下是我的主要方法: public static void main(

过去,我将JMS消费者应用程序部署为在Tomcat(Windows box)下部署的SpringWebapps。然后,这些消费者将在同一Tomcat实例下与我的其他Web应用程序一起运行。然而,随着我使用的消费者数量的增加,我意识到这正变成一场维护噩梦

我的解决方案是将这些webapps转换为作为jar部署的“main-method”独立应用程序。事实上,我能够成功地将它们打包在一起,以尽可能多地重用资源(DAO、依赖项等)

以下是我的主要方法:

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    try {
        FooListener fooListener = (FooListener) context.getBean("fooListener");

        fooListener.start();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    try {
        BarListener barListener = (BarListener) context.getBean("barListener");

        barListener.start();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
我的问题:

  • 我是否需要做一些特殊的事情来关闭我的主方法应用程序中的JMS连接,或者它们会在应用程序终止时正常关闭
  • 对于是否使用tomcat或作为独立应用程序部署jms使用者,是否有人有任何个人偏好或其他方面的偏好
  • 编辑:

    更多信息:傻瓜监听器和BarListener扩展了以下抽象类。它们从applicationContext.xml文件中相应的bean继承它们的值,并且它们都重写onMessage()方法以异步使用消息

    public abstract class TextMessageListener implements MessageListener {
    
        protected ConnectionFactory connectionFactory;
    
        protected String queueName;
    
        protected String selectors;
    
        public void start() throws JMSException {
            Connection connection = connectionFactory.createConnection();
    
            Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    
            MessageConsumer consumer = session.createConsumer(session.createQueue(queueName), selectors);
    
            consumer.setMessageListener(this);
    
            connection.start();
        }
    
        public abstract void onMessage(Message message);
    }
    

    您必须通过以下方式为应用程序上下文注册关闭挂钩:

    context.registerShutdownHook();
    
    这将确保jvm关闭时,上下文也正常关闭

    我个人的偏好是总是将其部署到容器中——即使是像这样的独立应用程序,这是因为以下原因:

    • 管理更简单—部署/取消部署应用程序war,启动/停止容器运行时
    • 如果您需要构建任何类型的监控—有多少消息被使用,使用此设置将更容易,您只需添加显示相关信息的网页,而不是使用独立应用程序的jmx

    另外,为什么要显式调用listener.start(),Spring容器会自动执行此操作?

    这是一个非常好的问题-我不知道Spring容器会自动调用listener上的start()方法。在搜索这方面的文档时,我无意中在JmsTemplate上发现了一个博客,这可能会让我的生活更轻松。。。仅供参考-我可以通过在Springbeans中添加init method=“start”来删除手动listener.start()调用:哦,好的,我明白你关于start方法的观点。实际上,甚至这也不是必需的,因为您可以将您的侦听器注册为MessageListenerContainer的委托,并将connectionFactory注入其中:这里有一个很好的参考-我能够实现MessageListenerContainer,结果是“TextMessageListener”我创建的类消失了,主方法变成了一行代码:ApplicationContext context=new ClassPathXmlApplicationContext(“ApplicationContext.xml”);以下是我在spring配置中添加的内容: