简体   繁体   English

JMS-ActiveMQ-Servlet(远程服务器(Apache-ActiveMQ))和控制台Java程序

[英]JMS - ActiveMQ - Servlet(Remote Server (Apache-ActiveMQ)) and Console Java Program

I have to send a message from a JAVA console program to a servlet on APACHE Tomcat 7.0.42 Server and using ActiveMQ 5.8.0 and send the acknowledgement message back to the program and continue the same thing until server goes offline. 我必须将消息从JAVA控制台程序发送到APACHE Tomcat 7.0.42 Server上的servlet,并使用ActiveMQ 5.8.0,然后将确认消息发送回该程序,并继续执行相同的操作,直到服务器脱机为止。

I am completely new to JMS, i only know servlets,jsp,listeners,ie no frameworks. 我对JMS完全陌生,我只知道servlet,jsp,侦听器,即没有框架。
I have: Eclipse-Kepler and JDK1.7 and was not able to configure ActiveMQ on Apache. 我有:Eclipse-Kepler和JDK1.7,并且无法在Apache上配置ActiveMQ。
I read quite many blogs but nothing seems to work 我读了很多博客,但似乎无济于事

Please, guide me how to go about the problem. 请指导我如何解决该问题。

Thanks you. 谢谢。

If you are using a servlet-container only (Tomcat), you can create an unmanaged thread like this: 如果仅使用servlet容器(Tomcat),则可以创建如下非托管线程:

@WebListener
public class MyServletContextListener implements ServletContextListener {
    public void contextInitialized(final ServletContextEvent sce) {
        final java.util.Timer timer = new Timer();
        // Executes repeatedly (delay = 4000, period = 5000)
        timer.schedule(new ReplyTask(), 4000, 5000);
        sce.getServletContext().setAttribute("replyTaskTimer", timer);
    }

    public void contextDestroyed(final ServletContextEvent sce) {
        final java.util.Timer timer =
          (Timer) sce.getServletContext().getAttribute("replyTaskTimer");
        timer.cancel();
    }
}

In the ReplyTask just read the incoming queue, and send something on an outgoing queue (I suggest using two different queues to ping and pong). ReplyTask只需读取传入队列,然后在传出队列中发送一些内容(我建议使用两个不同的队列来ping和pong)。 You must cancel the timer, that thread will otherwise survive undeployments and redeployments. 必须取消计时器,否则该线程将在取消部署和重新部署后继续存在。

Note: If you were using an application server (JBoss for example), you could do that using a Message driven bean (MDB) . 注意:如果您使用的是应用程序服务器(例如JBoss),则可以使用消息驱动bean(MDB)来实现 More elegant and concise, and the threading is managed by the application server. 更加简洁明了,线程由应用程序服务器管理。 The extra benefit of using an application server like JBoss is the integrated HornetQ JMS provider. 使用像JBoss这样的应用服务器的额外好处是集成的HornetQ JMS提供程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM