Message persistence in activemq

Source: Internet
Author: User
This article was posted on the Internet. Before we can try it out, let's talk about it first. I do not know if it is original, the URL is as follows http://zhaiyl.spaces.live.com/blog/cns! 939a197d2798715b! 125. Entry
Activemq supports message persistence ). Message persistence is a good method for reliable message transmission. With message persistence, even if the sender and receiver are not online at the same time or the message center is down after the sender sends the message, after the message center is restarted, the message can still be sent out. If this persistence and reliablemessaging are combined, the message can be reliably transmitted.
The message persistence principle is very simple. After the sender sends the message, the message center first stores the message to a local data file, memory database, or remote database, then try to send the message to the receiver. if the message is successfully sent, the message will be deleted from the storage. If the message fails, the attempt will continue. After the message center is started, check the storage location. If a message is not successfully sent, send the message.
For activemq, message persistence is also very simple and can be achieved only through configuration information. Here we mainly introduce two different persistence methods.
  1. High Performance Journal
    This is activemq. Based on the open-source howl (high-speed objectweb logger), it extends the howl to store messages of any size (howl can only store records of a fixed size ), message persistence method. It can quickly store messages in local files, and such files are managed in a database-like way. In this way, if you send 10,000 messages, only a few messages may not be successfully sent. When a checkpoint is reached, journal stores a batch of unsuccessful messages to the database through JDBC, this avoids multiple database operations, greatly improving performance and ensuring reliability.
    The configuration method is very simple, that is, no configuration is required. Activemq supports Journal by default. The following information can be found in the activemq. xml configuration file:

    Code:
    <Persistenceadapter> <br/> <journaledjdbc journallogfiles = "5" journallogfilesize = "1024" datadirectory = "$ {activemq. home}/activemq-Data "/> <br/> </persistenceadapter>
    [Ctrl + A select all]

    Journallogfiles can be changed here. This attribute is used to create several data files by default to store messages. Journallogfilesize is the data file size. The default value is 20 mb. Datadirectory points to the location where data files are stored.

  2. Message persistence using MySQL
    Activemq persists almost all databases (because messages are stored in the database through JDBC ). The method is also simple, that is, the configuration information is slightly changed.

    Code:
    <Persistenceadapter> <br/> <jdbcpersistenceadapter datasource = "# mysql-ds"/> <br/> </persistenceadapter> <br/>
    [Ctrl + A select all]

    Datasource specifies the name of the data source used as MySQL-Ds. You must configure the data source outside the <broker> tag in the activemq. xml file. The following is the configuration information of MySQL.

    Code:
    <Bean id = "mysql-ds" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <br/> <property name =" driverclassname "value =" com. mySQL. JDBC. driver "/> <br/> <property name =" url "value =" JDBC: mysql: // localhost/activemq? Relaxautocommit = true "/> <br/> <property name =" username "value =" gos2 "/> <br/> <property name =" password "value =" gos2" /> <br/> <property name = "poolpreparedstatements" value = "true"/> <br/> </bean> <br/>
    [Ctrl + A select all]

    The meaning of most information is clear. Note that relaxautocommit needs to be set to true and does not know what it means. The configuration information of different databases may be different. You need to check the configuration information yourself.
    After the configuration file is modified, download the JDBC driver package of the selected database and put it under % activemq_home %/lib/. Then start % activemq_home %/bin/activemq. bat.
    Note that activemq uses MySQL to persist messages. You must first create a database. The database name is activemq as shown in the preceding configuration information. After activemq. bat is started, a table is created in the database. When I use mysql4.1, the max key lengh... error. The reason is that the table created by activemq uses a combination of three fields as the primary key. When each field is varchar (250), the total number is 750 varchar, if one verchar2 byte is used (it is said that if UTF-8 encoding is used, it may be 3 bytes ??), Exceeds the 1024 bytes allowed by MySQL. I tried to change the MySQL settings. I did not succeed. I finally manually changed 250 of the SQL statements printed on the console to 100, and created this table, delete other created tables. Restart activemq. bat. Success !!! :)
    In this way, the message center has the message persistence function, and the message sender needs to use the persistent mode in JMS to send messages when sending messages. The sample code is as follows:

    Code:
    <Br/> messageproducer producer = session. createproducer (destination); <br/> producer. setdeliverymode (deliverymode. persistent); <br/>/if you do not want to persist, use the following statement <br/> // producer. setdeliverymode (deliverymode. non_persistent); <br/>
    [Ctrl + A select all]

    In this way, your message can be successfully sent no matter how it is sent.

 

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.