springboot整合activemq正常运行,ActiveMQ服务器收不到消息

学习activemq使用中,使用springboot 2.3 整合activemq使用,控制台能够正常输出,但是activemq服务器中查看消息为空。

控制台显示

 但是activemq服务器的queues为空

原因:application.yml配置文件出现错误,spring没有与server对齐

 

修改后

 

修改后,activemq服务器中收到消息。

下面为全部文件 

1、pom.xml 

<?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>

    <groupId>org.example</groupId>
    <artifactId>boot_activemq</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>


    <properties>
        <project.build.encoding>UTF-8</project.build.encoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot Activemq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

2、 application.yml

server:
  port: 7777

spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: false
my_queue: boot-active-queue
#!!!!!!注意间隔错误!!!!
#能够运行但是activemq收不到消息

3、ConfigBean.class

package active.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

@EnableJms
@Component
public class ConfigBean {

        @Value("${my_queue}")
        private String myQueue;

        @Bean
        public Queue queue()
        {
            return new ActiveMQQueue(myQueue);
        }


}

4、生产者类

package active.produce;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;

@Component
public class boot_activemq_produce{

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void productMessage()
    {
        jmsMessagingTemplate.convertAndSend(queue,"*****"+ UUID.randomUUID().toString());

    }


    //每隔5s时间向队列发送消息
    @Scheduled(fixedDelay=5000)  //每间隔2s向队列发送消息
    public void send() {
        String msgString = System.currentTimeMillis()+" ";
        jmsMessagingTemplate.convertAndSend(queue,msgString);
        System.out.println("点对点通讯,msg"+msgString);
    }
}



5、启动类

package active;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;


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

}

6、测试类

package ActiveMQ;


import active.MainApp_Produce;
import active.produce.boot_activemq_produce;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;

@SpringBootTest(classes = MainApp_Produce.class) //对那个类进行测试
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@WebAppConfiguration   //以webapp的形式
public class testActiveMQ {
    @Autowired   //javax//autowired是spring的
    private boot_activemq_produce produce;
    @Test
    public void test_send()throws Exception{
        produce.productMessage();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置Spring Boot整合ActiveMQ,你可以按照以下步骤进行操作: 1. 添加ActiveMQ依赖: 在你的项目中的pom.xml文件中,添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 2. 配置ActiveMQ连接信息: 在你的application.properties或application.yml文件中,添加以下配置: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 这里,你需要设置ActiveMQ的通信地址,以及账户名和密码。 3. 配置队列或主题模式: 默认情况下,ActiveMQ提供的是队列模式。如果你想使用主题模式,可以添加以下配置: ``` spring.jms.pub-sub-domain=true ``` 4. 启动ActiveMQ服务器: 你需要启动ActiveMQ服务器来进行消息的发送和接收。可以通过在浏览器中访问http://localhost:8161,使用管理员账户名和密码(admin/admin)登录ActiveMQ管理界面来启动服务器。 这样,你就可以在Spring Boot项目中成功配置和使用ActiveMQ了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java springboot整合activemq工程](https://download.csdn.net/download/weixin_47315082/88136003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot整合ActiveMQ](https://blog.csdn.net/weixin_45698637/article/details/123443728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [《SpringBoot篇》25.SpringBoot整合ActiveMQ](https://blog.csdn.net/weixin_47343544/article/details/128164353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值