繁体   English   中英

在 GCP PubSub 和 Spring 引导中捕获发布错误

[英]Catching publish errors in GCP PubSub and Spring Boot

我有一个 Spring 引导应用程序,它需要偶尔将消息发布到 GCP PubSub。 我按照 spring 引导页面( https://spring.io/guides/gs/messaging-gcp-pubsub/ )上的说明实现了它,所以我实现了以下配置文件:

@Configuration
public class PubSubConfiguration {

    @Value("${myprog.pubsub.sms-topic}")
    private String topic;

    @Bean
    @ServiceActivator(inputChannel = "pubsubOutputChannel")
    public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
        return new PubSubMessageHandler(pubsubTemplate, this.topic);
    }

    @MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
    public interface PubsubOutboundGateway {
        void sendToPubsub(String text);
    }
}

从我的 rest controller,我自动连接消息网关并调用sendToPubsub

@RequestMapping("/api/stuff")
@RestController
public class StuffController {

    PubSubConfiguration.PubsubOutboundGateway messagingGateway;

    @Autowired
    public StuffController(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") PubSubConfiguration.PubsubOutboundGateway messagingGateway) {
        this.messagingGateway = messagingGateway;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/go")
    public ResponseEntity<String> send() {
        messagingGateway.sendToPubsub("TEST");
        return new ResponseEntity<>("Ok!", HttpStatus.OK);
    }

}

这可行,但是由于我们的特定用例,如果发布失败,我想以错误响应。 例如,如果我配置了一个不存在的主题,我想返回 500 错误,而它当前返回 200 并稍后异步抛出异常。 有什么方法可以让我在发布时访问未来?

Spring Cloud GCP PubSub 实现使用 Spring 集成框架并依赖它。 为此,您的发送到 PubSub 方法必须抛出异常,如Spring 集成文档中所述

    @MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
    public interface PubsubOutboundGateway {
        void sendToPubsub(String text) throws MessagingException;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM