Single queue, multiple @RabbitListener, but different services

Is it possible to have a single @RabbitListener like:

@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();
}

      

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

But now when I return it:

@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);
}

      

It looks like it was not possible to match the two. So I call the ping method and then return FindApplicationByIdResponse to that method. Why is this?

When I used different queues for them, it works fine. But I have to do a lot of queues to support all the RPC calls I want to make. Does anyone know if it is possible to use this type of query as the qualifier to which it will be used?

+3


source to share


1 answer


It doesn't work with @RabbitListener

method level, but it is possible at class level with @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) {
        ...
    }

}

      



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

+2


source







All Articles