JDK21 和 ActiveMQ 6.0.1

ActiveMQ 安装和测试

  • 一.ActiveMQ 下载
  • 二.ActiveMQ 安装
  • 三.代码测试
    • 1.Pom 依赖
    • 2.YML配置
    • 3.启动类
    • 4.MQ 配置类
    • 5.MQ 消费者
    • 6.发数测试类
  • 四.测试

一.ActiveMQ 下载

Apache 官网

Windows 下安装

JDK21 和 ActiveMQ 6.0.1_第1张图片

二.ActiveMQ 安装

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

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

activemq.bat start

JDK21 和 ActiveMQ 6.0.1_第2张图片

控制台

JDK21 和 ActiveMQ 6.0.1_第3张图片

查看启动结果

netstat -aon | findstr 61616

在这里插入图片描述

三.代码测试

1.Pom 依赖


<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.0modelVersion>
    <parent>
        <groupId>org.examplegroupId>
        <artifactId>flowable-testartifactId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>mqartifactId>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>3.2.1version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-activemqartifactId>
            <version>3.2.1version>
        dependency>
        <dependency>
            <groupId>org.apache.activemqgroupId>
            <artifactId>activemq-poolartifactId>
            <version>6.0.1version>
        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

JDK21 和 ActiveMQ 6.0.1_第4张图片

你可能感兴趣的:(消息中间件,activemq,jms,jdk21,spring,consumer)