ACTIVEMQ persistent to MySQL implementation message never loses configuration
1. Locate the activemq-jdbc-performance.xml below apache-activemq-5.15.2/examples/conf
2. Open Activemq-jdbc-performance.xml, add datasource= "#mysql-ds" after the Persistenceadapter node
and configure your database
In fact, you can change the Persistenceadapter node of Apache-activemq-5.15.2/conf/activemq.xml directly. The database can also be configured under
With Activemq-jdbc-performance.xml no localhost:8161 management page, and only with Openwire transport protocol, the default is open, Transportconnectors node is the transmission protocol
3. Copy the Activemq-jdbc-performance.xml to the apache-activemq-5.15.2/conf directory, and from the name Activemq.xml, overwrite the original activemq.xml
4. Create the ACTIVEMQ library in the corresponding database and restart the ACTIVEMQ
Here we start with debug mode, prompting for a jar without MySQL
5. We add the MySQL jar package below the Apache-activemq-5.15.2/lib, start again, will not error
6. You can see that the ACTIVEMQ library you just created has three more tables indicating that the configuration was successful.
Point-to-point test producer
import javax.jms.Connection;import javax.jms.ConnectionFactory;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 classProducer { Public Static void Main(string[] args) {//String user = Activemqconnection.default_user;//String password = Activemqconnection.default_password;//String URL = activemqconnection.default_broker_url;String subject ="Test.queue"; ConnectionFactory contectionfactory =New activemqconnectionfactory("tcp://192.168.1.109:61616");//ConnectionFactory contectionfactory = new Activemqconnectionfactory ("tcp://127.0.0.1:61616"); Try{Connection Connection = contectionfactory.createconnection(); Connection.Start(); Session session = connection.createsession(Boolean.TRUE, Session.Auto_acknowledge); Destination Destination = session.Createqueue(subject); MessageProducer producer = Session.Createproducer(destination);//Producer.setdeliverymode (deliverymode.persistent);//set to persistent for(inti =0; I < -;) {TextMessage Createtextmessage = session.Createtextmessage("This is the first to be sent"+ ++i +"message Message"); Producer.Send(Createtextmessage); System. out.println("section"+ i +"message Sent"); } Thread.Sleep( -); Session.Commit(); Session.Close(); Connection.Close(); }Catch(JMSException e) {//E.printstacktrace ();}Catch(Interruptedexception e) {//E.printstacktrace ();} }}
Consumers
import java.util.Date;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import Javax.jms.MessageConsumer;import Javax.jms.MessageListener;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory; Public classCustomer { Public Static void Main(string[] args) {//String user = Activemqconnection.default_user;////String password = Activemqconnection.default_password;////String URL = activemqconnection.default_broker_url;String subject ="Test.queue"; ConnectionFactory ConnectionFactory =New activemqconnectionfactory("tcp://192.168.1.109:61616");//ConnectionFactory connectionfactory = new Activemqconnectionfactory ("tcp://127.0.0.1:61616");Connection Connection;Try{connection= connectionfactory.createconnection(); Connection.Start();FinalSession session =connection.createsession(Boolean.TRUE, Session.Auto_acknowledge); Destination Destination = session.Createqueue(subject); Messageconsumer message = session.Createconsumer(destination); Message.Setmessagelistener(New MessageListener() { Public void OnMessage(Message msg) {TextMessage message = (textmessage) msg;Try{System. out.println("--Received the message:"+NewDate () +message.GetText()); Session.Commit(); }Catch(JMSException e) {//E.printstacktrace ();} } });//Thread.Sleep (30000);////Session.close ();////Thread.Sleep (30000);////Connection.close ();////Thread.Sleep (30000);}Catch(Exception e) {//E.printstacktrace ();} }}
At this time producer production data, consumers have been not online, the data will be persisted to the database of the ACTIVEMQ_MSGS table, even if the ACTIVEMQ service hangs, once again started, and so on consumers online can again obtain producer production data (after consumption database data will be automatically deleted), Achieve a no-loss effect
ACTIVEMQ persistent to MySQL implementation message never lost