ActiveMQ简单简绍(“点对点通讯”和 “发布订阅模式”)
点对点通讯(“代码”)
生产者:
public static void main(String[] args) throws JMSException {
//步骤一:创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//步骤二:创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//步骤三:启动连接
connection.start();
//步骤四:获取会话工厂
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//步骤五:创建队列
Queue queue = session.createQueue("wdksoft_queue");
//创建消息生产者
MessageProducer producer = session.createProducer(queue);
//消息持久化
producer.setDeliveryMode(2);
//模拟消息
TextMessage textMessage = session.createTextMessage("hello activeMQ");
//发送消息
producer.send(textMessage);
System.out.println("生产者生产消息完毕~");
//回收资源
session.close();
connection.close();
}
消费者:
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(false, Session.AUTO_ACKNOWLEDGE);
//获取到接受消息的队列
Queue queue = session.createQueue("wdksoft_queue");
//创建消费者
MessageConsumer consumer = session.createConsumer(queue);
while(true){
//获取消息
TextMessage message = (TextMessage)consumer.receive();
if(message!=null){
System.out.println("消费者获取消息:"+message.getText());
}else{
break;
}
}
//回收资源
session.close();
connection.close();
}
消费者:
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(false, Session.AUTO_ACKNOWLEDGE);
//获取到接受消息的队列
Topic topic = session.createTopic("wdksoft_topic");
//创建消费者
MessageConsumer consumer = session.createConsumer(topic);
while(true){
//获取消息
TextMessage message = (TextMessage)consumer.receive();
if(message!=null){
System.out.println("消费者获取消息:"+message.getText());
}else{
break;
}
}
//回收资源
session.close();
connection.close();
}
生产者:
public static void main(String[] args) throws JMSException {
//步骤一:创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
//步骤二:创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//步骤三:启动连接
connection.start();
//步骤四:获取会话工厂
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//步骤五:创建主题
Topic topic = session.createTopic("wdksoft_topic");
//创建消息生产者
MessageProducer producer = session.createProducer(null);
//消息持久化
producer.setDeliveryMode(2);
//模拟消息
TextMessage textMessage = session.createTextMessage("hello activeMQ pub");
//发送消息
producer.send(topic,textMessage);
System.out.println("生产者生产消息完毕~");
//回收资源
session.close();
connection.close();
}