环境准备:Linux上安装好了activemq并启动。
1、创建一个普通的maven工程,导入依赖:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.11.2</version>
</dependency>
2、创建生产者(点对点模式):
package com.soft.producter;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by Administrator on 2020/9/24 0024.
*/
public class producter01 {
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.25.128:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建会话
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 创建队列
Queue queue_01 = session.createQueue("queue_01");
// 创建生产者
MessageProducer producer = session.createProducer(queue_01);
// 创建消息
TextMessage textMessage = session.createTextMessage("hello queue_01");
// 发送消息
producer.send(textMessage);
// 提交事务
session.commit();
// 释放资源
session.close();
connection.close();
}
}
3、创建消费者(点对点模式):
package com.soft.consumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
/**
* Created by Administrator on 2020/9/24 0024.
*/
public class Consumer01 {
public static void main(String[] args) throws JMSException, IOException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.25.128:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建会话
final Session session = connection.createSession(true, 1);
// 创建队列
Queue queue_01 = session.createQueue("queue_01");
// 创建消费者
MessageConsumer consumer = session.createConsumer(queue_01);
//监听queue_01
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
// 提交事务
session.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
});
//线程阻塞
System.in.read();
// 释放资源
session.close();
connection.close();
}
}
4、测试结果(点对点模式):
5、生产者(发布订阅模式入门代码):
package com.soft.producter;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by Administrator on 2020/9/24 0024.
*/
public class Producter02 {
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.25.128:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建会话
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建Topic
Topic topic_01 = session.createTopic("topic_01");
// 创建生产者
MessageProducer producer = session.createProducer(topic_01);
// 创建消息
TextMessage textMessage = session.createTextMessage("hello topic_01");
// 发送消息
producer.send(textMessage);
// 提交事务
session.commit();
// 释放资源
session.close();
connection.close();
}
}
6、消费者(发布订阅模式入门代码):
package com.soft.consumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
/**
* Created by Administrator on 2020/9/24 0024.
*/
public class Consumer02 {
public static void main(String[] args) throws JMSException, IOException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.25.128:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建会话
final Session session = connection.createSession(true, 1);
//创建Topic
Topic topic_01 = session.createTopic("topic_01");
// 创建消费者
MessageConsumer consumer = session.createConsumer(topic_01);
//监听queue_01
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
// 提交事务
session.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
});
//线程阻塞
System.in.read();
// 释放资源
session.close();
connection.close();
}
}
7、测试结果: