ActiveMQ Persistence Configuration
Modify configuration file Activemq.xml
Modify Persistenceadapter
The following section is configured to comment out
<persistenceAdapter>
<kahadb directory= "${activemq.data}/kahadb"/>
</persistenceadapter >
Then update to the following configuration:
<persistenceAdapter>
<jdbcpersistenceadapter datasource= "#mysql-ds"/>
</ Persistenceadapter>
To define a data source bean
Outside of the broker node, the definition ID is mysql-ds bean, as follows:
<bean id= "Mysql-ds" class= "Org.apache.commons.dbcp2.BasicDataSource" destroy-method= "Close" >
< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name=
"url" value= "jdbc:mysql:/ /localhost:3306/activemq?relaxautocommit=true "/> <property name=" username "value=" "
root"/>
< Property name= "Password" value= "root"/> <property name= "poolpreparedstatements" value=
"true"/>
Creating Data
Create a database in MySQL named Activemq, and set the encoding to be UTF-8. Restart Activemq Modify Code
Set the persistence mode Sender.setdeliverymode (Deliverymode.persistent) on the producer, and the complete code is as follows:
Import Javax.jms.DeliveryMode;
Import Javax.jms.MapMessage;
Import Javax.jms.Queue;
Import javax.jms.QueueConnection;
Import Javax.jms.QueueConnectionFactory;
Import javax.jms.QueueSession;
Import javax.jms.Session;
Import org.apache.activemq.ActiveMQConnection;
Import Org.apache.activemq.ActiveMQConnectionFactory;
/** * Queue Mode message sender/public class Queuesender {//Send count public static final int send_num = 5;
The TCP address public static final String Broker_url = "tcp://localhost:61616"; Target, create http://localhost:8161/admin/queues.jsp public static final String destination = "Hoo.mq.persis in the ACTIVEMQ Administrator console
Tence "; /** * <b>function:</b> Send Message * * @author Hoojo * @createDate 2013-6-19 12:05:42 * param session * @param sender * @throws Exception/public static void SendMessage (Queuesession sessio
N, Javax.jms.QueueSender Sender) throws Exception {for (int i = 0; i < Send_num; i++) { String message = "Send messages to the first" + (i + 1) + "bar";
Mapmessage map = Session.createmapmessage ();
Map.setstring ("text", message);
Map.setlong ("Time", System.currenttimemillis ());
SYSTEM.OUT.PRINTLN (map);
Sender.send (map);
} public static void Run () throws Exception {queueconnection connection = null;
Queuesession session = NULL; try {//Create link factory queueconnectionfactory factory = new Activemqconnectionfactory (activemqconnection.
Default_user, Activemqconnection.default_password, Broker_url);
Create a connection through the factory connection = Factory.createqueueconnection ();
Start Connection Connection.start ();
Create a sessions session = Connection.createqueuesession (Boolean.true, Session.auto_acknowledge);
Create a message queue queue queue = Session.createqueue (destination); Create a message sender
Javax.jms.QueueSender sender = Session.createsender (queue);
Set the persistence mode Sender.setdeliverymode (deliverymode.persistent);
SendMessage (session, Sender);
Submit session session.commit ();
catch (Exception e) {throw e;
finally {//Shutdown release Resource if (session!= null) {session.close ();
} if (connection!= null) {connection.close ();
}} public static void Main (string[] args) throws Exception {Queuesender.run (); }
}
Consumer code does not need to be modified:
Import javax.jms.JMSException;
Import Javax.jms.MapMessage;
Import Javax.jms.Message;
Import Javax.jms.MessageListener;
Import Javax.jms.Queue;
Import javax.jms.QueueConnection;
Import Javax.jms.QueueConnectionFactory;
Import javax.jms.QueueSession;
Import javax.jms.Session;
Import org.apache.activemq.ActiveMQConnection;
Import Org.apache.activemq.ActiveMQConnectionFactory; /** * Message Receiver */public class QueueReceiver {//TCP address public static final String Broker_url = "tcp://localhost:
61616 "; Target, create http://localhost:8161/admin/queues.jsp public static final String target = "hoo.mq.persistence" in the ACTIVEMQ Administrator console
";
public static void Run () throws Exception {queueconnection connection = null;
Queuesession session = NULL; try {//Create link factory queueconnectionfactory factory = new Activemqconnectionfactory (activemqconnection.
Default_user, Activemqconnection.default_password, Broker_url); Through the factoryCreate a connection connection = Factory.createqueueconnection ();
Start Connection Connection.start ();
Create a sessions session = Connection.createqueuesession (Boolean.true, Session.auto_acknowledge);
Create a message queue queue queue = Session.createqueue (TARGET);
Create message producer Javax.jms.QueueReceiver receiver = session.createreceiver (queue);
Receiver.setmessagelistener (New MessageListener () {public void OnMessage (msg) {
if (msg!= null) {mapmessage map = (mapmessage) msg;
try {System.out.println (Map.getlong ("time") + "Receive #" + map.getstring ("text"));
catch (JMSException e) {e.printstacktrace ();
}
}
}
}); Hibernate 100ms again close Thread.Sleep (1000 * 3);
Submit session, Session.commit ();
catch (Exception e) {throw e;
finally {//Shutdown release Resource if (session!= null) {session.close ();
} if (connection!= null) {connection.close ();
}} public static void Main (string[] args) throws Exception {Queuereceiver.run (); }
}
Test
ACTIVEMQ persistence is produced by the producers of data, in the absence of consumer consumption, will first be guaranteed to the database, when the data is consumed by consumers, will be deleted from the database.
Basically the above principle, when testing, from running the producer's program, and then seeing in the database ACTIVEMQ automatically generates three sheets: Activemq_acks, Activemq_lock, Activemq_msgs, in Activemq_ The production data is seen in the Msgs table.
Then run the consumer-side program and the data in the Activemq_msgs table disappears after the execution completes. error for persistent configuration Java.lang.ClassNotFoundException:org.apache.commons.dbcp.BasicDataSource
After changing the ActiveMQ to MySQL configuration, the startup times are wrong. The main thing is to find Org.apache.commons.dbcp.BasicDataSource this class.
Through the Internet to find information, Basicdatasource located in Commons-dbcp2-xx.jar, (located in the Activemq lib/optional package);
The reason for the error is ACTIVEMQ configuration MySQL data source, to the class writing Org.apache.commons.dbcp2.BasicDataSource.
The configuration is as follows: