SpringBoot整合ActiveMQ快速入門


Spring Boot 具有如下特性:

  1. 為基於 Spring 的開發提供更快的入門體驗

  2. 開箱即用,沒有代碼生成,也無需 XML 配置。同時也可以修改默認值來滿足特定的需求。

  3. 提供了一些大型項目中常見的非功能性特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等。

  4. Spring Boot 並不是不對 Spring 功能上的增強,而是提供了一種快速使用 Spring 的方式。

快速入門案例:

 

最終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">
    <!--自己項目本身所屬的父工程-->
    <parent>
        <artifactId>project_demo</artifactId>
        <groupId>com.zy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_demo</artifactId>

    <!--變更jdk版本-->
    <properties>
        <java.version>1.7</java.version>
    </properties>

    <!--自己項目本身是有parent的情況-->
    <!--type 是 pom scope 是 import 這種類型的dependency只能在 dependencyManagement 標簽中聲明-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.4.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <!--spring boot起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--熱部署 在idea中需要設置-->
        <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>
</project>
View Code

 

首先是我們的pom.xml文件:

1,官方示例中,讓我們繼承一個spring的 spring-boot-starter-parent 這個parent:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

 

2,但是,一般情況下,在我們自己的項目中,會定義一下自己的 parent 項目,這種情況下,上面的這種做法就行不通了。那么,該如何來做呢?其實,在spring的官網也給出了變通的方法的:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

3,然后,把我們項目中的 子項目 中,parent 的聲明,修改為我們自己項目的 parent 項目就可以了

<?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">
    <!--自己項目本身所屬的父工程-->
    <parent>
        <artifactId>project_demo</artifactId>
        <groupId>com.zy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_demo</artifactId>

    <!--變更jdk版本-->
    <properties>
        <java.version>1.7</java.version>
    </properties>

    <!--自己項目本身是有parent的情況-->
    <!--type 是 pom scope 是 import 這種類型的dependency只能在 dependencyManagement 標簽中聲明-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.4.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <!--spring boot起步依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

 

其他一些配置文件默認在resources下的application.properties文件中:

#修改tomcat端口
server.port=8088
#-------
url=http://www.1688.com
#activemq地址
spring.activemq.broker-url=tcp://192.168.25.128:61616

 

簡單示例 HelloController:

package com.zy.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private Environment env;

    @RequestMapping("/helloSpringBoot")
    public String hello() {
        return "hello springboot";
    }

    @RequestMapping("/info")
    public String info() {
        return "info:" + env.getProperty("url");
    }
}

 

我們現在想訪問HelloController,所以需要創建一個引導類Application:

package com.zy.demo;

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

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

現在就可以訪問http://localhost:8088/hello/helloSpringBoot  (由於我們在application.properties配置文件中修改了tomcat的端口為8088)。

 

SpringBoot整合ActiveMQ:

不需要其他配置,值需要在pom文件中引入ActiveMQ的依賴,並創建生產者 消費者即可:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

生產者Producer:

package com.zy.demo;

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;

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

@RestController
public class Producer {
    //注入jsmtemplate
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/sendMsg")
    public void sendMsg(String msg) {
        jmsMessagingTemplate.convertAndSend("my_msg", msg);
        System.out.println("msg發送成功");
    }

    @RequestMapping("/sendMap")
    public void sendMap() {
        Map map = new HashMap();
        map.put("mobile", "13888888888");
        map.put("content", "王總喜提蘭博基尼");
        jmsMessagingTemplate.convertAndSend("my_map", map);
        System.out.println("map發送成功");
    }
}

消費者Consumer:

package com.zy.demo;

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

import java.util.Map;

@Component
public class Consumer {
    @JmsListener(destination = "my_msg")
    public void readMsg(String text) {
        System.out.println("接收到消息:" + text);
    }

    @JmsListener(destination = "my_map")
    public void readMap(Map map) {
        System.out.println(map);
    }
}

訪問http://localhost:8088/...... 即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM