java—在客户端处理rabbit侦听器异常以传递给rest端点

p1iqtdky  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(171)

我正在使用micronaut framework 2.2.1和rabbitmq消息传递系统。
制作人

@RabbitClient(ProductTopicConstants.FETE_BIRD_EXCHANGE)
@RabbitProperty(name = "replyTo", value = "amq.rabbitmq.reply-to")
public interface IProductProducer {
@Binding(ProductTopicConstants.GET_PRODUCT)
    Maybe<ProductViewModel> findById(String id);
}

听众

@RabbitListener
public class ProductListener {

@Queue(ProductTopicConstants.GET_PRODUCT)
    public ProductViewModel find(String id) {
        try {
            LOG.info(String.format("Listener --> Getting product for specified id = %s", id));
            ProductSearchCriteria criteria = new ProductSearchCriteria();
            criteria.setId(id);
            Bson query = QueryBuilder.QueryBuilder(criteria, Bson.class).get(0);
            ProductViewModel productViewModel = Single.fromPublisher(
                    repository.getCollection(ProductConstrants.PRODUCT_COLLECTION_NAME, Product.class)
                            .find(query)).map(successValue -> {
                return new ProductViewModel(
                        successValue.getId().toString(),
                        successValue.getName(),
                        successValue.getDescription(),
                        successValue.getPrice()
                );
            }).blockingGet();
            return productViewModel;
        } catch (Exception ex) {
            throw ex;
        }
    }
}

我有一个 RabbitListenerExceptionHandler 从侦听器捕获异常。

@Singleton
@Primary
@Replaces(DefaultRabbitListenerExceptionHandler.class)
public class RabbitListenerExceptionHandler implements io.micronaut.rabbitmq.exception.RabbitListenerExceptionHandler {
    private static final Logger LOG = LoggerFactory.getLogger(RabbitListenerExceptionHandler.class);
    @Override
    public void handle(RabbitListenerException exception) {
        if (LOG.isErrorEnabled()) {
            Optional<RabbitConsumerState> messageState = exception.getMessageState();
            if (messageState.isPresent()) {
                LOG.error("Error processing a message for RabbitMQ consumer [" + exception.getListener() + "]", exception);
            } else {
                LOG.error("RabbitMQ consumer [" + exception.getListener() + "] produced an error", exception);
            }
        }
    }
}

现在我需要将此异常抛出到rest端点,以便前端或api使用者知道问题的原因。
我如何才能做到这一点,任何帮助将不胜感激

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题