单个队列,多个 @RabbitListener 但不同的服务 [英] Single Queue, multiple @RabbitListener but different services

查看:24
本文介绍了单个队列,多个 @RabbitListener 但不同的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有一个@RabbitListener,例如:

Is it possible to have a single @RabbitListener, e.g:

@RabbitListener(queues = STORAGE_REQUEST_QUEUE_NAME)
public FindApplicationByIdResponse findApplicationById(FindApplicationByIdRequest request) {
    return repository.findByUuid(request.getId())
            .map(e -> new FindApplicationByIdResponse(conversionService.convert(e, Application.class)))
            .orElse(new FindApplicationByIdResponse(null));
}

@RabbitListener(queues = STORAGE_REQUEST_QUEUE_NAME)
public PingResponse ping(PingRequest request) {
    return new PingResponse();
}

而在消费者端,它会将请求发送到同一个请求队列,但操作不同?现在它将对象从 Json 转换为对象(例如:FindApplicationByIdRequest 或 PingRequest).

And on the consumer side, it will send requests to the same request queue, but with different operations? Now it converts the objects from Json to a object (e.g.: FindApplicationByIdRequest, or PingRequest).

但是现在,当我把它拿回来时:

But now, when i get it back:

@Override
public FindApplicationByIdResponse findApplicationById(FindApplicationByIdRequest request) {
    Object object = template.convertSendAndReceive(Queues.STORAGE_REQUEST_QUEUE_NAME, request);
    return handleResponse(FindApplicationByIdResponse.class, object);
}

@Override
public PingResponse ping(PingRequest request) {
    Object object = template.convertSendAndReceive(Queues.STORAGE_REQUEST_QUEUE_NAME, request);
    return handleResponse(PingResponse.class, object);
}

似乎无法将两者关联起来.所以我调用 ping 方法,然后在该方法中返回 FindApplicationByIdResponse.这是为什么呢?

It looks like it failed to correlate the two. So I call the ping method, then I get a FindApplicationByIdResponse back in that method. Why is that?

当我为他们使用不同的队列时,它工作正常.但是我最终不得不创建很多队列来支持我希望进行的所有 RPC 调用.任何人都知道是否可以使用请求类型作为它要使用的限定符?

When I used different queues for them, it works fine. But I end up having to make a lot of queues to support all the RPC calls I wish to make. Anyone know if its possible to use the request type as a qualifier to which one it's going to use?

推荐答案

在方法级别上使用 @RabbitListener 不起作用,但在类级别上使用 是可能的@RabbitHandler 关于方法:

That doesn't work with @RabbitListener on the method level, but it is possible on the class level with the @RabbitHandler on methods:

@RabbitListener(queues = STORAGE_REQUEST_QUEUE_NAME)
public class MultiListenerBean {

    @RabbitHandler
    public String bar(Bar bar) {
        ...
    }

    @RabbitHandler
    public String baz(Baz baz) {
        ...
    }

    @RabbitHandler
    public String qux(@Header("amqp_receivedRoutingKey") String rk, @Payload Qux qux) {
        ...
    }

}

https://docs.spring.io/spring-amqp/reference/html/#annotation-method-selection

这篇关于单个队列,多个 @RabbitListener 但不同的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