我正在尝试设置一个向RabbitMQ发布消息的Integration Workflow。
我有两个问题: 1.我的队列Bean是否正常工作,我希望它是:) 2.如何使用Integration DSL使用outbound-amqp-adapter设置消息的优先级?
@Configuration
public class RabbitConfig {
@Autowired
private ConnectionFactory rabbitConnectionFactory;
@Bean
TopicExchange worksExchange() {
return new TopicExchange("work.exchange", true, false);
}
@Bean
Queue queue() {
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-priority", 10);
return new Queue("dms.document.upload.queue", true, false, false, args);
}
@Bean
public RabbitTemplate worksRabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory);
template.setExchange("work.exchange");
template.setRoutingKey("work");
template.setConnectionFactory(rabbitConnectionFactory);
return template;
}
@Configuration
public class WorksOutbound {
@Autowired
private RabbitConfig rabbitConfig;
@Bean
public IntegrationFlow toOutboundQueueFlow() {
return IntegrationFlows.from("worksChannel")
.transform(Transformers.toJson())
.handle(Amqp.outboundAdapter(rabbitConfig.worksRabbitTemplate()))
.get();
}
}
更新 在能够使用适当的“优先级标头”推送消息之后,我可以使用Rabbit Management UI根据其优先级提取消息,但我无法使用spring-amqp使用者正确地提取消息...
@Bean
public SimpleMessageListenerContainer workListenerContainer() {
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(rabbitConnectionFactory);
container.setQueues(worksQueue());
container.setConcurrentConsumers(2);
container.setDefaultRequeueRejected(false);
return container;
}
答案 0 :(得分:1)
看起来不错。
在.handle()
之前,使用标题名为.enrichHeaders(...)
的{{1}}和一个整数值。
修改强>
IntegrationMessageHeaderAccessor.PRIORITY
和
@SpringBootApplication
public class So49692361Application {
public static void main(String[] args) {
SpringApplication.run(So49692361Application.class, args);
}
@Bean
public ApplicationRunner runner(SimpleMessageListenerContainer container, ApplicationContext ctx) {
return args -> {
Gate gate = ctx.getBean(Gate.class);
gate.send(new GenericMessage<>("foo", Collections.singletonMap("foo", 1)));
gate.send(new GenericMessage<>("bar", Collections.singletonMap("foo", 2)));
container.start();
};
}
@Bean
public static IntegrationFlow flow(AmqpTemplate amqpTemplate) {
return IntegrationFlows.from(Gate.class)
.enrichHeaders(h -> h.headerExpression(IntegrationMessageHeaderAccessor.PRIORITY,
"headers.foo"))
.handle(Amqp.outboundAdapter(amqpTemplate).routingKey("so49692361"))
.get();
}
@Bean
public Queue queue() {
return new Queue("so49692361", true, false, false, Collections.singletonMap("x-max-priority", 5));
}
@Bean
public SimpleMessageListenerContainer container(ConnectionFactory cf) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
container.setQueues(queue());
container.setMessageListener(m -> {
System.out.println(m);
});
container.setAutoStartup(false);
return container;
}
public interface Gate {
public void send(Message<?> message);
}
}