RabbitMQ 基础demo

pom.xml
<parent>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-parent</artifactId>
             <version>2.0.0.RELEASE</version>
</parent>
       
  <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <java.version>1.8</java.version>
           <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
             <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.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-amqp</artifactId>
             </dependency>
             
             <!-- 添加 junit 环境的 jar 包 -->
             <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-devtools</artifactId>
                   <optional>true</optional>
                   <scope>true</scope>
             </dependency>
  </dependencies>
  <build>
             <plugins>
                 <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
                   <configuration>
                       <fork>true</fork>
                   </configuration>
               </plugin>
             </plugins>
   </build>
application.yml
server:
  tomcat:
    uri-encoding: UTF-8
spring:
  application:
    name: 01-spring-boot-rabbitmq
  rabbitmq:
    host: 192.168.0.72
    port: 5672
    username: superrd
    password: superrd
QueueConfig.java
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 创建消息队列
**/
@Configuration
public class QueueConfig {
       
       /**
        * 创建队列
        **/
       @Bean
       public Queue createQueue(){
             return new Queue("hello-queue");
       }
}
Sender.java
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 消息发送者
* @author Administrator
*
*/
@Component
public class Sender {
       @Autowired
       private AmqpTemplate rabbitAmqpTemplate;
       /*
        * 发送消息的方法
        */
       public void send(String msg){
             //向消息队列发送消息
             //参数一:队列的名称。
             //参数二:消息
             this.rabbitAmqpTemplate.convertAndSend("hello-queue", msg);
       }
}
Receiver.java
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 消息接收者
* @author Administrator
*
*/
@Component
public class Receiver {
       /**
        * 接收消息的方法。采用消息队列监听机制
        * @param msg
        */
       @RabbitListener(queues="hello-queue")
       public void process(String msg){
             System.out.println("receiver: "+msg);
       }
}
测试代码(QueueTest.java):
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.jorudan.App;
import com.jorudan.Sender;
/**
* 消息队列测试类
* @author Administrator
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class)
public class QueueTest {
       @Autowired
       private Sender sender;
       
       /*
        * 测试消息队列
        */
       @Test
       public void test1(){
             this.sender.send("Hello RabbitMQ");
       }
}

正确结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值