SpringBoot整合ActiveMQ和开启持久化

一、点对点 1、提供者目录展示

  

SpringBoot整合ActiveMQ和开启持久化

2、导入依赖

  

SpringBoot整合ActiveMQ和开启持久化

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- spring boot web支持:mvc,aop... -->
    <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-activemq</artifactId>
    </dependency>

3、生产者application.yml配置文件

  

SpringBoot整合ActiveMQ和开启持久化

4、生产者MyProvider,通过JMSTemplate模板发送消息

package com.zn.p2p;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import Javax.annotation.Resource;

/**
 * 生产者,通过JMSTemplate模板发送消息
 */
@Component
public class MyProvider {
    //注入JMSTemplate模板
    @Resource
    private JmsTemplate jmsTemplate;
    //创建方法
    public void sendMessage(){
        //点对点,创建队列
        ActiveMQQueue queue=new ActiveMQQueue("SpringBoot_Queue");
        //发送消息
        jmsTemplate.convertAndSend(queue,"生产者产生的消息!");
    }
}

5、客户端访问ProviderController

package com.zn.p2p;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 客户端访问的方法
 */
@RestController
public class ProvideController {
    @Resource
    private MyProvider provider;

@RequestMapping("/sendMessage")
    public String sendMessage(){
        provider.sendMessage();
        return "sucess!!";
    }
}

6、provider启动类StartProvider

package com.zn;

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

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

7、消费者目录展示

  

SpringBoot整合ActiveMQ和开启持久化

8、导入依赖

  

SpringBoot整合ActiveMQ和开启持久化

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- spring boot web支持:mvc,aop... -->
    <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-activemq</artifactId>
    </dependency>

9、消费者application.yml配置文件

  

SpringBoot整合ActiveMQ和开启持久化

10、consumer启动类StartP2PConsumer

package com.zn;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/bcbd9f9695596f61d6709b6e8b546c09.html