美文网首页
ActiveMQ-中间件

ActiveMQ-中间件

作者: 通灵路耳 | 来源:发表于2020-06-24 10:27 被阅读0次

下载安装

消息中间件,公众号推送文章到“消息中间件服务器”,用户手机微信的“消息中间件客户端”自动获取信息

1、下载
链接:https://pan.baidu.com/s/1lqaaSXNpxAihqlVm0Cb9-g 
提取码:0p7t
2、启动:activemq.bat
3、访问:http://127.0.0.1:8161/
图片.png

队列模式

Active有两种模式,队列模式和主题模式
队列模式:生产者生产10个商品,一个商品只能被一个消费者得到
主题模式:公众号推送10条消息,凡关注该公众号的用户,都可以接受到这10条消息

0、导入jar

<dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.15.9</version>
    </dependency> 
      <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>4.3.1</version>
    </dependency>
  </dependencies>
   

1、判断工具类

package com.llhc;
import javax.swing.JOptionPane;
import cn.hutool.core.util.NetUtil;
public class ActiveMQUtil {
    public static void main(String[] args) {
        isEmpty();
    }
    /**
     * 判断是否启动ActiveMQ
     * @author 开发者
     *
     */
    public static void isEmpty(){
        if(NetUtil.isUsableLocalPort(8161)) {
            JOptionPane.showMessageDialog(null, "ActiveMQ 服务器未启动 ");
            System.exit(1);
        } 
    }
}

2、生产方Producer

package com.llhc;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.llhc.ActiveMQUtil;
/**
 * 生产方
 * @author 开发者
 *
 */
public class Producer {
    //服务地址,端口默认61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次发送的消息名称
    private static final String topicName="queue_style";
    public static void main(String[] args) throws JMSException {
        //0. 先判断端口是否启动了  Active MQ 服务器
        ActiveMQUtil.isEmpty();
        //1.创建ConnectionFactory,绑定地址
        ConnectionFactory factory=new ActiveMQConnectionFactory(url);
        //2.创建Connection
        Connection connection= factory.createConnection();
        //3.启动连接
        connection.start();
        //4.创建会话
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建一个目标 (队列类型)
        Destination destination=session.createQueue(topicName);
        //6.创建一个生产者
        MessageProducer producer=session.createProducer(destination);
 
        for (int i = 0; i < 100; i++) {
            //7.创建消息
            TextMessage textMessage=session.createTextMessage("队列消息-"+i);
            //8.发送消息
            producer.send(textMessage);
            System.out.println("发送:"+textMessage.getText());
        }
        //7. 关闭连接
        connection.close();
    }
}

3、消费方Consumer

package com.llhc;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import cn.hutool.core.util.RandomUtil;
import com.llhc.ActiveMQUtil;
/**
 * 订阅者
 * @author root
 *
 */
public class Consumer {
    //服务地址,端口默认61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次消费的消息名称
    private static final String topicName="queue_style";
 
    //消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
    private static final String consumerName="consumer-" + RandomUtil.randomString(5);
    public static void main(String[] args) throws JMSException {
        //0. 先判断端口是否启动了 Active MQ 服务器
        ActiveMQUtil.isEmpty();
        System.out.printf("%s 消费者启动了。 %n", consumerName);
 
        //1.创建ConnectiongFactory,绑定地址
        ConnectionFactory factory=new ActiveMQConnectionFactory(url);
        //2.创建Connection
        Connection connection= factory.createConnection();
        //3.启动连接
        connection.start();
        //4.创建会话
        Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建一个目标 (队列类型)
        Destination destination=session.createQueue(topicName);
        //6.创建一个消费者
        MessageConsumer consumer=session.createConsumer(destination);
        //7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {
 
            public void onMessage(Message arg0) {
                // TODO Auto-generated method stub
                TextMessage textMessage=(TextMessage)arg0;
                try {
                    System.out.println(consumerName +" 接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
 
            }
        });
         
        //8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
        //connection.close();
    }
}


4、启动2次消费方,启动一次生产方,就看到结果
图片.png

主题模式

Active有两种模式,队列模式和主题模式
队列模式:生产者生产10个商品,一个商品只能被一个消费者得到
主题模式:公众号推送10条消息,凡关注该公众号的用户,都可以接受到这10条消息

