springboot整合activiMQ

首先说明:

队列queue  的特点就是,许多人可以监听它,但是只有一个人能够收到消息。 
主题topic    的特点就是,许多人监听它,都能收到消息。

 

需要安装activiMQ在自己的电脑上。启动方法 在bin目录   activemq start   即可

访问activiMQ在浏览器输入 :http://localhost:8161  

 

创建一个springboot的项目

相关依赖pom

<?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.example</groupId>
    <artifactId>activemq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>activemq</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
View Code

application.properties

spring.activemq.broker-url=tcp://localhost:61616

说明:这里url的访问协议和路径要注意一下。

发送者

package cn.lzh.demo;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 消息生产者
 * @author Administrator
 *
 */
@RestController
public class QueueController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    
    @RequestMapping("/send")
    public void send(String text){
    
        jmsMessagingTemplate.convertAndSend("A1", text);
    }
    
    
    @RequestMapping("/sendmap")
    public void sendMap(){
        Map map=new HashMap<>();
        map.put("mobile", "13683391759");
        map.put("template_code", "SMS_86640114");
        map.put("sign_name","卡卡罗特");
        map.put("param", "{\"name\":\"小刘\"}");
        
        jmsMessagingTemplate.convertAndSend("A2", map);
    }
}

消费者

package cn.lzh.demo;

import java.util.Map;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @JmsListener(destination="A1")
    public void readMessage(String text){
        System.out.println("接收到消息:"+text);
    }
    
    @JmsListener(destination="A2")
    public void readMapMessage(Map map){
        System.out.println(map);        
    }
    
}

 

 

 

简单的要死简直!!!

 

posted @ 2018-06-20 23:36  发疯的man  阅读(769)  评论(0编辑  收藏  举报