原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11792398.html

RMQ Direct

Project Directory

Maven Dependency

 <?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>org.fool.rmq</groupId>
<artifactId>rmq</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency> <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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.properties

 spring.application.name=rmq
server.port= spring.rabbitmq.host=localhost
spring.rabbitmq.port=
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin rmq.config.exchange=log.direct
rmq.config.queue.info=log.info
rmq.config.queue.info.routing.key=log.info.routing.key
rmq.config.queue.error=log.error
rmq.config.queue.error.routing.key=log.error.routing.key

Source Code

Application.java

 package org.fool.rmq;

 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);
}
}

RmqConfig.java

 package org.fool.rmq.config;

 import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RmqConfig { @Bean
public Queue queue() {
return new Queue("hello-rmq");
}
}

LogProducer.java

 package org.fool.rmq.direct;

 import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.util.Date; @Component
public class LogProducer {
@Autowired
private AmqpTemplate rmqTemplate; @Value("${rmq.config.exchange}")
private String exchange; @Value("${rmq.config.queue.info.routing.key}")
private String infoRoutingKey; @Value("${rmq.config.queue.error.routing.key}")
private String errorRoutingKey; public void send() {
String message = "Hello " + new Date();
rmqTemplate.convertAndSend(exchange, infoRoutingKey, message);
rmqTemplate.convertAndSend(exchange, errorRoutingKey, message);
}
}

LogInfoConsumer.java

 package org.fool.rmq.direct;

 import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "${rmq.config.queue.info}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange}", type = ExchangeTypes.DIRECT),
key = "${rmq.config.queue.info.routing.key}"
))
public class LogInfoConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume info: " + message);
}
}

LogErrorConsumer.java

 package org.fool.rmq.direct;

 import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "${rmq.config.queue.error}", autoDelete = "true"),
exchange = @Exchange(value = "${rmq.config.exchange}", type = ExchangeTypes.DIRECT),
key = "${rmq.config.queue.error.routing.key}"
))
public class LogErrorConsumer {
@RabbitHandler
public void handle(String message) {
System.out.println("consume error: " + message);
}
}

ApplicationTest.java

 package org.fool.rmq.test;

 import org.fool.rmq.Application;
import org.fool.rmq.direct.LogProducer;
import org.fool.rmq.producer.Producer;
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; @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest { @Autowired
private Producer producer; @Autowired
private LogProducer logProducer; @Test
public void test() {
producer.send();
} @Test
public void testDirect() {
logProducer.send();
}
}

Console Output

RMQ Management

RMQ Direct的更多相关文章

  1. RMQ Fanout

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11795256.html RMQ Fanout Project Directory Maven Depe ...

  2. RMQ Topic

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11794927.html RMQ Topic Project Directory Maven Depen ...

  3. RabbitMQ学习之:(六)Direct Exchange (转贴+我的评论)

    From: http://lostechies.com/derekgreer/2012/04/02/rabbitmq-for-windows-direct-exchanges/ RabbitMQ fo ...

  4. RMQ Terminology

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11784644.html RMQ模型架构 RMQ Terminology Message 消息,消息是不 ...

  5. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  6. UVA 11235Frequent values(RMQ)

    训练指南P198 题意:给出一个非降序排列的整数数组a1, a2…… an,你的任务是对于一系列询问(i,j),回答ai, ai+1 ……aj 中出现的次数最多的次数 这题不仅学到了rmq的应用还学到 ...

  7. MMAP和DIRECT IO区别

    看完此文,题目不言自明.转自 http://blog.chinaunix.net/uid-27105712-id-3270102.html 在Linux 开发中,有几个关系到性能的东西,技术人员非常关 ...

  8. 51nod1174(RMQ)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1174 题意:中文题诶- 思路:RMQ模板题 关于RMQ: h ...

  9. direct path read

    在11g中,全表扫描可能使用direct path read方式,绕过buffer cache,这样的全表扫描就是物理读了. 在10g中,都是通过gc buffer来读的,所以不存在direct pa ...

随机推荐

  1. URL里的分号';'一定要编码为%3b!!!!

    http://en.wikipedia.org/wiki/Query_string The series of pairs is separated by the ampersand, '&' ...

  2. C++中让人忽视的左值和右值

    前言 为了了解C++11的新特性右值引用,不得不重新认识一下左右值.学习之初,最快的理解,莫过于望文生义了,右值那就是赋值号右边的值,左值就是赋值号左边的值.在中学的数学的学习中,我们理解的是,左值等 ...

  3. ceph-pve英语

    adapted accordingly并相应地调整 silosn. 筒仓:粮仓:贮仓(silo的复数) saturatevt. 浸透,使湿透:使饱和,使充满While one HDD might no ...

  4. FutureTask的用法以及两种常用的使用场景

    参考博客:https://blog.csdn.net/linchunquan/article/details/22382487 FutureTask可用于异步获取执行结果或取消执行任务的场景.通过传入 ...

  5. HashTable 和 HashMap 区别

    hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法. hashTable同步的,而HashMap是非同步的,效率上 ...

  6. 32 kill不掉的语句

    32 kill不掉的语句 在mysql中有两个kill命令:一个是kill query+线程id,表示终止这个线程正在执行的语句:一个是kill connection+线程id,缺省connectio ...

  7. Babel编译:动态计算的属性名

    ES2015允许使用表达式作为属性名. 编译前: const HELLO = 'hello'; let dog = { [HELLO](){ console.log('hello'); } } 编译后 ...

  8. Java ——数组 选择排序 冒泡排序

    本节重点思维导图 数组 public static void main(String[] args) { int a ; a=3; int[] b; b = new int[3];//强制开辟内存空间 ...

  9. 浅谈Java反射机制 之 获取类的 方法 和 属性(包括构造函数)

    上一篇 获取 类 的字节码文件 我们讲到了获取类的字节码文件的三种方法 第三种方法通过getClass("全路径名")获取字节码文件最符合要求 1.获取构造方法 先贴上我们要获取的 ...

  10. Django的ORM常用查找操作总结

    作者:python技术人 博客:https://www.cnblogs.com/lpdeboke/ 首先这里给出一个用户信息model class UserModel(models.Model): u ...