ActiveMQ application (1)-Installation and basic mode instance, activemq instance

Source: Internet
Author: User

ActiveMQ application (1)-Installation and basic mode instance, activemq instance

0.

Https://activemq.apache.org/download.html

 

1. decompress and start the activemq Service (you need to select different startup files based on different systems)

// Apache-activemq-5.13.1/bin/macosx/activemq start

 

2. log on to the activemq server to view the information.

Address: http: // localhost: 8161/

Click [Manage ActiveMQ broker] to log on to view detailed data. The default username and password are admin and admin.

 

 

3. Create an eclipse project

/Apache-activemq-5.13.1/lib

 

3.1 Common jms example

public class Sender {    private static final int SEND_NUMBER=5;        public static void main(String[] args){        ConnectionFactory connectionFactory;        Connection connection =null;        Session session;        Destination destination;        MessageProducer producer;                connectionFactory=new ActiveMQConnectionFactory(                ActiveMQConnection.DEFAULT_USER,                ActiveMQConnection.DEFAULT_PASSWORD,                "tcp://localhost:61616");        try{            connection = connectionFactory.createConnection();            connection.start();                        session=connection.createSession(Boolean.TRUE,                    Session.AUTO_ACKNOWLEDGE);                        destination=session.createQueue("JMeterQueue");                        producer=session.createProducer(destination);            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);            sendMessage(session,producer);            session.commit();        }catch(Exception e){            e.printStackTrace();        }finally{            try{                if(null!=connection){                    connection.close();                }            }catch(Throwable ignore){                            }        }    }        public static void sendMessage(Session session,MessageProducer producer) throws JMSException{        for(int i=1;i<SEND_NUMBER;i++){            TextMessage message=session.createTextMessage("ActiveMq send "+i);            System.out.println("ActiveMq send "+i);            producer.send(message);        }    }}

 

Public class Receiver {public static void main (String [] args) {ConnectionFactory connectionFactory; Connection connection = null; Session session; Destination destination; MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, "tcp: // localhost: 61616"); try {connection = connectionFactory. createConnection (); Connection. start (); session = connection. createSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); destination = session. createQueue ("JMeterQueue"); consumer = session. createConsumer (destination); while (true) {TextMessage message = (TextMessage) consumer. receive (10000); if (null! = Message) {System. out. println ("Message receive" + message. getText ();} else {break;} session. commit (); // session. after commit, Messages in Messages Enqueued will be consumed, and Messages Dequeued will be added; // if no commit is enabled, Messages Dequeued will always be 0, every time the consumer er is started, all unconsumed messages will be received.} catch (Exception e) {e. printStackTrace ();} finally {try {if (null! = Connection) connection. close () ;}catch (Throwable ignore ){}}}}

 

3.2 p2p example

Public class QueueSender {// Number of public static final int SEND_NUM = 5; // tcp address public static final String BROKER_URL = "tcp: // localhost: 61616"; // target, create public static final String DESTINATION = "mq. p2p. queue "; public static void run () throws Exception {QueueConnection connection = null; QueueSession session = null; try {// create a link factory QueueConnectionFactory factory = new ActiveMQCon NectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, BROKER_URL); // create a connection = factory through the factory. createQueueConnection (); // start the connection. start (); // create a session = connection. createQueueSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); // creates a Message Queue (queue Queue = session. createQueue (DESTINATION); // creates the message sender javax. jms. queueSender sender = session. create Sender (queue); // sets the persistence mode sender. setDeliveryMode (DeliveryMode. NON_PERSISTENT); sendMessage (session, sender); // submit the session. commit ();} catch (Exception e) {throw e;} finally {// close releasing resources if (session! = Null) {session. close () ;}if (connection! = Null) {connection. close () ;}} public static void sendMessage (QueueSession session, javax. jms. queueSender sender) throws Exception {for (int I = 0; I <SEND_NUM; I ++) {String message = "Send message No." + (I + 1) + "entries"; Message msg = session. createTextMessage (message); sender. send (msg) ;}} public static void main (String [] args) throws Exception {QueueSender. run ();}}

 

Public class QueueReceiver {// tcp address public static final String BROKER_URL = "tcp: // localhost: 61616"; // target, create public static final String TARGET = "mq. p2p. queue "; public static void run () throws Exception {QueueConnection connection = null; QueueSession session = null; try {// create a connection factory QueueConnectionFactory factory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, CtiveMQConnection. DEFAULT_PASSWORD, BROKER_URL); // create a connection = factory through the factory. createQueueConnection (); // start the connection. start (); // create a session = connection. createQueueSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); // creates a Message Queue (queue Queue = session. createQueue (TARGET); // creates the message producer javax. jms. queueReceiver already ER = session. createReceiver (queue); receiver. setMessageListener (new M EssageListener () {public void onMessage (Message msg) {if (msg! = Null) {TextMessage map = (TextMessage) msg; try {System. out. println (map. getText ();} catch (JMSException e) {e. printStackTrace () ;}}}); // shut down the Thread after Ms of sleep. sleep (1000*20); // submit the session. commit ();} catch (Exception e) {throw e;} finally {// close releasing resources if (session! = Null) {session. close () ;}if (connection! = Null) {connection. close () ;}} public static void main (String [] args) throws Exception {QueueReceiver. run ();}}

 

3.3 subscription example

Public class TopicSender {// Number of public static final int SEND_NUM = 5; // tcp address public static final String BROKER_URL = "tcp: // localhost: 61616"; // target, create public static final String DESTINATION = "mq. topic "; public static void run () throws Exception {TopicConnection connection = null; TopicSession session = null; try {// create a connection factory TopicConnectionFactory factory = new ActiveMQConnect IonFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, BROKER_URL); // create a connection = factory through the factory. createTopicConnection (); // start the connection. start (); // create a session = connection. createTopicSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); // creates a Message Queue Topic = session. createTopic (DESTINATION); // create a message sender TopicPublisher publisher = session. createPublishe R (topic); // sets the persistence mode publisher. setDeliveryMode (DeliveryMode. NON_PERSISTENT); sendMessage (session, publisher); // submit the session. commit ();} catch (Exception e) {throw e;} finally {// close releasing resources if (session! = Null) {session. close () ;}if (connection! = Null) {connection. close () ;}} public static void sendMessage (TopicSession session, TopicPublisher publisher) throws Exception {for (int I = 0; I <SEND_NUM; I ++) {String message = "message sent" + (I + 1) + ""; TextMessage msg = session. createTextMessage (message); publisher. send (msg) ;}} public static void main (String [] args) throws Exception {TopicSender. run ();}}

 

Public class topickerer {// tcp address public static final String BROKER_URL = "tcp: // localhost: 61616"; // target, create public static final String TARGET = "mq. topic "; public static void run () throws Exception {TopicConnection connection = null; TopicSession session = null; try {// create a connection factory TopicConnectionFactory factory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, Activ EMQConnection. DEFAULT_PASSWORD, BROKER_URL); // create a connection = factory through the factory. createTopicConnection (); // start the connection. start (); // create a session = connection. createTopicSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); // creates a Message Queue Topic = session. createTopic (TARGET); // create a message producer TopicSubscriber subscriber = session. createSubscriber (topic); subscriber. setMessageListener (new Message Listener () {public void onMessage (Message msg) {System. out. println (msg) ;}}); // shut down the Thread after Ms of sleep. sleep (1000*20); // submit the session. commit ();} catch (Exception e) {throw e;} finally {// close releasing resources if (session! = Null) {session. close () ;}if (connection! = Null) {connection. close () ;}} public static void main (String [] args) throws Exception {TopicReceiver. run ();}}

 

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.