Apache-ActiveMQ transport XmlMessage

Source: Internet
Author: User

Preface

This article is original. For more information, see the source!

Apache-ActiveMQ: it is an open-source message transfer service provided by Apache. It is very simple and practical to install and use and can be used as a message cache manager (personal opinion ), see what ActiveMQ provides.

ActiveMQ provides two message transmission methods: queue and Topics ).

The difference between the two lies in obtaining the message end. In Queues mode, messages can only be obtained once, that is, multiple message retrieval ends will not obtain duplicate information.

The Topics method is equivalent to subscription. All the message recipients can obtain the same message.

In contrast, Queues is more suitable for load balancing, so that multiple message receivers can process a group of messages. Topics are suitable for message distribution or message multi-purpose use.

ActiveMQ provides the following types of messages: BytesMessage, ObjectMessage, TextMessage, and XmlMessage, as well as some uncommon MapMessagee and StreamMessage.

Next, let's take a look at the specific usage.

1. Configure Apache-ActiveMQ

The installation and configuration of Apache-ActiveMQ are relatively simple. Download Apache-ActiveMQ from the official website. The version used in this article is 5.5.1, which is said to be relatively stable.

In addition, download the two bin packages with the corresponding DLL, which will be used in the project later.

After the download, if the java SDK has not been installed, install the SDK. Otherwise, the SDK cannot run. To configure environment variables, refer to environment variable settings in Windows 7.

After the preceding steps are complete, double-click activemq. bat under directory \ apache-activemq-5.5.1-bin \ apache-activemq-5.5.1 \ bin to start the service.

Apache-ActiveMQ has a monitoring website page http: // 127.0.0.1: 8161/admin/by default. You can view the parameters of Apache-ActiveMQ.

For details about how to configure Apache-ActiveMQ, refer to the official documentation. Apache-ActiveMQ provides a wide range of configurations, including distribution policies, data recovery policies, Server Load balancer, and persistence policies.

If you are interested, You can Google it. Many configuration documents are written. This blog will not repeat the configuration.

Ii. message transmission

Apache-ActiveMQ divides the process of message transmission into two parts. The provided message is called a Producer (which can be understood as the Producer), and the received message is called a Consumer ).

Producter adds a message to the Apache-ActiveMQ Topic (Queue). Consumer extracts the message from the Apache-ActiveMQ Topic (Queue). It can be seen that many others write TextMessage. In this example, XMLMessage is used as the message object, we hope to provide some help to readers who need to transmit messages using XML. The following code is used as an example.

Create a new Producer to send messages:

Main (= ConnectionFactory (IConnection connection = (ISession session = session. createProducer (Apache. NMS. activeMQ. commands. activeMQTopic (I = (I <= Employee () {name = + I. toString (), num ==++ name {; num {;Producer

Code Analysis:

  • Create a factory class example. Port 61616 is the default port and can be configured in the Apache-ActiveMQ configuration file.
  • Create a connection, create a Session, use the Session to create a Producer of ActiveMQTopic, and select the ActiveMQQueue method.
  • The Employee class is a custom Employee class, which contains a Name attribute and a Num (number) attribute.
  • CreateXmlMessage: Creates a message and passes in the Employee instance parameter. This method automatically converts this type to XML format.
  • The Send method sends messages, which contains four parameters.
    • The first parameter is the message to be sent.
    • The second parameter is used to set whether the message is persistent. There are multiple persistence methods (such as files and databases), which can be defined in the ActiveMQQueue configuration file. The default parameter is file.
    • The third parameter sets the message priority.
    • The fourth parameter sets the message expiration time. If it is MaxValue, it indicates that the message never expires. Select

Sending a message is simple. The following is the code for receiving the message:

Main (= ConnectionFactory (IConnection connection = (ISession session = session. createDurableConsumer (Apache. NMS. activeMQ. commands. activeMQTopic (), + = + employee [] + + employee [= Dictionary <,> Dictionary <,> dic {; [= (XmlNode node =Consumer

Code Analysis:

  • Same as sending a message, connect to Apache-ActiveMQ first.
  • Different from sending a message, you must use the Start method to explicitly open the connection.
  • Create a Session and CreateDurableConsumer to create a consumer (subscriber)
  • Sets the consumer acceptance event. After receiving the message, the consumer_Listener method is executed.
  • The Employee class corresponds to the Employee class in the Producer. It uses Dictionary <string, string> to save Employee information and implements an implicit type conversion.

 

Now we can run our sample program. Remember to run the Apache-ActiveMQ service program first.

Run the following command:

 

Summary:

Apache-ActiveMQ is a good tool for regular processing of some messages, and solves the problem that the speed of message sending is different from that of message processing.

You are welcome to provide valuable comments and suggestions. If you have any questions, please add my QQ: 54474314 for discussion.

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.