消息队列08SpringBoot搭建RabbitmqJava对象发送接收

编程

gralde

compile "org.springframework.cloud:spring-cloud-starter-bus-amqp"

application.properties

spring.rabbitmq.host=127.0.0.1

spring.rabbitmq.port=5672

spring.rabbitmq.prod.port=5674

spring.rabbitmq.username=admin

spring.rabbitmq.password=admin

spring.rabbitmq.virtual-host=/

spring.rabbitmq.publisher-confirms=true

# 消费手动确认

spring.rabbitmq.listener.simple.acknowledge-mode=manual

spring.rabbitmq.listener.direct.acknowledge-mode=manual

#消费者最小数量

spring.rabbitmq.listener.simple.concurrency=1

#消费之最大数量

spring.rabbitmq.listener.simple.max-concurrency=10

#在单个请求中处理的消息个数,他应该大于等于事务数量(unack的最大数量)

spring.rabbitmq.listener.simple.prefetch=1

代码

生产者

public void sendJavaObject () throws Exception  {

Order order = new Order ();

order.setId("123");

order.setName("订单标题");

order.setContent("订单数据");

Map<String ,Object> header = Maps.newHashMap();

header.put("test","测试");

MessageHeaders mhs = new MessageHeaders(header);

Message message = MessageBuilder.createMessage(order,mhs);

this.rabbitTemplate.convertAndSend("TopicExchange", "Topic.B.A", message);

}

消费者

@RabbitListener(bindings = @QueueBinding(//绑定队列和交换机

value = @Queue(value = "Topic-B"),//创建queuqe

exchange = @Exchange(value = "TopicExchange",type = "topic"),//创建交换机

key = "Topic.#"//.#是匹配全部,如Topic.a或者Topic.a.b

))

@RabbitHandler

public void TopicReceiverB(@Payload Order order, Channel channel, @Headers Map<String ,Object> headers) throws Exception{

System.out.println(" order : " +order.getName()+"#"+order.getId()+"#"+order.getContent());

System.out.println(" headers : " +headers.toString());

Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);

channel.basicAck(deliveryTag,false);

}

发送对象

注意要  继承序列号的 接口 Serializable

@Data

public class Order implements Serializable {

private String id;

private String name;

private String content;

调用sendJavaObject 执行结果

 

以上是 消息队列08SpringBoot搭建RabbitmqJava对象发送接收 的全部内容, 来源链接: utcz.com/z/516626.html

回到顶部