springboot整合activemq 简单版本

整个流程就是访问页面http://localhost:8888/index跳转到我写的简单页面welcome.xml,期间触发一个生产消费消息的过程

配置文件:application.yml 

server:
  port: 8888
spring:
  thymeleaf:
      prefix: classpath:/templates/
      suffix: .html
      mode: HTML5
      encoding: utf-8
      content-type: text/html
      cache: false
  activemq:
      broker-url: tcp://localhost:61616
      user: admin
      password: admin
      in-memory: false
      pool:
        enabled: true
        max-connections: 5
        idle-timeout: 30000
        expiry-timeout: 0

pom.xml

xml version="1.0" encoding="UTF-8"?>
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">
    4.0.0
    com.example
    demo
    0.0.1-SNAPSHOT

    demo
    Demo project for Spring Boot
    
        org.springframework.boot
        spring-boot-starter-parent
        1.4.0.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-activemq
        
        
            org.apache.activemq
            activemq-pool
            5.7.0
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

springboot整合activemq 简单版本_第1张图片目录结构

代码:

package taikang.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}


package taikang.test.service;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

@Service("produceService")
public class ProduceService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void sendMsg(String destinationName, String message) {
        Destination destination = new ActiveMQQueue(destinationName);
        jmsMessagingTemplate.convertAndSend(destination, message);
    }
}
package taikang.test.service;

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

@Service
public class ConsumerService {

    @JmsListener(destination = "HelloChina")
    public void receiveMsg(String text){
        System.out.println("收到消息---->"+text);
    }

}
package taikang.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import taikang.test.service.ProduceService;

@Controller
public class ProducerController {

    @Autowired
    private ProduceService produceService;

    @RequestMapping("/index")
    public String index() {
        for (int i = 0; i < 15; i++) {
            produceService.sendMsg("HelloChina", "哈哈,你好,你太帅了+" + i);
        }
        return "welcome";
    }
}
csstest.css
body {
    padding: 0px;
    margin: auto;
    font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System;
    background-color: #00F;
    font-size: 60px;
    text-align: left;
}
welcome.html
html>
lang="en">

    charset="UTF-8">
    </span>Title<span style="color:#e8bf6a;">
    href="css/csstest.css" rel="stylesheet"/>
    


welcome page is login.........

springboot整合activemq 简单版本_第2张图片

springboot整合activemq 简单版本_第3张图片

在ProduceService中,我遇到个问题

jmsMessagingTemplate

一直下边有红色的错误,说注入失败,但我发现整个文件并未报错,特别是我已经导包,所以我就强制性取消了那个错误,当然我原来启动也米有问题,正常能够运行出相关的结果。具体是什么原因我还不能理解

 @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate 

注意我在配置文件中,设置了activemq的连接池,不需要的可以把

pool:
        enabled: true-----》设置为false或者直接把pool删掉
        max-connections: 5
        idle-timeout: 30000
        expiry-timeout: 0


你可能感兴趣的:(消息中间件)