0、导入jar

<dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.15.9</version>
    </dependency> 
      <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>4.3.1</version>
    </dependency>
  </dependencies>
   

1、判断工具类

package com.llhc;
import javax.swing.JOptionPane;
import cn.hutool.core.util.NetUtil;
public class ActiveMQUtil {
    public static void main(String[] args) {
        isEmpty();
    }
    /**
     * 判断是否启动ActiveMQ
     * @author 开发者
     *
     */
    public static void isEmpty(){
        if(NetUtil.isUsableLocalPort(8161)) {
            JOptionPane.showMessageDialog(null, "ActiveMQ 服务器未启动 ");
            System.exit(1);
        } 
    }
}


2、生产方

package com.llhc;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {

   //服务地址,端口默认61616
   private static final String url="tcp://127.0.0.1:61616";
   //这次发送的消息名称
   private static final String topicName="topic_style";
   public static void main(String[] args) throws JMSException {
       //0. 先判断端口是否启动了  Active MQ 服务器
       ActiveMQUtil.isEmpty();
       //1.创建ConnectiongFactory,绑定地址
       ConnectionFactory factory=new ActiveMQConnectionFactory(url);
       //2.创建Connection
       Connection connection= factory.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 < 100; i++) {
           //7.创建消息
           TextMessage textMessage=session.createTextMessage("主题消息-"+i);
           //8.发送消息
           producer.send(textMessage);
           System.out.println("发送:"+textMessage.getText());
       }
       //7. 关闭连接
       connection.close();
   }
}

3、消费方

package com.llhc;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
 
import org.apache.activemq.ActiveMQConnectionFactory;
 
import cn.hutool.core.util.RandomUtil;
/**
 * 订阅者
 * @author root
 *
 */
public class Consumer {
    //服务地址,端口默认61616
    private static final String url="tcp://127.0.0.1:61616";
    //这次消费的消息名称
    private static final String topicName="topic_style";
 
    //消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
    private static final String consumerName="consumer-" + RandomUtil.randomString(5);
    public static void main(String[] args) throws JMSException {
         
        //0. 先判断端口是否启动了 Active MQ 服务器
        ActiveMQUtil.isEmpty();
        System.out.printf("%s 消费者启动了。 %n", consumerName);
        //1.创建ConnectiongFactory,绑定地址
        ConnectionFactory factory=new ActiveMQConnectionFactory(url);
        //2.创建Connection
        Connection connection= factory.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 arg0) {
                // TODO Auto-generated method stub
                TextMessage textMessage=(TextMessage)arg0;
                try {
                    System.out.println(consumerName +" 接收消息:"+textMessage.getText());
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
 
            }
        });
         
        //8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
        //connection.close();
    }
}

相关文章

  • ActiveMQ-中间件

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

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

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

  • ActiveMQ-安全机制

    activemq的web管理页面:http://127.0.0.1:8161/admin activemq管控台使...

  • ActiveMQ-持久化存储

    activemq持久化存储:可以切换不同的存储技术(默认是kahadb,leveldb,mysql,oracle)...

  • 翻译

    Laravel 的路由中间件 简介 创建中间件 注册中间件全局中间件为路由指定中间件中间件组 中间件参数 Term...

  • 中间件学习——具体分类

    中间件分为远程过程调用中间件、数据访问中间件、消息中间件、事务(交易)处理中间件、分布式对象中间件。 远程过程调用...

  • nodejs19-express中间件

    中间件 匹配路由之前和之后做的操作 应用级中间件 路由级中间件 错误处理中间件 内置中间件 第三方中间件 应用级中...

  • 4.3KOA 中间件模块化与中间件合成

    中间件模块化与中间件合成 一、中间件模块化 定义中间件模块 使用中间件模块 二、使用 koa-compose 模块...

  • 4.2KOA 中间件执行流程

    中间件执行流程 代码执行流程 中间件 1 开始执行中间件 2 开始执行执行内容中间件 2 结束执行中间件 1 结束...

  • 13.中间件和上下文处理器

    中间件 中间件的引入image.png django中的中间件django 中的中间件(middleware),在...

网友评论

      本文标题:ActiveMQ-中间件

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