JDK21 和 ActiveMQ 6.0.1

一.ActiveMQ 下载

Apache 官网

Windows 下安装

在这里插入图片描述

二.ActiveMQ 安装

JDK21 已安装并正确配置环境变量

解压上面下载的压缩包,64位电脑单击如图位置打开控制台,并输入命令

activemq.bat start

在这里插入图片描述

控制台

在这里插入图片描述

查看启动结果

netstat -aon | findstr 61616

在这里插入图片描述

三.代码测试

1.Pom 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>flowable-test</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>mq</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>6.0.1</version>
        </dependency>
    </dependencies>

</project>

2.YML配置

server:
  port: 8085
spring:
  activemq:
    broker-url: tcp://localhost:61616

3.启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MqApp {
    public static void main(String[] args) {
        SpringApplication.run(MqApp.class, args);
    }
}

4.MQ 配置类

package org.example.config;

import jakarta.jms.Queue;
import jakarta.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;


/**
 * @Author zhx & moon
 * @Since 1.8
 * @Date 2024-01-09 上午 10:55
 */
@Configuration
public class MqConfig {

    @Value("${spring.activemq.broker-url}")
    String brokerUrl;

    static final String key = "flowable-test";

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl);
        return factory;
    }

    /**
     * 点对点队列
     * @return
     */
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(key);
    }

    /**
     * 主题
     * @return
     */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic(key);
    }

    /**
     * queue 模式连接点
     * @param connectionFactory
     * @return
     */
    @Bean("jmsListenerContainerQueue")
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    /**
     * topic 模式连接点
     * @param connectionFactory
     * @return
     */
    @Bean("jmsListenerContainerTopic")
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setPubSubDomain(true);
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

}

5.MQ 消费者

package org.example.config;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @Author zhx & moon
 * @Since 1.8
 * @Date 2024-01-09 上午 11:33
 */
@Component
public class MqConsumer {

    static final String key = "flowable-test";

    @JmsListener(destination = key,containerFactory = "jmsListenerContainerQueue")
    public void consumerQ(String msg){
        System.out.println("queue: " + msg);
    }

    @JmsListener(destination = key,containerFactory = "jmsListenerContainerTopic")
    public void consumerT(String msg){
        System.out.println("topic: " + msg);
    }

}

6.发数测试类

package org.example.controller;

import jakarta.jms.Queue;
import jakarta.jms.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author zhx & moon
 * @Since 1.8
 * @Date 2024-01-09 上午 11:29
 */
@RestController
@RequestMapping("/test")
public class Test {

    @Autowired
    Queue queue;

    @Autowired
    Topic topic;

    @Autowired
    JmsMessagingTemplate template;

    @GetMapping("/queue")
    public void producerQ(@RequestParam("msg") String msg){
        template.convertAndSend(queue,msg);
    }

    @GetMapping("/topic")
    public void producerT(@RequestParam("msg") String msg){
        template.convertAndSend(topic,msg);
    }

}

四.测试

127.0.0.1:8085/test/topic?msg=topic test

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪悟道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值