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)