Simple use of JMS-activemq

Source: Internet
Author: User
Tags stomp

First, download activemq. the following link lists all versions:
Http://activemq.apache.org/download-archives.html
Each version provides links for different operating systems:

 

The company's computer is windows and started with activemq. bat in the directory:

 

The default port number is 61616, which can be seen in CONF/activemq. xml:

<transportConnectors>    <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->    <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>    <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>    <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>    <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>    <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/></transportConnectors>

 

Related Maven dependency:

<dependency>    <groupId>javax.jms</groupId>    <artifactId>javax.jms-api</artifactId>    <version>2.0</version></dependency><dependency>    <groupId>org.apache.activemq</groupId>    <artifactId>activemq-core</artifactId>    <version>5.7.0</version></dependency>

 

Use javax. JMS. Session to communicate with the JMS provider:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(        ActiveMQConnection.DEFAULT_USER,        ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");                                                                                                                                                Connection connection = connectionFactory.createConnection();connection.start();                                                                                                                                                Session session = connection.createSession(Boolean.TRUE,        Session.AUTO_ACKNOWLEDGE);

 

Then some destinations, senders, and sent content are all made by the session:

Destination = session. createqueue ("this is Sparta !! "); Messageproducer producer = session. createproducer (destination); textmessage message0 = session. createtextmessage (" this is Sparta !!! "); Textmessage message1 = session. createtextmessage (" this is also Sparta !!! "); Textmessage message2 = session. createtextmessage (" These are all Sparta !!! "); Producer. Send (message0); producer. Send (message1); producer. Send (message2); Session. Commit ();

 

With producer, consumer is also available. The method for receiving messages is as follows:

MessageConsumer consumer = session.createConsumer(destination);System.out.println(((TextMessage) consumer.receive(10000)).getText());

 

The result is that the consumer goes to the receive one by one, just as the recipient confirms it in person.
Maybe we can let listener do the following:

consumer.setMessageListener(new MessageListener() {    public void onMessage(Message message) {        try {            System.out.println("listener catched:::"+((TextMessage)message).getText());        } catch (JMSException e) {            e.printStackTrace();        }    }});

 

When this consumer is set to listener, it cannot be received in the receive () method,
Otherwise, javax. JMS. illegalstateexception: cannot synchronously receive a message when a messagelistener is set

If you want to use publish/subscribe, simply change createqueue to createtopic, but you must understand that the topic is stateless.

The complete code is as follows, Sender:

{Connectionfactory = new activemqconnectionfactory (activemqconnection. default_user, activemqconnection. default_password, "TCP: // localhost: 61616"); connection = connectionfactory. createconnection (); connection. start (); Session session = connection. createsession (Boolean. true, session. auto_acknowledge); destination Destination = session. createqueue ("this is Sparta !! "); Messageproducer producer = session. createproducer (destination); textmessage message0 = session. createtextmessage (" this is Sparta !!! "); Textmessage message1 = session. createtextmessage (" this is also Sparta !!! "); Textmessage message2 = session. createtextmessage (" These are all Sparta !!! "); Producer. Send (message0); producer. Send (message1); producer. Send (message2); Session. Commit ();}

 

Recipient:

{        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(                ActiveMQConnection.DEFAULT_USER,                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");;        Connection connection = connectionFactory.createConnection();        connection.start();                                                                                                                                                                                                                                                                Session session = connection.createSession(Boolean.FALSE,                Session.AUTO_ACKNOWLEDGE);                                                                                                                                                                                                                                                                Destination destination = session.createQueue("this is sparta!!");        MessageConsumer consumer = session.createConsumer(destination);        System.out.println(((TextMessage) consumer.receive(10000)).getText());        System.out.println(((TextMessage) consumer.receive(10000)).getText());        System.out.println(((TextMessage) consumer.receive(10000)).getText());    }

 

Simple use of JMS-activemq

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.