Consuming Rabbit MQ messages using Eclipse on Tomcat -


i using eclipse, tomcat , rabbit mq.

i want able consume messages of queue hit queue. have managed using java class in eclipse (see below), have not been able working when deploying war file on tomcat server.

package org.com.hello; import com.rabbitmq.client.connectionfactory; import com.rabbitmq.client.connection; import com.rabbitmq.client.channel; import com.rabbitmq.client.queueingconsumer; public class hellorecv {    public static void main(string[] argv)       throws java.io.ioexception,              java.lang.interruptedexception {     connectionfactory factory = new connectionfactory();     factory.sethost("172.24.3.53");     factory.setport(6672);     factory.setusername("user");     factory.setpassword("password");     connection connection = factory.newconnection();     channel channel = connection.createchannel();      channel.queuedeclare("q1", true, false, false, null);     system.out.println(" [*] waiting messages. exit press ctrl+c");      queueingconsumer consumer = new queueingconsumer(channel);     channel.basicconsume("q1", true, consumer);      while (true) {       queueingconsumer.delivery delivery = consumer.nextdelivery();       string message = new string(delivery.getbody());       system.out.println(" [x] received '" + message + "'");     }      } }    

is there need add web.xml file , if so, should add file?

create war application using eclipse:

you can see video: https://www.youtube.com/watch?v=fk22hqz9l_m

or googling copy code inside servlet. should use init method.

an pseudo code:

    public class yourservlet extends httpservlet {        int count;        public void init(servletconfig config) throws servletexception {         super.init(config);          connectionfactory factory = new connectionfactory();          factory.sethost("172.24.3.53");         factory.setport(6672);         factory.setusername("user");         factory.setpassword("password");         connection connection = factory.newconnection();         channel channel = connection.createchannel();          channel.queuedeclare("q1", true, false, false, null);      /// shoud put consumer inside thread.     channel.basicconsume("q1", true, new defaultconsumer(){     /// here should use default consumer , not queueingconsumer because blocking      });     }   }         public void doget(httpservletrequest req, httpservletresponse res)                                 throws servletexception, ioexception {         res.setcontenttype("text/plain");         printwriter out = res.getwriter();         .....         }     } 

see defaultconsumer

hope helps


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -