整个流程就是访问页面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"?>
<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>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.7.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

springboot整合activemq 简单版本目录结构

代码:

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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="css/csstest.css" rel="stylesheet"/>
    </meta>
</head>
<body>
<p>welcome page is login.........</p>
</body>
</html>

springboot整合activemq 简单版本

springboot整合activemq 简单版本

在ProduceService中,我遇到个问题

jmsMessagingTemplate

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

 @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate 

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

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


相关文章: