Introduction to activemq and use of stomp Protocol

Source: Internet
Author: User
Tags add time stomp

Link: http://blog.csdn.net/shagoo/article/details/6077686

As the business volume of Internet enterprises continues to expand, enterprise information network systems become more complex, and performance problems become increasingly prominent. Serial service processing methods have become a major bottleneck, we need more Asynchronous Parallel Processing to improve the business processing capability of enterprise information systems. Therefore, an independent message processing system emerged. ActiveMQ is one of the best open-source message systems. For our technology selection, stability and adaptability are the most important considerations. Therefore, Apache organizes the background and supports publishing/subscription (Pub/Sub) streaming Text Orientated Messaging Protocol and REST ActiveMQ have become the preferred message processor. It is tested in asynchronous mode, the information processing throughput of the entire system is still ideal (a common server can send and receive 4000 messages per second, with a speed of 4 K bytes per message ).

Next let's take a look at the basic configuration and use of ActiveMQ. Most of our company's internal services are written in scripting languages, such as PHP/Python, so we use the Stomp protocol for processing; in addition, in order to improve the efficiency of the message "producer" as much as possible, we adopt the asynchronous nio mode. There are also many configuration and options that need attention in terms of concurrent processing and load balancing, we will try to add time later.

1. Download ActiveMQ (Latest Version 5.4.1)

2. decompress and install the package to the/usr/local/activemq directory.

3. Install the Initialization Configuration File./bin/activemq setup/etc/default/activemq (save it to the default location and fine-tune the startup parameters later)

4. Edit./conf/activemq. xml and add the following section (authentication/stomp + nio ):
...
<Plugins>
<! -- Add By James; Configure authentication; Username, passwords and groups -->
<SimpleAuthenticationPlugin>
<Users>
<AuthenticationUser username = "system" password = "$ {activemq. password}" groups = "users, admins"/>
<AuthenticationUser username = "user" password = "$ {guest. password}" groups = "users"/>
<AuthenticationUser username = "guest" password = "$ {guest. password}" groups = "guests"/>
</Users>
</SimpleAuthenticationPlugin>
</Plugins>
...
<! -- Add by James for support stomp protocol -->
<Transportctor name = "stomp + nio" uri = "stomp + nio: // 0.0.0.0: 61613? Transport. closeAsync = false "/>
...

5. Use the Stomp library to send and receive messages to ActiveMQ (Java examples are not provided here, because there are too many examples on Google. We use PHP as a script language program example ).

A> The following are simple commands for the Stomp protocol:

* SEND
* SUBSCRIBE
* UNSUBSCRIBE
* BEGIN
* COMMIT
* ABORT
* ACK
* DISCONNECT

B> PHP instance (transaction/persistent/ack)

The Stomp PHP library can be downloaded from Google Code. When using the following test cases, you can use http: // activemq-hostname: 8161/admin/to view the number and status of messages.

C> amq_sent.php (producer example)
...
// Include a library
Require_once ("Stomp. php ");

// Make a connection
$ Con = new Stomp ("tcp: // 192.168.1.11: 61613 ");

// Connect
$ Con-> connect ("guest", "password ");

// Begin transaction
$ Con-> begin ("tx1 ");

Try {

// Build a message by map
Require_once ("Stomp/Message/Map. php ");
$ MsgBody = array ("city" => "Belgrade", "name" => "Dejan ");
$ MsgHeader ["persistent"] = "true"; // very important: Because By default, Stomp produced messages are set to non-persistent.
$ MsgHeader ["transformation"] = "jms-map-json ";
$ MapMessage = new StompMessageMap ($ msgBody, $ msgHeader );

// Send the message to the queue
$ Con-> send ("/queue/test", $ mapMessage, array ("transaction" => "tx1 "));

// Test rollback logic
// Throw new Exception ("Sending failed ...");

// Commit sending message
$ Con-> commit ("tx1 ");

// Print message
Echo "Sent message :";
Print_r ($ msgBody );

} Catch (Exception $ e ){

// Rollback sending
$ Con-> abort ("tx1 ");

// Print message
Echo $ e-> getMessage ();
}

// Disconnect
$ Con-> disconnect ();
...

D> amq_recv.php (Consumer example)
...
// Include a library
Require_once ("Stomp. php ");

// Make a connection
$ Con = new Stomp ("tcp: // 192.168.1.11: 61613 ");

// Connect with authentication
$ Con-> connect ("guest", "password ");

// Set read timeout
$ Con-> setReadTimeout (1 );

// Subscribe to the queue
$ Con-> subscribe ("/queue/test", array ("transformation" => "jms-map-json "));

// Receive a message from the queue
$ Msg = $ con-> readFrame ();

// Do what you want with the message
If ($ msg! = Null ){
Echo "Received message :";
Print_r ($ msg );
// Mark the message as received in the queue
$ Con-> ack ($ msg );
} Else {
Echo "Failed to receive a message/n ";
}

// Disconnect
$ Con-> disconnect ();
...

6. Currently, the latest version of activemq message data is stored in a fast Text Database, kahadb. Although it is fast to use, once an error is recovered, there are always many problems. Therefore, I suggest you enable the persistence options of mainstream databases. Although it is slower, data recovery is much simpler. We use MySQL to store persistent message data. The database is activemq, configured in. /CONF/activemq. XML, as follows:
...
<Persistencefactory>
<Journalpersistenceadapterfactory datadirectory = "$ {activemq. Base}/Data" datasource = "# mysql-ds"/>
</Persistencefactory>
...
<! -- MySQL datasource sample setup -->
<Bean id = "mysql-ds" class = "org. Apache. commons. DBCP. basicdatasource" Destroy-method = "close">
<Property name = "driverclassname" value = "com. MySQL. JDBC. Driver"/>
<Property name = "url" value = "jdbc: mysql: // 192.168.1.10: 11811/activemq? RelaxAutoCommit = true "/>
<Property name = "username" value = "admin"/>
<Property name = "password" value = "ihush2010"/>
<Property name = "maxActive" value = "200"/>
<Property name = "poolPreparedStatements" value = "true"/>
</Bean>
...

Summary & Notes:
1. Messages constructed by the Stomp protocol are non-persistent by default. To ensure message persistence, the Message Header "persistent: true" must be added. This takes me half a day ~
2. Pay attention to the running efficiency of "consumers" during programming, because a large number of "Slow consumers" may encounter problems on non-persistent Topics, especially when there are multiple consumers, it is easy to cause slow ActiveMQ response ~
3. If you do not use the Stomp protocol to establish communication with ActiveMQ, try to use persistent connections. The start () and stop () Methods of Connection are expensive.
4. If you use the cluster master-slave, but it is always unstable, we recommend that you use a single server.

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.