ActiveMQ 启动
官方网站 activemq.apache.org
正常启动
**出现问题解决 **
计算机名出现中文
其他启动失败
进入查看消息队列的信息
ActiveMQ 消息队列的入门案例
生产消费模式 是一对一的 一个生产只能一个消费
导入坐标
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.13.4</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
生产者
/**
* Java 程序操作activeMQ的生产模型消费 入门案例 生产者 ()
*/
public class QueueProducerTest {
public static void main(String[] args) throws Exception {
//1 获取连接工厂 (给定activeMQ 的连接地址)
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
//2.从工厂获取连接
Connection connection = factory.createConnection();
//3.启动连接
connection.start();
//4.从连接中创建一个会话 是否支持事务 支持事务的类型
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//5.使用会话创建队列的目的地
Queue queue = session.createQueue("test-queue");
//6.创建消息的生产者对象
MessageProducer producer = session.createProducer(queue);
//7.创建消息内容
TextMessage textMessage = session.createTextMessage("avtiveMQ的入门案例");
//8 发送消息 发送到那个队列中
producer.send(queue,textMessage);
//9 释放资源
producer.close();
session.close();
connection.close();
}
/**
* createSession(paramA,paramB);
paramA 取值有 : true or false 表示是否支持事务
paramB 取值有:Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE,SESSION_TRANSACTED
createSession(paramA,paramB);
paramA是设置事务的,paramB设置acknowledgment mode
paramA设置为false时:paramB的值可为Session.AUTO_ACKNOWLEDGE,Session.CLIENT_ACKNOWLEDGE,DUPS_OK_ACKNOWLEDGE其中一个。
paramA设置为true时:paramB的值忽略, acknowledgment mode被jms服务器设置为SESSION_TRANSACTED 。
Session.AUTO_ACKNOWLEDGE为自动确认,客户端发送和接收消息不需要做额外的工作。
Session.CLIENT_ACKNOWLEDGE为客户端确认。客户端接收到消息后,必须调用javax.jms.Message的acknowledge方法。jms服务器才会删除消息。
DUPS_OK_ACKNOWLEDGE允许副本的确认模式。
一旦接收方应用程序的方法调用从处理消息处返回,会话对象就会确认消息的接收;而且允许重复确认。在需要考虑资源使用时,这种模式非常有效。
*/
}
消费者
/**
* Java 程序操作activeMQ的生产模型消费 入门案例 消费者 ()
*/
public class QueueConsumerTest {
public static void main(String[] args) throws JMSException {
//1.创建连接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
//2.创建连接
Connection connection = factory.createConnection();
//3.启动连接
connection.start();
//4.创建会话对象
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建消息目的地
Queue queue = session.createQueue("test-queue");
//6.创建消息的消费者
MessageConsumer consumer = session.createConsumer(queue);
//7.使用消费者接收消息(两种方式
// 一 固定接收 每次都需要启动云运行
// 二 轮询接收 启动之后无须在处理 采用监听器方式)
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
//在此处接收消息
//由于生产者 发送的消息位 TextMessage 所以 我们这里接收后需要转换一下
try { //有可能出现异常
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
//last end 释放资源 第二种方式不用释放资源
// consumer.close();
// session.close();
// connection.close();
}
}
订阅模式 是一对多的
导入坐标
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.13.4</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
发布者
//订阅 发布者
public class producerTest {
public static void main(String[] args) throws Exception {
//1 获取连接工厂 (给定activeMQ 的连接地址)
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
//2.从工厂获取连接
Connection connection = factory.createConnection();
//3.启动连接
connection.start();
//4.从连接中创建一个会话 是否支持事务 支持事务的类型
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//5.使用会话创建队列的目的地,创建的是发布订阅模型
Topic topic = session.createTopic("test-topic");
//6.创建消息的生产者对象
MessageProducer producer = session.createProducer(topic);
//7.创建消息内容
TextMessage textMessage = session.createTextMessage("avtiveMQ topic的入门案例");
//8 发送消息 发送到那个队列中
producer.send(topic,textMessage);
//9 释放资源
producer.close();
session.close();
connection.close();
}
}
订阅者
public static void main(String[] args) throws Exception {
//1.创建连接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
//2.创建连接
Connection connection = factory.createConnection();
//3.启动连接
connection.start();
//4.创建会话对象
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建消息目的地
Topic topic = session.createTopic("test-topic");
//6.创建消息的消费者
MessageConsumer consumer = session.createConsumer(topic);
//7.使用消费者接收消息(两种方式
// 一 固定接收 每次都需要启动云运行
// 二 轮询接收 启动之后无须在处理 采用监听器方式)
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
//在此处接收消息
//由于生产者 发送的消息位 TextMessage 所以 我们这里接收后需要转换一下
try { //有可能出现异常
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
//last end 释放资源 第二种方式不用释放资源
// consumer.close();
// session.close();
// connection.close();
}
}
spring 与 ActiveMQ 整合
导入坐标
<!-- activemq start -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.11.2</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<!-- activemq end -->
<!-- spring 与 mq整合 start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.13</version>
</dependency>
<!-- spring 与 mq整合 end -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
配置 applicationContext-mq.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<!--配置连接工厂:ActiveMQ的连接工厂-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin"></amq:connectionFactory>
<!--配置spring支持会话缓存的连接工厂-->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!--注入提供商的连接工厂-->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!--设置Session缓存的大小-->
<property name="sessionCacheSize" value="100"></property>
</bean>
<!--配置spring提供的jms模板-->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!--注入连接工厂,是spring的-->
<property name="connectionFactory" ref="connectionFactory"></property>
<!--指定是否发布订阅模型-->
<property name="pubSubDomain" value="false"></property>
</bean>
<!--配置spring提供的jms模板-->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!--注入连接工厂,是spring的-->
<property name="connectionFactory" ref="connectionFactory"></property>
<!--指定是否发布订阅模型-->
<property name="pubSubDomain" value="true"></property>
</bean>
</beans>
配置 applicationContext-listener.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<!--配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.itheima.listener"></context:component-scan>
<!--配置连接工厂:ActiveMQ的连接工厂-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin"></amq:connectionFactory>
<!--配置spring支持会话缓存的连接工厂-->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!--注入提供商的连接工厂-->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!--设置Session缓存的大小-->
<property name="sessionCacheSize" value="100"></property>
</bean>
<!--配置生产消费模型的监听器-->
<jms:listener-container destination-type="queue">
<jms:listener destination="spring-queue" ref="queueListener"></jms:listener>
</jms:listener-container>
<!--配置发布订阅模型的监听器-->
<jms:listener-container destination-type="topic">
<jms:listener destination="spring-topic" ref="topicListener"></jms:listener>
</jms:listener-container>
</beans>
java QueueListener
@Component
public class QueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
MapMessage message1 = (MapMessage) message;
try {
String name = message1.getString("name");
String age = message1.getString("age");
System.out.println(name + "-消费者--" + age);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
java TopicListener
@Component
public class QueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
MapMessage message1 = (MapMessage) message;
try {
String name = message1.getString("name");
String age = message1.getString("age");
System.out.println(name + "-消费者--" + age);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
//消费者不能停 所以只能使用main方法了
public class SpringActivemqConsumerTest {
//消费者不能停 所以只能使用main方法了
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-listener.xml");
context.start();
}
}
发布者 和 订阅者
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-mq.xml")
public class SpringActivemqTest {
@Autowired
private JmsTemplate jmsQueueTemplate;
@Autowired
private JmsTemplate jmsTopicTemplate;
@Test
public void testjmsQueueTemplate(){
jmsQueueTemplate.send("spring-queue", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("name","达达");
mapMessage.setString("age","22");
return mapMessage;
}
});
}
@Test
public void testjmsTopicTemplate() {
jmsTopicTemplate.send("spring-topic", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("name","达达");
mapMessage.setString("age","22");
return mapMessage;
}
});
}
}