ActiveMQ Run case

Source: Internet
Author: User

Objective

ActiveMQ is the most popular, powerful, open source messaging bus that Apache has produced. ActiveMQ is a JMS provider implementation that fully supports the JMS1.1 and the Java EE 1.4 specification, although the JMS specification has been around for a long time, but JMS still plays a special role in the middle of today's Java EE applications.

First, use the introduction environment preparation

1, activemq Download Portal: https://activemq.apache.org/download.html, MyEclipse

2, start ACTIVEMQ, My computer is Win64 bit, so start the bin Win64 in the Activemq.bat

3, need to enter the user name and password to enter, the page successfully started the effect. (The default user name and password are: admin)


Start Test 1, create a new JMS Java project, import the jar packages in the downloaded ACTIVEMQ file, such as the Activemq-all-5.11.1.jar in the author directory


Import Project


2. New Message producer Jmsproducer.java

Package Com.hcg.activemq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.jmsexception;import Javax.jms.messageproducer;import javax.jms.Session; Import Javax.jms.textmessage;import Org.apache.activemq.activemqconnection;import org.apache.activemq.activemqconnectionfactory;/** * Message producer * * @author Babylon * 2016-5-9 */public class JMSProducer {Priv Ate static final string USERNAME = activemqconnection.default_user;//Default connection user name private static final String PASSWORD = Activ emqconnection.default_password;//default connection password private static final String Brokeurl = Activemqconnection.default_broker_url ;//The default connection address private static final int sendnum= 10;//the number of messages sent public static void main (string[] args) {connectionfactory Connec   Tionfactory; Connection Factory Connection connection = null; A thread that connects session session;//sessions, receives or sends messages destination the destination of destination;//messages messageproducer messageproducer;//message producer// Instantiating a connection factory connectionfactory = new Activemqconnectionfactory (JmsprodUcer. USERNAME, Jmsproducer.password, jmsproducer.brokeurl); try {connection = connectionfactory.createconnection ();// Get the connection through the Connection Factory Connection.start ();//start Connection Session = Connection.createsession (true, Session.auto_acknowledge);// Create Session Destination = Session.createqueue ("FirstQueue1");//Create message Queue MessageProducer = session.createproducer (destination)    ;//Create message producer//Send Message SendMessage (session, MessageProducer); Formally submit the action to send the message Session.commit ();}  catch (JMSException e) {e.printstacktrace ();} Finally {//close connection if (Connection!=null) {try {connection.close ();} catch (JMSException e) {e.printstacktrace ()}}}} /** * Send Message * @param session * @param messageproducer * @throws jmsexception */public static void SendMessage (Session Sess Ion, MessageProducer MessageProducer) throws jmsexception{for (int i=0; i < jmsproducer.sendnum; i++) {TextMessage Message = Session.createtextmessage ("message sent by ActiveMQ" +i); SYSTEM.OUT.PRINTLN ("Send message:" +i); messageproducer.send (message);}}

3, F11 running instance, can see successfully sent 10 messages


3. Create Consumer Jmsconsumer.java

Package Com.hcg.activemq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.jmsexception;import Javax.jms.messageconsumer;import javax.jms.Session; Import Javax.jms.textmessage;import Org.apache.activemq.activemqconnection;import org.apache.activemq.activemqconnectionfactory;/** * Message Consumer * @author Babylon * 2016-5-9 */public class Jmsconsumer {privat E static final String USERNAME = activemqconnection.default_user;//Default connection user name private static final String PASSWORD = Activem qconnection.default_password;//default connection password private static final String Brokeurl = activemqconnection.default_broker_url;/   /default connection address public static void main (string[] args) {connectionfactory connectionfactory; Connection Factory Connection connection = null; A thread that connects session session;//sessions, receives or sends messages destination the destination of destination;//messages Messageconsumer messageconsumer;//message producer// Instantiating a connection factory connectionfactory = new Activemqconnectionfactory (Jmsconsumer.username, Jmsconsumer.password, JMSConsumer.BRokeurl); try {connection = connectionfactory.createconnection ();//Connect to the factory to get the connection Connection.start ();//Start Connection session = Connection.createsession (False, Session.auto_acknowledge);//create session without transaction Destination = Session.createqueue (" FirstQueue1 ");//Create message Queue Messageconsumer = Session.createconsumer (destination);//Create Message consumer while (true) {TextMessage TextMessage = (textmessage) messageconsumer.receive (100000), if (textmessage! = null) {SYSTEM.OUT.PRINTLN ("message Received:" + Textmessage.gettext ());} else {break;}}} catch (JMSException e) {e.printstacktrace ();}}}

4, F11 run the consumer, you can see a new consumer in the console, sent messages have been consumed processing


5, while (true) this way to handle consumption is inappropriate, the following to listen to the way to handle the creation of consumer Jmsconsumer_listener.java.

Package Com.hcg.activemq;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.jmsexception;import Javax.jms.messageconsumer;import javax.jms.Session; Import org.apache.activemq.activemqconnection;import org.apache.activemq.activemqconnectionfactory;/** * Message Consumer- Listening mode consumption * @author Babylon * 2016-5-9 */public class Jmsconsumer_listener {private static final String USERNAME = Activemqco Nnection. default_user;//default connection user name private static final String PASSWORD = activemqconnection.default_password;//default connection password private Static final String Brokeurl = activemqconnection.default_broker_url;//Default connection address public static void main (string[] args) {Co   Nnectionfactory ConnectionFactory; Connection Factory Connection connection = null; A thread that connects session session;//sessions, receives or sends messages destination the destination of destination;//messages Messageconsumer messageconsumer;//message producer// Instantiate the connection factory connectionfactory = new Activemqconnectionfactory (Jmsconsumer_listener. USERNAME, Jmsconsumer_listener. PASSWORD, JmsconSumer_listener. Brokeurl); try {connection = connectionfactory.createconnection ();//Connect to the factory to get the connection Connection.start ();//Start Connection session = Connection.createsession (False, Session.auto_acknowledge);//create session without transaction Destination = Session.createqueue (" FirstQueue1 ");//Create message Queue Messageconsumer = Session.createconsumer (destination);// Create Message Consumer Messageconsumer.setmessagelistener (new Listener ());//Register Message Listener} catch (JMSException e) {e.printstacktrace ();}}}
Listener objects need to implement MessageListener
Package Com.hcg.activemq;import Javax.jms.jmsexception;import Javax.jms.message;import Javax.jms.MessageListener; Import javax.jms.textmessage;/** * Message listening * * @author Babylon * 2016-5-9 */public class Listener implements messagelistener{ /* * Received message */@Overridepublic void onMessage (Message message) {try {System.out.println ("Messages Received:" + ((textmessage) msg). GetText ());} catch (JMSException e) {e.printstacktrace ();}}}
6, run the class, found that the number of consumers increased by one.


How to send and receive messages 1. Create a message Publisher
Package Com.hcg.activemq2;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.jmsexception;import Javax.jms.messageproducer;import javax.jms.Session; Import Javax.jms.textmessage;import Org.apache.activemq.activemqconnection;import org.apache.activemq.activemqconnectionfactory;/** * Message Producer-Message publisher * * @author Babylon * 2016-5-9 */public class Jmsproduc  Er {private static final string USERNAME = activemqconnection.default_user;//Default connection user name private static final string PASSWORD = activemqconnection.default_password;//default connection password private static final String Brokeurl = Activemqconnection.default_ broker_url;//default connection address private static final int sendnum = 10;//number of messages sent public static void main (string[] args) {CONNECTIONFAC   Tory ConnectionFactory; Connection Factory Connection connection = null; A thread that connects session session;//sessions, receives or sends messages destination the destination of destination;//messages messageproducer messageproducer;//message producer// Instantiating a connection factory connectionfactory = new ActivemqconnectionfactoRy (Jmsproducer.username, Jmsproducer.password, jmsproducer.brokeurl); try {connection = Connectionfactory.createconnection ();//Connect to the factory to get the connection Connection.start ();//Start Connection session = Connection.createsession ( True, Session.auto_acknowledge);//create session//Destination = Session.createqueue ("FirstQueue1");//create message Queue Destination = Session.createtopic ("FirstTopic1"); messageproducer = Session.createproducer (destination);//Create message producer//    Send Message SendMessage (session, MessageProducer); Formally submit the action to send the message Session.commit ();}  catch (JMSException e) {e.printstacktrace ();} Finally {//close connection if (Connection!=null) {try {connection.close ();} catch (JMSException e) {e.printstacktrace ()}}}} /** * Posted Message * @param session * @param messageproducer * @throws jmsexception */public static void SendMessage (session ses Sion, MessageProducer MessageProducer) throws jmsexception{for (int i=0; i < jmsproducer.sendnum; i++) {TextMessage Message = Session.createtextmessage ("message sent by ActiveMQ" +i); SYSTEM.OUT.PRINTLN ("Send message:" +i); Messageproducer.send (Message);}}} 
2. Create a Message subscriber 1
Package Com.hcg.activemq2;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.jmsexception;import Javax.jms.messageconsumer;import javax.jms.Session; Import org.apache.activemq.activemqconnection;import org.apache.activemq.activemqconnectionfactory;/** * Message Consumer- Message subscribers 1 * @author Babylon * 2016-5-9 */public class Jmsconsumer {private static final String USERNAME = activemqconnection. default_user;//default connection user name private static final String PASSWORD = activemqconnection.default_password;//default connection password private Static final String Brokeurl = activemqconnection.default_broker_url;//Default connection address public static void main (string[] args) {Co   Nnectionfactory ConnectionFactory; Connection Factory Connection connection = null; A thread that connects session session;//sessions, receives or sends messages destination the destination of destination;//messages Messageconsumer messageconsumer;//message producer// Instantiate the connection factory connectionfactory = new Activemqconnectionfactory (Jmsconsumer.username, Jmsconsumer.password, Jmsconsumer.brokeurl); try {Connection = Connectionfactory.createconnection ();//Connect to the factory to get the connection Connection.start ();//Start Connection session = Connection.createsession (False, Session.auto_acknowledge);//create session without transaction//Destination = Session.createqueue (" FirstQueue1 ");//create message Queue Destination = Session.createtopic (" FirstTopic1 "); Messageconsumer = Session.createconsumer ( destination);//Create Message Consumer Messageconsumer.setmessagelistener (new Listener ());//Register Message Listener} catch (JMSException e) { E.printstacktrace ();} }}

3. Create a Message subscriber 2
Package Com.hcg.activemq2;import Javax.jms.connection;import Javax.jms.connectionfactory;import Javax.jms.destination;import Javax.jms.jmsexception;import Javax.jms.messageconsumer;import javax.jms.Session; Import org.apache.activemq.activemqconnection;import org.apache.activemq.activemqconnectionfactory;/** * Message Consumer- Message subscribers 2 * @author Babylon * 2016-5-9 */public class JMSConsumer2 {private static final String USERNAME = activemqconnection . default_user;//default connection user name private static final String PASSWORD = activemqconnection.default_password;//default connection password private Static final String Brokeurl = activemqconnection.default_broker_url;//Default connection address public static void main (string[] args) {Co   Nnectionfactory ConnectionFactory; Connection Factory Connection connection = null; A thread that connects session session;//sessions, receives or sends messages destination the destination of destination;//messages Messageconsumer messageconsumer;//message producer// Instantiate the connection factory connectionfactory = new Activemqconnectionfactory (Jmsconsumer2.username, Jmsconsumer2.password, Jmsconsumer2.brokeurl); try {COnnection = Connectionfactory.createconnection ();//Connect to the factory to get the connection Connection.start ();//Start Connection session = Connection.createsession (False, Session.auto_acknowledge);//create session without transaction//Destination = Session.createqueue (" FirstQueue1 ");//create message Queue Destination = Session.createtopic (" FirstTopic1 "); Messageconsumer = Session.createconsumer ( destination);//Create Message Consumer Messageconsumer.setmessagelistener (new Listener2 ());//Register Message Listener} catch (JMSException e) { E.printstacktrace ();} }}

Final directory structure:

Demo:https://github.com/jasonbabylon/activemq

ActiveMQ Run case

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.