spring boot 1.5.4 整合rabbitMQ(十七)

1.2.2     创建spring-boot-MQ工程

spring-boot-rabbitMQ

项目源码,

码云地址:https://git.oschina.net/wyait/springboot1.5.4.git

github地址https://github.com/wyait/spring-boot-1.5.4.git

 0601476f29dbc4601706a03ed9b0bf66.png

pom.xml文件:

<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <parent>

      <!-- spring boot项目的parent -->

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>1.5.4.RELEASE</version>

   </parent>

   <groupId>com.wyait.mq</groupId>

   <artifactId>spring-boot-mq</artifactId>

   <version>1.0.0-SNAPSHOT</version>

   <dependencies>

      <dependency>

        <!-- spring boot 引入Web模块。自动配置:tomcatspringmvcjackson -->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

        <exclusions>

           <exclusion>

              <artifactId>spring-boot-starter-logging</artifactId>

              <groupId>org.springframework.boot</groupId>

           </exclusion>

        </exclusions>

      </dependency>

      <dependency>

        <!-- test使用 -->

        <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-log4j</artifactId>

        <version>1.3.2.RELEASE</version>

      </dependency>

      <dependency>

        <!-- 整合rabbitmq,添加amqp依赖 -->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-amqp</artifactId>

      </dependency>

   </dependencies>

<build>

      <plugins>

        <plugin>

           <!-- 配置spring bootmaven插件 -->

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-maven-plugin</artifactId>

        </plugin>

      </plugins>

   </build>

</project>

 

Application启动类:

@Configuration

@SpringBootApplication

public class MqApplication {

   public static void main(String[] args) {

      SpringApplication.run(MqApplication.class, args);

   }

}

 

添加log4j.propertiesapplication.propertiesapplication-dev.properties配置文件。

 

application-dev.properties配置文件中添加rabbitmq配置:

spring.application.name=springboot-rabbitmq

spring.rabbitmq.host=127.0.0.1

spring.rabbitmq.port=5672

spring.rabbitmq.username=wyait

spring.rabbitmq.password=wyait

spring.rabbitmq.virtual-host=/

# p端收到回调,确认消息发送结果

spring.rabbitmq.publisher-confirms=true

spring.rabbitmq.publisher-returns=true

spring.rabbitmq.template.mandatory=true

 

编写RabbitMqConfig配置类:

创建RabbitMQ的配置类RabbitMqConfig,用来配置队列、交换器、路由等高级信息。这里我们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。

@Configuration

public class RabbitMqConfig {

   @Bean

   public Queue helloQueue() {

      return new Queue("hello");

   }

 

}

 

编写Sender类:

创建消息生产者Sender。通过注入AmqpTemplate接口的实例来实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入其具体实现。在该生产者,我们会产生一个字符串,并发送到名为hello的队列中。

@Component

public class Sender {

   @Autowired

   private AmqpTemplate rabbitMQTemplate;

 

   public void send() {

      String context = "hello :" + new Date();

      System.out.println("Sender : " + context);

      this.rabbitMQTemplate.convertAndSend("hello",context);

   }

}

 

编写Recevier类:

创建消息消费者Receiver。通过@RabbitListener注解定义该类对hello队列的监听,并用@RabbitHandler注解来指定对消息的处理方法。所以,该消费者实现了对hello队列的消费,消费操作为输出消息的字符串内容。

@Component

// 监听“hello”队列

@RabbitListener(queues ="hello")

public class Receiver {

 

   @RabbitHandler

   // handler注解来指定对消息的处理方法

   public void process(String hello) {

      System.out.println("Receiver:" + hello);

   }

}

 

编写test类:

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes =MqApplication.class)

public class MqApplicationTest{

   @Autowired

   private Sender send;

 

   @Test

   public void test() {

      System.out.println("==========发送消息!");

      send.send();

   }

 

}

 

完成程序编写之后,下面开始尝试运行。首先确保RabbitMQ Server已经开始,然后进行下面的操作:

 

  • 启动应用主类,从控制台中,我们看到如下内容,程序创建了一个访问127.0.0.1:5672wyait的连接。

 

Created newconnection: rabbitConnectionFactory#1e456bc3:0/SimpleConnection@2d428d9b [delegate=amqp://wyait@192.168.10.120:5672/,localPort= 2801]

 

同时,我们通过RabbitMQ的控制面板,可以看到ConnectionChannels中包含当前连接的条目,Queues中查看队列概况。

289a7434ec7cf248b080401f87338d95.png

 

  • 运行单元测试类,我们可以看到控制台中输出下面的内容,消息被发送到了RabbitMQ Serverhello队列中。

 

==========发送消息!

Sender : hello:Thu Sep 14 16:06:54 CST 2017

d93b67b08a2393c8b60220d9d6290440.png

 

  • 切换到应用主类的控制台,我们可以看到类似如下输出,消费者对hello队列的监听程序执行了,并输出了接受到的消息信息。

 

4be6ff2481d59291309d0cf3d3a2553a.png

 

通过上面的示例,我们在Spring Boot应用中引入spring-boot-starter-amqp模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。然而在实际应用中,还有很多内容没有演示,下面继续研究其他消息队列模式,大家也可以查阅RabbitMQ的官方教程,有更全面的了解。




本文转自 wyait 51CTO博客,原文链接:http://blog.51cto.com/wyait/1977527,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值