/**
* P2P消费者
*/
public class P2P_Consumer {
public static void main(String[] args) throws JMSException {
//步骤一:创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
//步骤二:创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//步骤三:启动连接
connection.start();
//步骤四:获取会话工厂
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//步骤五:创建队列
Queue quque = session.createQueue("MyQueue");
//创建消费者
MessageConsumer consumer = session.createConsumer(quque);
//循环获取消息
while (true){
TextMessage message = (TextMessage)consumer.receive();
session.commit();
if (message!=null){
System.out.println("消费者获取信息:"+message.getText());
}else {
break;
}
}
//回收资源
session.close();
connection.close();
}
}
②手动签收
消费者必须显示调用手动签收的方法进行签收,否则队列当中还是存在数据