简体   繁体   中英

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.

I am completely new to JMS, i only know servlets,jsp,listeners,ie no frameworks.
I have: Eclipse-Kepler and JDK1.7 and was not able to configure ActiveMQ on Apache.
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:

@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). 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) . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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