1. Place the jar file connected to the MySQL database in the activemq Lib directory
2, modify the ACTIVEMQ conf directory active.xml file, modify the way data persisted
2.1 How to modify the persisted data of the original KSHADB
<persistenceAdapter> <!-- <kahadb directory= "${activemq.data}/kahadb"/>-- < Jdbcpersistenceadapter datasource= "#mysql-ds"/> </persistenceAdapter>
2.2 Connect the configuration of MySQL (note the location of the configuration file placement)
<!--for persisting data to MySQL database-- class= "Org.apache.commons.dbcp.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=" 1234 "/> <property name=" maxactive "value=" $ "/> <property name= "poolpreparedstatements" value= "true"/>
3. Persist data to MySQL operation
3.1 Restart Activemq, and run the program, put persistent data, view the active database of MySQL
4. Add code to persistent data
Importjavax.jms.Connection; Importjavax.jms.ConnectionFactory; ImportJavax.jms.DeliveryMode; Importjavax.jms.Destination; ImportJavax.jms.MessageProducer; Importjavax.jms.Session; ImportJavax.jms.TextMessage; Importorg.apache.activemq.ActiveMQConnectionFactory; Public classSender { Public Static voidMain (string[] args)throwsException {//1, establish connectionfactory factory object, need to fill in the user name, password, and the address of the connection//use only the default. The port number is "TCP://localhost:61616 "ConnectionFactory ConnectionFactory =NewActivemqconnectionfactory ("Zhangsan",//Activemqconnectionfactory.default_user,"123",//Activemqconnectionfactory.default_password,"tcp://localhost:61616"); //2. Create a connection connection from the ConnectionFactory factory object//and call connection's Start method to open the connection, connection default is not openConnection Connection =connectionfactory.createconnection (); Connection.start (); //3. Create session sessions through connection objects (contextual objects),//parameter one, indicating whether the transaction is turned on//parameter two, indicating the signature mode, generally used with automatic sign-off and the client's own confirmation sign//The first parameter is set to True, which indicates that the transaction is turned on//when you open a transaction, remember to manually commit the transactionSession Session=connection.createsession (Boolean.true, Session.client_acknowledge); //4. Create a destination object from the session, which refers to the object that the client uses to specify the production message destination and consumer message source. //In PTP mode, destination refers to the queue//In the Publish subscription model, destination refers to the topicDestination Destination = Session.createqueue ("queue1"); //5. Use session to create a message object producer or consumerMessageProducer MessageProducer =session.createproducer (destination); //6. If yes, the producer, using the MessageProducer Setdelivermode method setting, the persistence and non-persistence of messagesMessageproducer.setdeliverymode (deliverymode.persistent); //7. Finally create data using the TextMessage form of the JMS specification (via Session object)//and send the data using MessageProducer's Send method for(inti = 0; I < 5; i++) {TextMessage TextMessage=Session.createtextmessage (); Textmessage.settext ("I am the message" +i); Messageproducer.send (TextMessage); } //manually commit an open transactionSession.commit (); //Release Connection if(Connection! =NULL) {connection.close (); } } }
JMS Learning Eight (ACTIVEMQ messages persisted to MySQL database)