消息队列ActiveMQ的安装与使用

(一)介绍

消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构。是大型分布式系统不可缺少的中间件。目前在生产环境,使用较多的消息队ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ等。
本文主要讲ActiveMQ的安装与简单使用。

(二)安装与启动

首先摆上ActiveMQ官网的安装启动文档http://activemq.apache.org/getting-started.html
我使用的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集群的布置,以后会进行更加详细的纪录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值