package com.demo.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
public class JmsConsumer {
private static final String ACTIVEMQ_URL = "tcp://192.168.56.10:61616";
private static final String QUEUE_NAME = "queue01";
//异常抛到外面
public static void main(String[] args) throws JMSException {
//1.创建连接工厂,按照给定的URL,采用默认的用户名密码
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工厂,获得connection并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
//3.创建会话session
//两个参数transacted=事务,acknowledgeMode=确认模式(签收)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列queue还是主题topic)
Queue queue = session.createQueue(QUEUE_NAME);
//5.创建消息的消费者,指定消费哪一个队列里面的消息
MessageConsumer messageConsumer = session.createConsumer(queue);
//循环获取
while (true) {
//6.通过消费者调用方法获取队列里面的消息(发送的消息是什么类型,接收的时候就强转成什么类型)receive 等待 一一对应
TextMessage textMessage = (TextMessage) messageConsumer.receive();
if (textMessage != null) {
System.out.println("****消费者接收到的消息: " + textMessage.getText());
} else {
break;
}
}
//关闭资源 顺着申请 倒着关闭
messageConsumer.close();
session.close();
connection.close();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fb6a0a2538e25e9a4906d4d5c72335f3.png)
package com.demo.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
public class JmsConsumer {
private static final String ACTIVEMQ_URL = "tcp://192.168.56.10:61616";
private static final String QUEUE_NAME = "queue01";
//异常抛到外面
public static void main(String[] args) throws JMSException {
//1.创建连接工厂,按照给定的URL,采用默认的用户名密码
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工厂,获得connection并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
//3.创建会话session
//两个参数transacted=事务,acknowledgeMode=确认模式(签收)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列queue还是主题topic)
Queue queue = session.createQueue(QUEUE_NAME);
//5.创建消息的消费者,指定消费哪一个队列里面的消息
MessageConsumer messageConsumer = session.createConsumer(queue);
//循环获取
while (true) {
//6.通过消费者调用方法获取队列里面的消息(发送的消息是什么类型,接收的时候就强转成什么类型)receive 等待 一一 对应
//TextMessage textMessage = (TextMessage) messageConsumer.receive(); 在资源充足情况下可以用
TextMessage textMessage = (TextMessage) messageConsumer.receive(4000L); // 加时间 过时不候
if (textMessage != null) {
System.out.println("****消费者接收到的消息: " + textMessage.getText());
} else {
break;
}
}
//关闭资源 顺着申请 倒着关闭
messageConsumer.close();
session.close();
connection.close();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/55af62483065e897e301c637c9355088.png)