1、springboot和activemq的使用相对来说比较方便了,我在网上看了很多其他的资料,但是自己写出来总是有点问题所以,这里重点描述一下遇到的一些问题。

2、至于activemq的搭建和springmvc的搭建可以参考:http://www.cnblogs.com/ll409546297/p/6898155.html

3、项目的搭建和使用

  1)目录结构

  

  2)需要的依赖包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>com.troy</groupId>
<artifactId>springbootactivemq</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
</dependencies>
</project>

  3)基本的配置application.yml

server:
port: 8090
spring:
activemq:
broker-url: tcp://192.168.5.10:61616
user: admin
password: admin
jms:
pub-sub-domain: true

  说明:jms:pub-sub-domain:true(为true时是topic模式,为false是queue模式)  

  4)这里介绍两种发送数据的方式

  (1)确定发送方式,这种需要做配置

  topic模式:ActiveMqTopicConfig.class

@Configuration
public class ActiveMqTopicConfig { @Bean
public Topic topic(){
return new ActiveMQTopic("topic");
}
}

  queue模式:ActiveMqQueueConfig.class

@Configuration
public class ActiveMqQueueConfig { @Bean
public Queue queue(){
return new ActiveMQQueue("queue");
}
}

  发送:ActiveMqClient.class

@RestController
@RequestMapping("/api")
public class ActiveMqClient { @Autowired
private JmsTemplate jmsTemplate; @Autowired
private Topic topic; @Autowired
private Queue queue; @RequestMapping("/send")
public void send(){
jmsTemplate.convertAndSend(this.topic,"发送的topic数据!");
jmsTemplate.convertAndSend(this.queue,"发送的queue数据!");
}
}

  注:这里我方便测试,就直接写的调用的方式!

  (2)直接发送的方式:ActiveMqClient.class

@RestController
@RequestMapping("/api")
public class ActiveMqClient { @Autowired
private JmsTemplate jmsTemplate; @RequestMapping("/send")
public void send(){
jmsTemplate.send("topic", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText("send data");
return textMessage;
}
});
}
}

  5)接收数据:ActiveMqServer.class

@Component
public class ActiveMqServer { @JmsListener(destination = "topic")
public void receiveTopic(String message) {
System.out.println("监听topic=============监听topic");
System.out.println(message); } @JmsListener(destination = "queue")
public void receiveQueue(String message) {
System.out.println("监听queue=============监听queue");
System.out.println(message); }
}

  注:也可以实现MessageListener接口来接收数据,但是需要配置的东西和springmvc差不多,这里不做介绍。

springboot与activemq的使用的更多相关文章

  1. SpringBoot JMS(ActiveMQ) 使用实践

    ActiveMQ 1. 下载windows办的activeMQ后,在以下目录可以启动: 2. 启动后会有以下提示 3. 所以我们可以通过http://localhost:8161访问管理页面,通过tc ...

  2. Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ

    集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...

  3. springboot与ActiveMQ整合

    前言 很多项目, 都不是一个系统就做完了. 而是好多个系统, 相互协作来完成功能. 那, 系统与系统之间, 不可能完全独立吧? 如: 在学校所用的管理系统中, 有学生系统, 资产系统, 宿舍系统等等. ...

  4. SpringBoot集成ActiveMQ

    前面提到了原生API访问ActiveMQ和Spring集成ActiveMQ.今天讲一下SpringBoot集成ActiveMQ.SpringBoot就是为了解决我们的Maven配置烦恼而生,因此使用S ...

  5. 使用SpringBoot集成ActiveMQ

    SpringBoot是个好东西,好多java常用的东西都被集成进去了 JMS 在 Spring Boot 中的使用 使用Spring/Spring Boot集成JMS的陷阱 Spring-boot J ...

  6. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  7. SpringBoot整合ActiveMQ快速入门

    Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...

  8. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

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

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

随机推荐

  1. 泛型:上边界和通配符的使用以及对ArrayList的学习

      --------------- public class Wildcord { public static void main(String[] args) { /** * 类引用结构说明Pers ...

  2. Django 数据模型的字段列表整理

    一个模型最重要也是唯一必需的部分,是它定义的数据库字段. 字段名称限制: 1.一个字段名不能是一个Python保留字,因为那样会导致一个Python语法错误. 2.一个字段名不能包含连续的一个以上的下 ...

  3. 【[USACO08FEB]酒店Hotel】

    比较基础的线段树了 我们要维护最大连续子串,这个可以说是一个比较套路的操作了 我们在[SHOI2009]会场预约这道题中已经比较深刻的认识到了这个套路了 对于这道题,我们显然要知道一个区间内最大的全为 ...

  4. ROBOCOPY——Windows 的可靠文件复制

    复制指定类型文件 (-s :含子目录  不包括空目录) 复制所有 (-e :含子目录  包括空目录) 复制指定成层级内的 (-lev:n 仅复制源目录树的前 n 层) 复制排除给定类型后的 (-xf) ...

  5. 【转】maven命令-P 参数引发的思考

    序言: maven 命令:clean package -Dmaven.test.skip=true -P product 1.命令很简单是:清class文件,打包构建,跳过测试,注意最后一个 -P p ...

  6. java序列化报错

    Main.javat mainsr &java.util.Collections$UnmodifiableList�%1�� L listq ~xr ,java.util.Collection ...

  7. OC中对象的description方法

    周所周知,我们在做项目时, 可以在类的.m文件中重写该类的对象的描述description方法: 示例: -(NSString *)description{    NSString *str = [N ...

  8. 从C语言的volatile关键字,了解C#的volatile机制(转载)

    C#中有一个关键字volatile,一直不太明白到底什么时候才用它,只知道在多线程操作同一个变量的时候要使用volatile关键字,下面看到了一篇C语言关于volatile关键字的介绍,写的很不错,其 ...

  9. win7下添加库文件出现“file is not regcognized”问题

    最近几天需要画电路图,所以安装了protel se99,安装后在添加库文件的时候出现“file is not regcognized”的问题 百度查了一下,说win7基本上都会出现这个问题. 实际上, ...

  10. 四、MapReduce 基础

    是一个并行计算框架(计算的数据源比较广泛-HDFS.RDBMS.NoSQL),Hadoop的 MR模块充分利用了HDFS中所有数据节点(datanode)所在机器的内存.CUP以及少量磁盘完成对大数据 ...