jms - How can I push messages from Activemq to Java Application -


hi developers,

i want write 2 .java files using jms library names messageproducer , messageconsumer.

i added activemq-all-5.8.0.jar , commons-io-2.4.jar files in lib folder.and changed port number of activemq 61616 61617.

using messageproducer.java file,i send messages activemq.for wrote code it's working fine.if want see click on link.

enter image description here

i want send messages activemq messageconsumer.java.this application in apache tomcat(http://localhost:8080/executablefileprocess/messageconsumer)

once messageconsumer receives message, separates message-body message , print on console(just testing).for wrote following 2 java files.but it's not working.

messageconsumer.java :

 package packagename;  import java.io.ioexception;  import javax.jms.connection;  import javax.jms.connectionfactory;  import javax.jms.messagelistener;  import javax.jms.queue;  import javax.jms.session;  import javax.servlet.servletexception;  import javax.servlet.http.httpservlet;  import javax.servlet.http.httpservletrequest;  import javax.servlet.http.httpservletresponse;  import org.apache.activemq.activemqconnectionfactory;  public class messageconsumer extends httpservlet { @override protected void service(httpservletrequest arg0, httpservletresponse arg1)         throws servletexception, ioexception { try {     //creating connectionfactory object way     connectionfactory connectionfactory=new activemqconnectionfactory("admin","admin","tcp://localhost:61617");     //establishing connection b/w application , activemq     connection connection=connectionfactory.createconnection();     session session=connection.createsession(false, session.auto_acknowledge);     queue queue=session.createqueue("messagetesing");     javax.jms.messageconsumer consumer=session.createconsumer(queue);     //fetching queues activemq     messagelistener listener = new mylistener();     consumer.setmessagelistener(listener);     connection.start(); } catch (exception e) {     // todo: handle exception }  } } 

mylistener.java :

package packagename; import javax.jms.message; import javax.jms.messagelistener; public class mylistener implements messagelistener { public void onmessage(message msg) {     system.out.println(msg); } }; 

i didn't configure destination queue in activemq console , didn't mention "destination" while sending message messageproducer.java.

i using eclipse.how can print messagebody in console,actually based on messagebody operations in messageconsumer.java.but testing need see messagebody.

i hope,you understand trying.

i new jmsand java,so can explain clearly.so far wrote code using google search.but didn't find anywhere issue.

can suggest me.

thanks.

possibly program starts , terminates, because have written client receive messages asynchronously (by using messagelistener, in different thread), , main thread terminates.

try this:

                connection.start();                  system.out.println("press key terminate");                 try {                     system.in.read();                 } catch (ioexception e) {                     e.printstacktrace();                 }                  system.out.println("end"); 

this should keep program running, until hit key.


to read body of textmessage, use (this quoted activemq hello world examples:

            if (message instanceof textmessage) {                 textmessage textmessage = (textmessage) message;                 string text = textmessage.gettext();                 system.out.println("received: " + text);             } else {                 system.out.println("received: " + message);             } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -