首先是对一些基本概念的一个理解
可以参考以下文章:
【参考】深入浅出JMS(一)–JMS基本概念
【参考】深入浅出JMS(二)–ActiveMQ简单介绍以及安装
【参考】Sping+ActiveMQ整合
【参考】activemq持久化ActiveMQ的简单实现
【参考网上的一篇文章,但是由于当时看的比较多,没有记下文章的url,不好意思】
• 新建一个Java Project
• 引入activeMQ下面的一些jar包 —- apache-activemq-5.14.4- bin/apache-activemq-5.14.4/lib
• Java Project 的项目结构• 生产者代码
package com.lfj.activemq;
/**
* 生产者代码
*/
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
private static final int SEND_NUMBER = 10;
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS
// Provider 的连接
Connection connection = null; // Session: 一个发送或接收消息的线程
Session session; // Destination :消息的目的地;消息发送给谁.
Destination destination; // MessageProducer:消息发送者
MessageProducer producer; // TextMessage message;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
try { // 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session.createTextMessage("ActiveMq 发送的消息"
+ i);
// 发送消息到目的地方
System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
}
}
}
• 消费者者代码
package com.lfj.activemq;
/**
* 消费者代码
*/
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
while (true) {
// 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
3.Spring+ActiveMQ整合的实例
- 这里面从gitHub上下载了一个项目Spring+activeMQ简单整合
- 项目下下来,一开始并不能运行,我修改了一些东西,将父工程里的pom.xml文件修改了
(1) 私服换成了我本地的
<repositories>
<repository>
<id>local-nexus</id><name>nexus respository</name><url>http://maven.dev.yizhangtong.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots>
</repositories>
(2) build标签里的关于maven的compiler和install的插件进行了修改,还设置了一个默认的全局命令
<!-- 默认配置了一个全局命令,否则No goals have been specified for this build -->
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
<optimise>true</optimise>
<debug>true</debug>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<id>attach-source</id>
<phase>install</phase>
</execution>
</executions>
</plugin>
</plugins>
(3) activemq-client模块中的activemq_config.xml中加了几个监听的bean
<bean id="userPushListener" class="com.lwl.activemq.listener.UserPushListener"></bean>
<bean id="newsPushListener" class="com.lwl.activemq.listener.NewsPushListener"></bean>
<bean id="clientPushListener" class="com.lwl.activemq.listener.ClientPushListener"></bean>
(4) activemq-client模块中的log4j.xml中涉及日志目录的都修改一下
<param name="File" value="D:/log/lwl_activemq_client/lwl_activemq_client.log" />
先在tomcat上运行activemq-service,然后访问 http://localhost:8080/activemq-service/index.html
然后再运行activemq-client,消息就可以被消费了