java - JMSException: Queue does not exist -
i new in jms , want create basic messageproducer sends message , messageconsumer receives message asynchronously. when run code error message :
messageproducer.java
package activemq.test; import java.util.date; import org.apache.activemq.activemqconnectionfactory; import javax.jms.*; public class messageproducer{ javax.jms.messageproducer producer = null; connection connection = null; session session = null; public messageproducer(){ try { // create connectionfactory activemqconnectionfactory connectionfactory = new activemqconnectionfactory("tcp://localhost:61616"); // create connection connection = connectionfactory.createconnection(); connection.start(); // create session session = connection.createsession(false, session.auto_acknowledge); // create destination (topic or queue) destination destination = session.createqueue("test.foo"); // create messageproducer session topic or queue producer = session.createproducer(destination); producer.setdeliverymode(deliverymode.non_persistent); // create messages string text = "hello world! from: messageproducer"; textmessage message = session.createtextmessage(text); // tell producer send message system.out.println("producer going send message"); producer.send(message); } catch (exception e) { system.out.println("caught: " + e); e.printstacktrace(); } } public void sendmessage(){ try { // create messages string text = "hello world! from: " + new date(); textmessage message = session.createtextmessage(text); // tell producer send message system.out.println("sent message: "+ message.hashcode()); producer.send(message); } catch (exception e) { e.printstacktrace(); } } public void close(){ // clean try { session.close(); connection.close(); } catch (jmsexception e) { e.printstacktrace(); } } }
messageconsumer.java
package activemq.test; import org.apache.activemq.activemqconnectionfactory; import javax.jms.*; public class messageconsumer implements exceptionlistener{ connection connection = null; javax.jms.messageconsumer consumer = null; session session = null; public messageconsumer(){ try { // create connectionfactory activemqconnectionfactory connectionfactory = new activemqconnectionfactory("tcp://localhost:61616"); // create connection connection = connectionfactory.createconnection(); connection.start(); connection.setexceptionlistener(this); // create session session = connection.createsession(false, session.auto_acknowledge); // create destination (topic or queue) destination destination = session.createqueue("test.foo"); // create messageconsumer session topic or queue consumer = session.createconsumer(destination); messagelistener listener = new messagelistener() { public void onmessage(message message) { try { if (message instanceof textmessage) { textmessage textmessage = (textmessage) message; system.out.println("received message" + textmessage.gettext() + "'"); } } catch (jmsexception e) { system.out.println("caught:" + e); e.printstacktrace(); } } }; consumer.setmessagelistener(listener); } catch (exception e) { system.out.println("caught: " + e); e.printstacktrace(); } } @override public void onexception(jmsexception exception) { system.out.println("jms exception occured. shutting down client."); } public void close(){ // clean try { consumer.close(); session.close(); connection.close(); } catch (jmsexception e) { e.printstacktrace(); } } }
appmain.java
public class appmain { public static void main(final string arg[]) throws exception { messageproducer msproducer = new messageproducer(); msproducer.sendmessage(); msproducer.close(); messageconsumer msconsumer = new messageconsumer(); msconsumer.close(); } }
when messageconsumer created, error message:
caught: javax.jms.jmsexception: amq119017: queue jms.queue.test.foo not exist javax.jms.jmsexception: amq119017: queue jms.queue.test.foo not exist @ org.apache.activemq.util.jmsexceptionsupport.create(jmsexceptionsupport.java:54) @ org.apache.activemq.activemqconnection.syncsendpacket(activemqconnection.java:1405) @ org.apache.activemq.activemqsession.syncsendpacket(activemqsession.java:1925) @ org.apache.activemq.activemqmessageconsumer.<init>(activemqmessageconsumer.java:275) @ org.apache.activemq.activemqsession.createconsumer(activemqsession.java:1157) @ org.apache.activemq.activemqsession.createconsumer(activemqsession.java:1101) @ org.apache.activemq.activemqsession.createconsumer(activemqsession.java:1014) @ org.apache.activemq.activemqsession.createconsumer(activemqsession.java:987) @ activemq.test.messageconsumer.<init>(messageconsumer.java:36) @ activemq.test.appmain.main(appmain.java:17) caused by: activemqnonexistentqueueexception[errortype=queue_does_not_exist message=amq119017: queue jms.queue.test.foo not exist] @ org.apache.activemq.core.server.impl.serversessionimpl.createconsumer(serversessionimpl.java:448) @ org.apache.activemq.core.protocol.openwire.amq.amqserversession.createconsumer(amqserversession.java:326) @ org.apache.activemq.core.protocol.openwire.amq.amqconsumer.init(amqconsumer.java:138) @ org.apache.activemq.core.protocol.openwire.amq.amqsession.createconsumer(amqsession.java:144) @ org.apache.activemq.core.protocol.openwire.openwireprotocolmanager.addconsumer(openwireprotocolmanager.java:544) @ org.apache.activemq.core.protocol.openwire.openwireconnection.processaddconsumer(openwireconnection.java:1118) @ org.apache.activemq.command.consumerinfo.visit(consumerinfo.java:347) @ org.apache.activemq.core.protocol.openwire.openwireconnection.bufferreceived(openwireconnection.java:272) @ org.apache.activemq.core.remoting.server.impl.remotingserviceimpl$delegatingbufferhandler.bufferreceived(remotingserviceimpl.java:678) @ org.apache.activemq.core.remoting.impl.netty.activemqchannelhandler.channelread(activemqchannelhandler.java:77) @ io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext.java:332) @ io.netty.channel.abstractchannelhandlercontext.firechannelread(abstractchannelhandlercontext.java:318) @ io.netty.channel.defaultchannelpipeline.firechannelread(defaultchannelpipeline.java:787) @ io.netty.channel.nio.abstractniobytechannel$niobyteunsafe.read(abstractniobytechannel.java:125) @ io.netty.channel.nio.nioeventloop.processselectedkey(nioeventloop.java:507) @ io.netty.channel.nio.nioeventloop.processselectedkeysoptimized(nioeventloop.java:464) @ io.netty.channel.nio.nioeventloop.processselectedkeys(nioeventloop.java:378) @ io.netty.channel.nio.nioeventloop.run(nioeventloop.java:350) @ io.netty.util.concurrent.singlethreadeventexecutor$2.run(singlethreadeventexecutor.java:116) @ java.lang.thread.run(thread.java:745)
why error when messageconsumer created, don't error when messageproducer created.
i use activemqserver broker:
server.java
package activemq.test; import java.util.hashmap; import java.util.hashset; import java.util.map; import org.apache.activemq.api.core.transportconfiguration; import org.apache.activemq.core.config.configuration; import org.apache.activemq.core.config.impl.configurationimpl; import org.apache.activemq.core.remoting.impl.netty.nettyacceptorfactory; import org.apache.activemq.core.server.activemqserver; import org.apache.activemq.core.server.activemqservers; public class server { public static void main(final string arg[]) throws exception { try { // step 1. create configuration, , set properties accordingly configuration configuration = new configurationimpl(); //we need server lock file configuration.setjournaldirectory("target/data/journal"); configuration.setpersistenceenabled(false); // http://activemq.apache.org/what-is-the-difference-between-persistent-and-non-persistent-delivery.html configuration.setsecurityenabled(false); // http://activemq.apache.org/security.html /** * map configuration values not necessary (it configures default values). * if want modify run example in 2 different hosts, remember * modify client's connector @ {@link embeddedremoteexample}. */ map<string, object> map = new hashmap<string, object>(); map.put("host", "localhost"); map.put("port", 61616); // https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/5/html/hornetq_user_guide/ch14s04.html transportconfiguration transpconf = new transportconfiguration(nettyacceptorfactory.class.getname(),map); hashset<transportconfiguration> settransp = new hashset<transportconfiguration>(); settransp.add(transpconf); configuration.setacceptorconfigurations(settransp); // https://github.com/apache/activemq-6/blob/master/activemq-server/src/main/java/org/apache/activemq/spi/core/remoting/acceptor.java // step 2. create , start server activemqserver server = activemqservers.newactivemqserver(configuration); server.start(); } catch (exception e) { e.printstacktrace(); throw e; } } }
i think, in producer, starting connection before setting destination. try starting afterwards....
// create connectionfactory activemqconnectionfactory connectionfactory = new activemqconnectionfactory("tcp://localhost:61616"); // create connection connection = connectionfactory.createconnection(); // create session session = connection.createsession(false, session.auto_acknowledge); // create destination (topic or queue) destination destination = session.createqueue("test.foo"); // create messageproducer session topic or queue producer = session.createproducer(destination); producer.setdeliverymode(deliverymode.non_persistent); connection.start(); // create messages string text = "hello world! from: messageproducer"; textmessage message = session.createtextmessage(text); // tell producer send message system.out.println("producer going send message"); producer.send(message);
on other hand, consumer, suggest implement messageconsumer (instead of exception). once implemented, in constructor can initiate consumer
activemqconnectionfactory factory = new activemqconnectionfactory(url); connection = factory.createconnection(); session = connection.createsession(false, session.auto_acknowledge); // create destination (topic or queue) destination destination = session.createqueue("test.foo"); // create messageconsumer session topic or queue consumer = session.createconsumer(destination).setmessagelistener(this); connection.start();
.... , implement onmessage method
public void onmessage(message message) { try { if (message instanceof textmessage) { textmessage textmessage = (textmessage) message; system.out.println("received message" + textmessage.gettext() + "'"); } } catch (jmsexception e) { system.out.println("caught:" + e); e.printstacktrace(); } }
Comments
Post a Comment