这是我参与2022首次更文挑战的第21天,活动详情查看:2022首次更文挑战
###(一)介绍 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构。是大型分布式系统不可缺少的中间件。目前在生产环境,使用较多的消息队ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ等。 本文主要讲ActiveMQ的安装与简单使用。 ###(二)安装与启动 首先摆上ActiveMQ官网的安装启动文档activemq.apache.org/getting-sta… 我使用的mq是5.15.0版本,系统是centos7.3(某**云服务器最低配)。 官网教程中给的连接竟然404了,于是手动在官网下载linux版本的在上传到服务器;然后解压到某个目录下(我是解压到/opt目录下了),接着 cd /opt/apache-activemq-5.15.0/bin/ 执行./activemq start 命令就启动ActiveMQ了
(三)Java客户端使用
在mq下载的压缩包里有有一个jar包activemq-all-5.15.0.jar,将个jar包加载到项目中即可。ActiveMQ符合JMS(Java Message Service)规范,其主要提供了两种服务,一种是点对点的消息传输(queue),一种是一对多的消息传输(topic)下面将给出这两种方式的生产者和消费者代码示例。 ####(3.1)点对点
/**
*
* @author yasin
* @TODO Queue producer
*/
public class Producer {
public static String URL = "tcp://127.0.0.1:61616";//mq服务地址,自己的公网ip就不暴露了
public static void main(String[] args) {
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,URL);
Connection connection = null;
try {
connection= factory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("MessageQueue");//创建一个queue,并且命名为MessageQueue,消费方凭借这个名字进行匹配消费
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);//设置消息模式,此处为不持久,就没有消费者订阅,则消息不保存
ObjectMessage message = session.createObjectMessage("hello everyone");//创建发布的消息
producer.send(message);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
}finally{
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
/**
*
* @author yasin
* @TODO Queue consumer
*
*/
public class Consumer {
public static String URL = "tcp://127.0.0.1:61616";//mq服务地址
public static void main(String[] args){
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,URL);
try {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("MessageQueue");
MessageConsumer consumer = session.createConsumer(destination);
while(true){
ObjectMessage message = (ObjectMessage) consumer.receive(10000);
if(message!=null){
String messageContent = (String) message.getObject();
System.out.println(messageContent);
}else{
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
(3.2)一对多(topic)
/**
*
* @author yasin
* @TODO Topic Producer
*/
public class Producer {
public static String URL = "tcp://127.0.0.1:61616";//mq服务地址
public static void main(String[] args){
ConnectionFactory factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
URL);
try {
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("MessageTopic");
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = session.createTextMessage();
message.setText("message_hello_yasin");
producer.send(message);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
/**
*
* @author yasin
* @TODO Topic consumer
*
*/
public class Consumer {
public static String URL = "tcp://127.0.0.1:61616";//mq服务地址
public static void main(String[] args){
ConnectionFactory factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
URL
);
try {
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("MessageTopic");
MessageConsumer consumer = session.createConsumer(topic);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage tm = (TextMessage) msg;
try {
System.out.println(tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
MessageConsumer consumer1 = session.createConsumer(topic);//第二个消费者
consumer1.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage tm = (TextMessage) msg;
try {
System.out.println("1::"+tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
} catch (JMSException e) {
e.printStackTrace();
}
}
}
###(四)activemq数据的显示 activemq内置了数据监控系统,是运行在Jetty上的网页,可通过浏览器直接访问,地址 ip:8161/admin 初始账号密码都是admin 点击导航栏的queues就可查看所有发送的点对点的消息历史及状态,topics是一对多。
本文只是一个简单的安装和使用介绍,在生产环境中还需要更加详细的配置爱以及mq集群的布置,以后会进行更加详细的纪录