Ken*_*rud 4 amqp rabbitmq spring-amqp
是否可以有一个@RabbitListener,例如:
@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();
}
Run Code Online (Sandbox Code Playgroud)
而在消费者端,它会将请求发送到同一个请求队列,但操作不同?现在它将对象从 Json 转换为对象(例如:FindApplicationByIdRequest 或 PingRequest)。
但是现在,当我取回它时:
@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);
}
Run Code Online (Sandbox Code Playgroud)
看起来它无法将两者关联起来。所以我调用 ping 方法,然后在该方法中返回 FindApplicationByIdResponse。这是为什么?
当我为它们使用不同的队列时,它工作正常。但是我最终不得不制作很多队列来支持我希望进行的所有 RPC 调用。任何人都知道是否可以使用请求类型作为它将使用哪个类型的限定符?
这不适@RabbitListener用于方法级别,但可以在类级别使用@RabbitHandleron 方法:
@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) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
https://docs.spring.io/spring-amqp/reference/html/#annotation-method-selection
| 归档时间: |
|
| 查看次数: |
4088 次 |
| 最近记录: |