Spring Boot与ActiveMQ整合

1.引入依赖

 

<properties>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
         <!--activeMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
    </dependencies>

 2.创建生产者

   

/**
 * 消息生产类
 */
@RestController
public class QueueController {

    @Resource //用@Autowired报错,不知道为什么
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/send")
    public void send(String text){
        jmsMessagingTemplate.convertAndSend("itcast",text );
    }

    @RequestMapping("/sendMap")
    public void sendMap(){
        Map map =new HashMap();
        map.put("姓名","张三" );
        map.put("年龄",18 );
        jmsMessagingTemplate.convertAndSend("itcast_map",map );
    }

3.创建消费者

 

/**
 * 消息接受类
 */
@Component
public class QueueConsumer {
   @JmsListener(destination = "itcast")
   public void readMessage(String text){
       System.out.println("接受消息为:"+text);
   }

   @JmsListener(destination = "itcast_map")
    public void readMap(Map map){
       System.out.println("接受的Map"+map);
   }
}

 4.创建引导类

 

/**
 * springboot引导类
 */
@SpringBootApplication//springboot引导类注解
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}

5.配置不使用内置的activeMQ(不配置就使用内置的activeMQ)

 在 application.properties中(文件名称不能变)

#配置内置tomcat端口号
server.port=8088
#使用外置的activeMQ
spring.activemq.broker-url=tcp://192.168.60.128:61616

 

  

posted on 2018-11-17 15:54  雨后黄昏  阅读(145)  评论(0编辑  收藏  举报

导航