1.配置pom相关依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
       <version>1.5.4.RELEASE</version>
     </dependency>

2.在application.yml配置文件下配置相关信息

 spring:          #表示该配置直接为Spring容器负责处理 
jms:
pub-sub-domain: false #配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
activemq:
user: admin #连接用户名
password: admin #连接密码
broker-url: tcp://0.0.0.0:61616 #消息组件的连接主机信息

3.创建消费者代码

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service; @Service
public class MessageConsumerService {
@JmsListener(destination = "mldn.msg.queue")
public void receiveMessage(String text) {//进行消息接收处理
System.err.println("接受消息"+text);
}
}

4.建立生产者代码

生产者接口:

public interface ImessageProducerService {
public void sendMessage(String msg);
}

生产者实现:

import javax.annotation.Resource;
import javax.jms.Queue; import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; import cn.mldn.microboot.producer.ImessageProducerService;
@Service
public class MessageProducerServiceImpl implements ImessageProducerService {
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
@Resource
private Queue queue;
@Override
public void sendMessage(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue,msg);; } }

5.建立测试类

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import cn.mldn.microboot.StartSpringBootMain;
import cn.mldn.microboot.producer.ImessageProducerService; @SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
@Resource
private ImessageProducerService messageProducer;
@Test
public void testSend() throws Exception{
for (int i = 0; i < 103; i++) {
this.messageProducer.sendMessage("mldn-"+i);
}
}
}

6.增加消息Queue的配置

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms; @Configuration
@EnableJms
public class ActiveMQConfig {
@Bean
public Queue queue() {
return new ActiveMQQueue("mldn.msg.queue");
}
}

7.启动activemq的服务,该服务可自行下载,我这里下载了一个Windows版本的,直接启动就好。

8.登录activmq,http://localhost:8161/admin/,用户名密码采用默认的,admin、admin。也可以在activmq的配置文件配置用户

这样我们就成功登录。

最后进行测试,测试成功,查看activemq的Queues,成功接收消息

SpringBoot配置activemq消息队列的更多相关文章

  1. SpringBoot集成ActiveMq消息队列实现即时和延迟处理

    原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...

  2. springboot中activeMQ消息队列的引入与使用(发送短信)

    1.引入pom依赖 <!--activemq--><dependency> <groupId>org.springframework.boot</groupI ...

  3. activemq消息队列的使用及应用docker部署常见问题及注意事项

    activemq消息队列的使用及应用docker部署常见问题及注意事项 docker用https://hub.docker.com/r/rmohr/activemq/配置在/data/docker/a ...

  4. JAVA的设计模式之观察者模式----结合ActiveMQ消息队列说明

    1----------------------观察者模式------------------------------ 观察者模式:定义对象间一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的 ...

  5. ActiveMQ消息队列从入门到实践(4)—使用Spring JMS收发消息

    Java消息服务(Java Message Service ,JMS)是一个Java标准,定义了使用消息代理的通用API .在JMS出现之前,每个消息代理都有私有的API,这就使得不同代理之间的消息代 ...

  6. ActiveMQ 消息队列服务

      1 ActiveMQ简介 1.1 ActiveMQ是什么 ActiveMQ是一个消息队列应用服务器(推送服务器).支持JMS规范. 1.1.1 JMS概述 全称:Java Message Serv ...

  7. ActiveMQ基础教程(四):.net core集成使用ActiveMQ消息队列

    接上一篇:ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列 这里继续说下.net core集成使用ActiveMQ.因为代码比较多,所以放到gitee上:https://gitee ...

  8. C#实现ActiveMQ消息队列

    本文使用C#实现ActiveMQ消息队列功能. 一.首先需要导入两个包,分别是:Apache.NMS 和 Apache.NMS.ActiveMQ 二.创建Winform程序实现生产者功能. 三.Pro ...

  9. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

随机推荐

  1. 微服务之consul(一)

    一.概述 consul是google开源的一个使用go语言开发的服务发现.配置管理中心服务.内置了服务注册与发现框 架.分布一致性协议实现.健康检查.Key/Value存储.多数据中心方案,不再需要依 ...

  2. python3-列表字典简单练习题

    .求全部元素的和[,,,,,,,] >>> a=[,,,,,,,] >>> result= >>> for i in a: ... result+ ...

  3. WPF Geometry 添加Path数据

    当图片转svg,svg转Xaml后,根据数据加载显示图片 DrawingImage: <DrawingImage x:Key="Image.Search"> <D ...

  4. MyBatis基本要素---核心配置文件

    今天就简单的叙述下MyBatis的核心配置文件吧~~ configuration  配置 properties  可以配置在java属性配置文件中 settings   修改Mybatis在运行时的行 ...

  5. 小程序中使用ECharts 异步加载数据

    官网例子都是同步的,怎么引入及同步demo请移步官网 <view class="container"> <ec-canvas id="mychart-d ...

  6. Odoo / PS Cloud12版本中,产品变体功能如何使用

    场景: 产品:陶瓷马克杯 产品颜色变体:红色.蓝色.白色 产品尺寸变体:10CM.12CM.15CM 每个变体都有不同价格维度 odoo / PS Cloud 专业实施开发 EMAIL:1715860 ...

  7. odoo12 物流 自动计算运费 ,采购销售使用不同计量单位自动换算

    在物流和食品生鲜类行业,重量是很重要的因素,不仅要处理产品的数量,也要处理产品的重量.在多数行业,重量也是订单的重要数据. odoo原生包含了对重量的处理,下文中将指导如何优雅应用odoo原生模块处理 ...

  8. 正则表达式匹配html标签里面的内容

    假如html标签里面有一句:String a = "<style type=\"text/css\"> div \n" + "{ marg ...

  9. 【Java】itext根据模板生成pdf(包括图片和表格)

    1.导入需要的jar包:itext-asian-5.2.0.jar itextpdf-5.5.11.jar. 2.新建word文档,创建模板,将文件另存为pdf,并用Adobe Acrobat DC打 ...

  10. TSC条码打印机C#例程(tsclib.dll调用) 【转】

    //----  program.cs using System;using System.Collections.Generic;using System.Windows.Forms; using S ...