ActiveMQ (li), activemqli

Source: Internet
Author: User

ActiveMQ (li), activemqli

I. ActiveMQ

First, ActiveMQ is not a framework. It is not a struct, webx, or netty framework. It is more like a tomcat server, because you must start it before using it, the relationship between activeMQ and JMS is a bit similar to that between tomcat and servlet. tomcat implements the servlet specification. Similarly, activeMQ implements the JMS specification and they are all servers, before using the service, you must start the server.

// # The environment for activeMQ preparation is as simple as that for tomcat. // 1. download: wget drawing xvf apache-activemq-5.4.2-bin.tar.gz // 3. start: Go to the bin directory and run the activemq script. Input http: // localhost: 8161. After the ActiveMQ server is successfully started, we can send messages to or receive messages from the server.

The above is the installation, decompression, and startup of the Linux environment. The following describes how to configure and use ActiveMQ in Windows. Http://activemq.apache.org/download.htmldownload stable.

Ii. ActiveMQ send messages

import javax.jms.Connection;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory; public class SendMessage {     private static final String url          = "tcp://10.20.156.22:61616";    private static final String QUEUE_NAME   = "yunpeng.jiangyp";    protected String            expectedBody = "

This code is very simple, that is, to send a message to the activemq server. After successfully sending a message to activemq, you can go to (http: // 10.20.156.22: 8161/admin/queues. jsp)Activemq server consoleThis message is displayed. The console displays two messages accumulated by yunpeng. jiangyp.

3. ActiveMQ receives messages

 

import javax.jms.Connection;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory; public class ReceiveMessage {     private static final String url        = "tcp://10.20.156.22:61616";    private static final String QUEUE_NAME = "yunpeng.jiangyp";     public static void main(String[] args) throws JMSException {         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);        Connection connection = factory.createConnection();        connection.start();         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);        Destination desion = session.createQueue(QUEUE_NAME);        MessageConsumer consumer = session.createConsumer(desion);        while (true) {             Message message = consumer.receive(1000);            if (message == null) break;            if (message instanceof TextMessage) {                TextMessage textMsg = (TextMessage) message;                String msg = textMsg.getText();                System.out.println(msg);            }        }        connection.close();     }}

 

This code is also very simple, that is, to receive a message from the activemq server, you can find that the Connection, Session, and Queue must be created to send and receive messages. The only difference is that MessageProducer is created to send messages, the MessageConsumer is created to receive messages, which proves the producer and consumer modes of activemq. Running this code prints the message information sent before, and the console displays the consumption of two accumulated messages.

Two message sending methods are available in JMS, including point-to-point and publish/subscribe.

IV,

Related Article

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.