美文网首页
Java消息中间件-ActiveMQ-入门实战

Java消息中间件-ActiveMQ-入门实战

作者: 任重而道元 | 来源:发表于2017-08-26 15:21 被阅读95次

一、Window下载ActiveMQ

1. 打开网站

http://activemq.apache.org

官网

2. 安装,启动

安装 启动 启动页面

二、队列模式消息

1. JMS编码接口之间的关系

JMS编码接口之间的关系

2.代码实战

  1. 添加maven,最新的版本可以去官网找
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.9.0</version>
</dependency>
  1. 编写生产者代码
package market.daoge.active.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 生产者
 * Created by Lidy on 2017/8/26 0026.
 */
public class AppProducer {

    private static final String url = "tcp://localhost:61616";
    private static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException {

        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.创建连接
        Connection connection = connectionFactory.createConnection();
        // 3.启动连接
        connection.start();
        // 4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.创建一个目标
        Destination destination = session.createQueue(queueName);
        // 6.创建一个生产者
        MessageProducer producer = session.createProducer(destination);

        for(int i=0;i<5;i++) {
            // 7.创建消息
            TextMessage textMessage = session.createTextMessage("text" + i);
            // 8.发布消息
            producer.send(textMessage);
            System.out.println("发送消息" + textMessage.getText());
        }
        // 9.关闭连接
        connection.close();
    }
}

  1. 登录后台查看,用户名和密码默认为admin,admin
activeMQ后台管理 查看队列详情
  1. 编写消费者代码
package market.daoge.active.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import javax.xml.soap.Text;

/**
 * 消费者
 * Created by Lidy on 2017/8/26 0026.
 */
public class AppConsumer {

    private static final String url = "tcp://localhost:61616";
    private static final String queueName = "queue-test";

    public static void main(String[] args) throws JMSException {

        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.创建连接
        Connection connection = connectionFactory.createConnection();
        // 3.启动连接
        connection.start();
        // 4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.创建一个目标
        Destination destination = session.createQueue(queueName);
        // 6.创建一个消费者
        MessageConsumer consumer = session.createConsumer(destination);
        // 7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage)message;
                try {
                    System.out.println("接收消息" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        // 9.关闭连接
//        connection.close();
    }
}

  1. 结果,运行两个消费者代码,运行一个生产者代码,按照队列的方式
第一个消费者 第二个消费者 生产者

三、主题模式消息

1.代码实战

  1. 创建生产者
package market.daoge.active.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 生产者
 * Created by Lidy on 2017/8/26 0026.
 */
public class AppProducer {

    private static final String url = "tcp://localhost:61616";
    private static final String topicName = "topic-test";

    public static void main(String[] args) throws JMSException {

        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.创建连接
        Connection connection = connectionFactory.createConnection();
        // 3.启动连接
        connection.start();
        // 4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.创建一个目标
        Destination destination = session.createTopic(topicName);
        // 6.创建一个生产者
        MessageProducer producer = session.createProducer(destination);

        for(int i=0;i<5;i++) {
            // 7.创建消息
            TextMessage textMessage = session.createTextMessage("text" + i);
            // 8.发布消息
            producer.send(textMessage);
            System.out.println("发送消息" + textMessage.getText());
        }
        // 9.关闭连接
        connection.close();
    }
}

  1. 创建消费者
package market.daoge.active.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消费者
 * Created by Lidy on 2017/8/26 0026.
 */
public class AppConsumer {

    private static final String url = "tcp://localhost:61616";
    private static final String topicName = "topic-test";

    public static void main(String[] args) throws JMSException {

        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // 2.创建连接
        Connection connection = connectionFactory.createConnection();
        // 3.启动连接
        connection.start();
        // 4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.创建一个目标
        Destination destination = session.createTopic(topicName);
        // 6.创建一个消费者
        MessageConsumer consumer = session.createConsumer(destination);
        // 7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage)message;
                try {
                    System.out.println("接收消息" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        // 9.关闭连接
//        connection.close();
    }
}

  1. 启动两个消费者,一个生产者,两个消费者同时收到5条信息
生产者 第一个消费者 第二个消费者

相关文章

  • Java消息中间件-ActiveMQ-入门实战

    一、Window下载ActiveMQ 1. 打开网站 http://activemq.apache.org 2. ...

  • RocketMQ入门

    RocketMQ入门 1. RocketMQ简介 RocketMQ是阿里开源的消息中间件,它是纯java开发,具有...

  • Java消息中间件之RabbitMQ

    什么是消息中间件? 消息代理规范JMS(Java Message Service)JAVA消息服务:基于JVM消息...

  • 消息中间件概念

    什么是JMS Java消息服务(Java Message Service),Java平台中关于面向消息中间件的接口...

  • Java秒杀系统实战系列~整合RabbitMQ实现消息异步发送

    摘要: 本篇博文是“Java秒杀系统实战系列文章”的第八篇,在这篇文章中我们将整合消息中间件RabbitMQ,包括...

  • ActiveMQ-中间件

    下载安装 队列模式 主题模式

  • JMS

    慕课网Java消息中间件笔记 JMS定义 Java消息服务(Java Message Server)即JMS,是一...

  • Java高并发--消息队列

    Java高并发--消息队列 主要是学习慕课网实战视频《Java并发编程入门与高并发面试》的笔记 举个例子:在购物商...

  • ActiveMQ

    1.Java消息中间件学习笔记一 -- 什么是消息中间件? https://blog.csdn.net/winte...

  • JAVA名词汇

    1、Java中间件,分布式系统、分布式缓存、消息队列 JAVA中间件:包括服务框架中间件:解决集群间的访问通信问题...

网友评论

      本文标题:Java消息中间件-ActiveMQ-入门实战

      本文链接:https://www.haomeiwen.com/subject/ehbmdxtx.html