springboot與activemq的使用


1、springboot和activemq的使用相對來說比較方便了,我在網上看了很多其他的資料,但是自己寫出來總是有點問題所以,這里重點描述一下遇到的一些問題。

2、至於activemq的搭建和springmvc的搭建可以參考:http://www.cnblogs.com/ll409546297/p/6898155.html

3、項目的搭建和使用

  1)目錄結構

  

  2)需要的依賴包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.troy</groupId>
    <artifactId>springbootactivemq</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>1.5.7.RELEASE</version>
        </dependency>
    </dependencies>
</project>

  3)基本的配置application.yml

server:
  port: 8090
spring:
  activemq:
    broker-url: tcp://192.168.5.10:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: true

  說明:jms:pub-sub-domain:true(為true時是topic模式,為false是queue模式)  

  4)這里介紹兩種發送數據的方式

  (1)確定發送方式,這種需要做配置

  topic模式:ActiveMqTopicConfig.class

@Configuration
public class ActiveMqTopicConfig {

    @Bean
    public Topic topic(){
        return new ActiveMQTopic("topic");
    }
}

  queue模式:ActiveMqQueueConfig.class

@Configuration
public class ActiveMqQueueConfig {

    @Bean
    public Queue queue(){
        return new ActiveMQQueue("queue");
    }
}

  發送:ActiveMqClient.class

@RestController
@RequestMapping("/api")
public class ActiveMqClient {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Topic topic;

    @Autowired
    private Queue queue;

    @RequestMapping("/send")
    public void send(){
        jmsTemplate.convertAndSend(this.topic,"發送的topic數據!");
        jmsTemplate.convertAndSend(this.queue,"發送的queue數據!");
    }
}

  注:這里我方便測試,就直接寫的調用的方式!

  (2)直接發送的方式:ActiveMqClient.class

@RestController
@RequestMapping("/api")
public class ActiveMqClient {

    @Autowired
    private JmsTemplate jmsTemplate;

    @RequestMapping("/send")
    public void send(){
        jmsTemplate.send("topic", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText("send data");
                return textMessage;
            }
        });
    }
}

  5)接收數據:ActiveMqServer.class

@Component
public class ActiveMqServer {

    @JmsListener(destination = "topic")
    public void receiveTopic(String message) {
        System.out.println("監聽topic=============監聽topic");
        System.out.println(message);

    }

    @JmsListener(destination = "queue")
    public void receiveQueue(String message) {
        System.out.println("監聽queue=============監聽queue");
        System.out.println(message);

    }
}

  注:也可以實現MessageListener接口來接收數據,但是需要配置的東西和springmvc差不多,這里不做介紹。

 


免責聲明!

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



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