Java Connection Activemq code example (producer and consumer)

Source: Internet
Author: User
Tags message queue

Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory;Importjavax.jms.*; Public classJMSProducer {//Default connection user name    Private Static FinalString USERNAME =Activemqconnection.default_user; //Default connection Password    Private Static FinalString PASSWORD =Activemqconnection.default_password; //Default Connection Address    Private Static FinalString Brokeurl =Activemqconnection.default_broker_url; //number of messages sent    Private Static Final intSendnum = 10;  Public Static voidMain (string[] args) {//Connection FactoryConnectionFactory ConnectionFactory; //ConnectionConnection Connection =NULL; //threads that the session accepts or sends messagesSession session; //Destination of messageDestination Destination; //message producerMessageProducer MessageProducer; //instantiating a connection factoryConnectionFactory =Newactivemqconnectionfactory (Jmsproducer.username, Jmsproducer.password, Jmsproducer.brokeurl); Try {            //getting connections from a connection factoryConnection =connectionfactory.createconnection (); //Start ConnectionConnection.start (); //Create sessionSession = Connection.createsession (true, Session.auto_acknowledge); //Create a message queue named HelloWorldDestination = Session.createqueue ("HelloWorld"); //Create a message producerMessageProducer =session.createproducer (destination); //Send MessageSendMessage (Session, MessageProducer);        Session.commit (); } Catch(Exception e) {e.printstacktrace (); }finally{            if(Connection! =NULL){                Try{connection.close (); } Catch(jmsexception e) {e.printstacktrace (); }            }        }    }    /*** Send Message *@paramSession *@parammessageproducer Message producer *@throwsException*/     Public Static voidSendMessage (Session session,messageproducer messageproducer)throwsexception{ for(inti = 0; i < Jmsproducer.sendnum; i++) {            //Create a text messageTextMessage message = Session.createtextmessage ("The message provider sends to MQ the" + i + "message!) "); System.out.println ("The message provider sends to MQ the" + i + "Message! "); //sending messages through message producersmessageproducer.send (message); }    }}
JMSProducer
Importorg.apache.activemq.ActiveMQConnection;Importorg.apache.activemq.ActiveMQConnectionFactory;Importjavax.jms.*; Public classJmsconsumer {Private Static FinalString USERNAME = Activemqconnection.default_user;//Default connection user name    Private Static FinalString PASSWORD = Activemqconnection.default_password;//Default connection Password    Private Static FinalString Brokeurl = Activemqconnection.default_broker_url;//Default Connection Address     Public Static voidMain (string[] args) {connectionfactory connectionfactory;//Connection FactoryConnection Connection =NULL;//ConnectionSession session;//threads that the session accepts or sends messagesDestination Destination;//Destination of messageMessageconsumer Messageconsumer;//the consumer of the message//instantiating a connection factoryConnectionFactory =Newactivemqconnectionfactory (Jmsconsumer.username, Jmsconsumer.password, Jmsconsumer.brokeurl); Try {            //getting connections from a connection factoryConnection =connectionfactory.createconnection (); //Start ConnectionConnection.start (); //Create sessionSession = Connection.createsession (false, Session.auto_acknowledge); //Create a message queue that connects HelloWorldDestination = Session.createqueue ("HelloWorld"); //Create message ConsumersMessageconsumer =Session.createconsumer (destination);  while(true) {TextMessage TextMessage= (TextMessage) messageconsumer.receive (100000); if(TextMessage! =NULL) {System.out.println ("Received message:" +Textmessage.gettext ()); }Else {                     Break; }            }        } Catch(jmsexception e) {e.printstacktrace (); }    }}
Jmsconsumer

Java Connection Activemq code example (producer and consumer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.