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

查看:1124
本文介绍了单个队列,多个@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天全站免登陆