springboot与activemq的使用

简介: 1、springboot和activemq的使用相对来说比较方便了,我在网上看了很多其他的资料,但是自己写出来总是有点问题所以,这里重点描述一下遇到的一些问题。2、至于activemq的搭建和springmvc的搭建可以参考:http://www.

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差不多,这里不做介绍。

 

相关文章
|
消息中间件 Java Maven
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
95 0
ActiveMQ系列:结合SpringBoot,基于 application.xml 使用ActiveMQ
|
消息中间件 网络协议 Java
ActiveMQ系列:结合Spring,基于配置文件的使用ActiveMQ
从activemq脚本可以看出启动ActiveMQ实际是启动,bin文件夹下的其实activemq.jar 包中有一个类为Main,这就是active的启动入口,Main主要是加载lib目录和ClassPath,初始化 类加载器,委托给ShellCommand,由ShellCommand根据命令描述去执行,如果是Version和HELP, 则打印信息,若是启动命令,则通过XBeanBrokerFactory创建BrokerService
127 0
ActiveMQ系列:结合Spring,基于配置文件的使用ActiveMQ
|
消息中间件 网络协议 Java
SpringBoot 整合 ActiveMQ|学习笔记
快速学习 SpringBoot 整合 ActiveMQ
119 0
SpringBoot 整合 ActiveMQ|学习笔记
|
消息中间件 网络协议 Java
springboot整合jms之activemq
springboot整合jms之activemq
130 0
springboot整合jms之activemq
|
消息中间件 Java
Springboot ActiveMQ 消息重发延迟时间 坑记
Springboot ActiveMQ 消息重发延迟时间 坑记
358 0
Springboot ActiveMQ 消息重发延迟时间 坑记
|
消息中间件 Java 中间件
24、springboot集成ActiveMQ
目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。spring boot提供了对JMS系统的支持;springboot很方便就可以集成这些消息中间件。
262 0
24、springboot集成ActiveMQ
|
消息中间件 Java Spring
SpringBoot集成ActiveMQ抛出java.lang.NoClassDefFoundError异常
版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82732127 SpringBoot集成ActiveMQ抛出java.
1974 0
|
消息中间件 存储 安全
干货|SpringBoot JMS(ActiveMQ)API实践应用详解
Active是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。AC-tiveMQ使用Apache提供的...
254 0
|
消息中间件 存储 安全
SpringBoot集成ActiveMQ实例详解
SpringBoot集成ActiveMQ实例详解
353 0
SpringBoot集成ActiveMQ实例详解
|
消息中间件 Java Maven
ActiveMQ整合Spring框架
前面文章介绍了ActiveMQ的相关内容,本文介绍ActiveMQ和Spring的整合开发
ActiveMQ整合Spring框架