1.先去下一个mysql的JAR包,放在activemq的lib目录下
配置activemq.xml
1、修改persistenceAdapter
<persistenceAdapter> <!-- <kahaDB directory="${activemq.data}/kahadb"/>--> <jdbcPersistenceAdapter dataSource="#my-ds"/> </persistenceAdapter>
上面我们注释了默认的kahadb,添加了jdbc数据源。
2、增加数据源
<bean id="my-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.2.140:3306/activemq?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> <property name="initialSize" value="5" /> <property name="maxTotal" value="100" /> <property name="maxIdle" value="30" /> <property name="maxWaitMillis" value="10000" /> <property name="minIdle" value="1" /> </bean>
在mysql里建一个activemq库,数据库需要字符集设置为latin1。
启动activemq之后,会自动往库里插入三张表。
package com.ckl.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class HelloActiveMQ {
public static void main(String[] args) throws Exception {
HelloWorldProducer producer = new HelloWorldProducer();
HelloWorldConsumer consumer = new HelloWorldConsumer();
Thread threadProducer = new Thread(producer);
threadProducer.start();
//注释掉消费者,不然的话,马上消费者马上把消息消费了,来不及持久化
// Thread threadConsumer = new Thread(consumer);
// threadConsumer.start();
}
public static class HelloWorldProducer implements Runnable {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.2.140:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("DemoQueue");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
// producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);//消息需要持久化
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
public static class HelloWorldConsumer implements Runnable, ExceptionListener {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.2.140:61616");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("DemoQueue");
// Create a MessageConsumer from the Session to the Topic or
// Queue
MessageConsumer consumer = session.createConsumer(destination);
// Wait for a message
Message message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
public synchronized void onException(JMSException ex) {
System.out.println("JMS Exception occured. Shutting down client.");
}
}
}
运行即可,插入动作要在发送之前,不然会被先消费掉。
另外,关于DeliveryMode参数,non persistent参数会在MQ重启之后失效。如果想在MQ启动之后,消息还可以发送到消费端,就需要设成Persistent