headers 模式 不太常用。
使用 spring-rabbitmq-support
Java 核心代码
import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
public class Headers {
public static void main(String[] args) {
CachingConnectionFactory cf = new CachingConnectionFactory("ip");
cf.setUsername("root");
cf.setPassword("");
RabbitAdmin admin = new RabbitAdmin(cf);
Queue queue = new Queue("q1");
Queue queue2 = new Queue("q2");
admin.declareQueue(queue);
admin.declareQueue(queue2);
HeadersExchange exchange = new HeadersExchange("head");
admin.declareExchange(exchange);
Map <String,Object> map = new HashMap<String,Object>();
map.put("foo", "xxx");
map.put("x-match", "all");
admin.declareBinding(BindingBuilder.bind(queue).to(exchange).whereAll(map).match());
//admin.declareBinding(BindingBuilder.bind(queue2).to(exchange).w);
String body = "body";
MessageProperties messageProperties =new MessageProperties();
messageProperties.setHeader("foo", "xxx");
Message message = new Message(body.getBytes(), messageProperties);
// send something
RabbitTemplate template = new RabbitTemplate(cf);
template.convertAndSend("head", null, message);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
spring 配置文件方法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">
<!-- 连接配置 -->
<rabbit:connection-factory id="connectionFactory"
host="ip" username="root" password="harry" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- spring template声明 -->
<rabbit:template exchange="yyy" id="amqpTemplate"
connection-factory="connectionFactory" />
<rabbit:queue name="test_queue_key"></rabbit:queue>
<rabbit:queue name="test2"></rabbit:queue>
<rabbit:headers-exchange name="yyy">
<rabbit:bindings>
<rabbit:binding queue="test2">
<rabbit:binding-arguments>
<entry key="foo" value="bar" />
<entry key="x-match" value="all" />
</rabbit:binding-arguments>
</rabbit:binding>
</rabbit:bindings>
</rabbit:headers-exchange>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
发送消息
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("rabbit.xml");
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
String body = "body";
MessageProperties messageProperties =new MessageProperties();
messageProperties.setHeader("foo", "bar");
Message message = new Message(body.getBytes(), messageProperties);
// send something
template.send(message);
Thread.sleep(1000);
ctx.destroy();