Activemq publishing and subscription

Source: Internet
Author: User

Publish a message:

 

Code

Import Javax. JMS. connection;
Import Javax. JMS. jmsexception;
Import Javax. JMS. message;
Import Javax. JMS. messageconsumer;
Import Javax. JMS. messagelistener;
Import Javax. JMS. messageproducer;
Import Javax. JMS. Session;
Import Javax. JMS. textmessage;
Import Javax. JMS. topic;

ImportOrg. Apache. activemq. activemqconnectionfactory;
ImportOrg. Apache. activemq. Broker. brokerservice;
ImportOrg. Apache. activemq. Command. activemqtopic;

Public ClassTopictest {

Public   Static   Void Main (string [] ARGs) Throws Exception {
Activemqconnectionfactory Factory =   New Activemqconnectionfactory ( " TCP: // localhost: 61616 " );
Connection connection = Factory. createconnection ();
Connection. Start ();



// Create a topic
Topic =   New Activemqtopic ( " Testtopic " );
Session session = Connection. createsession ( False , Session. auto_acknowledge );

// Create a producer and send multiple messages.
Messageproducer producer = Session. createproducer (topic );
For ( Int I = 0 ; I < 10 ; I ++ ){
Producer. Send (session. createtextmessage ( " Zyg: "   + I ));
}

}
}

 

 

Subscribe to messages

 

Code

Import Javax. JMS. connection;
Import Javax. JMS. jmsexception;
Import Javax. JMS. message;
Import Javax. JMS. messageconsumer;
Import Javax. JMS. messagelistener;
Import Javax. JMS. messageproducer;
Import Javax. JMS. Session;
Import Javax. JMS. textmessage;
Import Javax. JMS. topic;

ImportOrg. Apache. activemq. activemqconnectionfactory;
ImportOrg. Apache. activemq. Broker. brokerservice;
ImportOrg. Apache. activemq. Command. activemqtopic;

Public   Class Testcomsumer {
Public   Static   Void Main (string [] ARGs) Throws Exception {
Activemqconnectionfactory Factory =   New Activemqconnectionfactory ( " TCP: // localhost: 61616 " );
Connection connection = Factory. createconnection ();
Connection. Start ();



// Create a topic
Topic =   New Activemqtopic ( " Testtopic " );
Session session = Connection. createsession ( False , Session. auto_acknowledge );

// Registered consumer 1
Messageconsumer comsumer1 = Session. createconsumer (topic );
Comsumer1.setmessagelistener ( New Messagelistener (){
Public   Void Onmessage (message m ){
Try {
System. Out. println ( " Consumer1 get "   + (Textmessage) M). gettext ());
} Catch (Jmsexception e ){
E. printstacktrace ();
}
}
});
// Registered consumer 2
Messageconsumer comsumer2 = Session. createconsumer (topic );
Comsumer2.setmessagelistener ( New Messagelistener (){
Public   Void Onmessage (message m ){
Try {
System. Out. println ( " Consumer2 get "   + (Textmessage) M). gettext ());
} Catch (Jmsexception e ){
E. printstacktrace ();
}
}
});
// Create a producer and send multiple messages.

}

}

 

 

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.