超级简单ActiveMQ双向主题订阅(队列同理)

ActiveMQ

不用我多说ActiveMQ是什么了,服务器配置其他教程也很多,这里主要讲如何用最简单的代码实现。

我刚学的时候,发现网上的教程大都基于Spring boot,也看了很多,走了很多弯路。

虽然这个框架非常好,但是如果只是个人做一些小设计,只需要简单地和服务器实现双向通讯,同时没必要学习那么多Spring互相调用的知识,这个教程非常适合小白和入门者,只需要最简单的Spring框架即可。

Spring boot模板下载

虽然还是躲不开Spring boot,但是我们无需去配置它,只需要下载一个模板,导入后面我发出来的代码即可。

下载模板:Spring init

根据自己的需要设置包名,名称,JDK版本。
在这里插入图片描述

在下面的依赖选项,搜索ActiveMQ,选择ActiveMQ 5,点击加号,添加进去。
在这里插入图片描述

点击绿色的Generate按钮下载进来,用idea打开。porn.xml中的依赖已经自动添加好了。
在这里插入图片描述

双向主题订阅

在自己的包下,新建三个类,把对应类名的代码复制即可。

主类 ActiveMQDoubleTopic

package com.example.ActiveMQ;

import javax.jms.*;
import java.util.Scanner;

public class ActiveMQDoubleTopic {

    //给定帐号,密码,地址
    public static final String ACTIVE_MQ_URL = "tcp://192.168.6.162:61616";
    public static final String ACTIVE_MQ_NAME = "admin";
    public static final String ACTIVE_MQ_PSW = "admin";
    //双向主题名字,根据需要修改,这里选择两个一样的,方便只用打开一个idea调试
    public static final String SEND_TOPIC_NAME = "SendTopic";
    public static final String GET_TOPIC_NAME = "SendTopic";

    public static void main(String[] args) throws JMSException {
        Scanner scanner = new Scanner(System.in);
        Producer producer = new Producer();
        Consumer consumer = new Consumer();
        while (true) {
            System.out.print("输入请求为:");
            producer.sendMessage(scanner.nextLine());
        }
    }
}

消息生产类 Producer

package com.example.ActiveMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Producer {

    private ActiveMQConnectionFactory activeMQConnectionFactory;
    private Connection connection;
    private Session session;
    private Topic topic;
    private MessageProducer messageProducer;

    //MessageProducer生产消息,发送到队列中
    public void sendMessage(String message) throws JMSException {
        System.out.println("发送了:" + message);
        TextMessage textMessage = session.createTextMessage(message);//创建消息
        messageProducer.send(textMessage);//通过MessageProducer发送给MQ
    }

    public Producer() throws JMSException {
        activeMQConnectionFactory = new ActiveMQConnectionFactory
                (ActiveMQDoubleTopic.ACTIVE_MQ_NAME, ActiveMQDoubleTopic.ACTIVE_MQ_PSW, ActiveMQDoubleTopic.ACTIVE_MQ_URL);//创建ActiveMQ连接工厂,给定用户名,密码,地址
        connection = activeMQConnectionFactory.createConnection();//获得连接connection,抛出了JMSException异常,需要处理,且启动
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//创建session,形参为事务,签收
        topic = session.createTopic(ActiveMQDoubleTopic.SEND_TOPIC_NAME);//创建目的地,Destination作为父类,是用其子类主题topic,jms包中
        messageProducer = session.createProducer(topic);//创建消息生产者
    }
}

消息消费者 Consumer

package com.example.ActiveMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Consumer {

    private ActiveMQConnectionFactory activeMQConnectionFactory;
    private Connection connection;
    private Session session;
    private Topic topic;
    private MessageConsumer messageConsumer;

    public void setGetMessage() throws JMSException {
        //通过事件监听方式来消费接收消息,当有消息时被触发
        this.messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if (message instanceof TextMessage){ //instanceof 是类型判断
                    TextMessage textMessage = (TextMessage)message;
                    //需要抛出异常
                    try {
                        System.out.println("接收到了:"+textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public Consumer() throws JMSException {
        activeMQConnectionFactory = new ActiveMQConnectionFactory
                (ActiveMQDoubleTopic.ACTIVE_MQ_NAME, ActiveMQDoubleTopic.ACTIVE_MQ_PSW, ActiveMQDoubleTopic.ACTIVE_MQ_URL);//创建ActiveMQ连接工厂,给定用户名,密码,地址
        connection = activeMQConnectionFactory.createConnection();//获得连接connection,抛出了JMSException异常,需要处理,且启动
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//创建session,形参为事务,签收
        topic = session.createTopic(ActiveMQDoubleTopic.GET_TOPIC_NAME);//Topic是Destination子类
        messageConsumer = session.createConsumer(topic);
        setGetMessage();//初始化监听事件
    }
}

运行结果:
在这里插入图片描述
解释一下:

  1. 输入了一条消息
  2. 调用Producer类的方法,发送了一条消息
  3. 因为while(ture)死循环,加上网络有传输时间,先显示第二次的“请输入请求”提示,scanner在等待下一次输入
  4. 此时,网络传输完了,监听接受主题的方法,监听到有一条消息进来,马上打印出来,中断了scanner的输入
  5. 打印完毕后,scanner继续,继续等待输入

要是想要双向队列,把对应的topic的变量和方法名,换成queue就行,很简单的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值