1. 基本配置
其基本配置需要更改的有两块内容。其意就是配置activemq.xml文件,再者就是配置mysql数据库。
首先,进入activemq安装目录,在conf文件夹下打开activemq.xml文件,修改一下配置:
(1) 默认使用的是kahadb,如下:
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/></persistenceAdapter>
改成:
<persistenceAdapter>
<jdbcPersistenceAdapter dataSource="#derby-ds"/></persistenceAdapter>
(2)在</broker>标签外面添加:
<bean id="derby-ds" 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/test_mq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
在配置好以上内容之后,activemq.xml文件就已经配置完成了。下面开始配置mysql数据库。
打开mysql数据库,然后根据activemq.xml文件中配置的:
<property name="url" value="jdbc:mysql://localhost:3306/test_mq?relaxAutoCommit=true"/>
创建mysql数据库test_mq。完成后将jdbc 的驱动,放在安装activemq的目录中lib文件夹下。至此 所有的基本操作完成。
2. 实例操作
编写生产者:
public class Sender {
public static void main(String[] args) throws Exception{
//第一步:建立ConnectionFactory工厂对象,需要填入用户名、密码,以及要连接的地址,均使用默认即可,默认端口为:"tcp://localhost:61616"
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_USER,
ActiveMQConnectionFactory.DEFAULT_PASSWORD,
"tcp://localhost:61616");
//第二步:通过ConnectionFactory工厂对象,我们创阿金一个Connection连接,并且采用Connection的start方法开启连接,Connection默认是关闭的
Connection connection = (Connection) connectionFactory.createConnection();
connection.start();
//第三步:通过Connection对象创建Session会话,用于接收消息,参数配置1是事务,参数配置2是签收模式,一般我们设置自动签收
// Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
// 使用事务的方式进行消息的发送
// Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//使用Client端签收的方式
Session session = connection.createSession(Boolean.TRUE, Session.CLIENT_ACKNOWLEDGE);
//第四步:通过session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费消息来源的对象,在PTP模板中Destination被称作Topic主题。在程序中可以使用多个Queue和Topic
Destination destination = session.createQueue("queue1");
//第五步:通过session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer、MessageConsumer
MessageProducer messageProducer = session.createProducer(null);
//第六步:使用MessageProducer的setSelveryMode方法为其设置持久化特性和非持久化特性(DeliveryMode)
// messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//第七步:最后我们使用JMS规范的TextMessage形式创建数据(通过Session对象),并用MessageProducer的send方法发送数据,同理客户端使用receive方法进行接收数据。最后不要忘记关机Connection连接
for (int i = 0; i < 5; i++) {
TextMessage textMessage = session.createTextMessage();
textMessage.setText("我是消息内容,id为:"+i);
// 第一个参数:目的地
// 第二个参数:消息
// 第三个参数:是否持久化
// 第四个参数:优先级
// 第五个参数:消息在jms上面的存放时间
messageProducer.send(destination, textMessage,DeliveryMode.PERSISTENT, i, 1000*60);
System.out.println("生产者:" + textMessage.getText());
}
//使用事务提交
session.commit();
if (connection != null) {
connection.close();
}
}
注意上面代码:
messageProducer.send(destination, textMessage,DeliveryMode.PERSISTENT, i, 1000*60);
中的DeliveryMode.PERSISTENT属性。
执行代码,然后在数据库中进行查看。我们会看到在之前创立的test_mq下面有了三张表:
然后,打开msgs表会看到一下的消息信息:
至此,生产者端生成消息队列的工作就完成了。