【问题标题】:Catching publish errors in GCP PubSub and Spring Boot在 GCP PubSub 和 Spring Boot 中捕获发布错误
【发布时间】:2020-10-13 09:41:26
【问题描述】:

我有一个 Spring Boot 应用程序,它需要偶尔将消息发布到 GCP PubSub。我按照 Spring Boot 页面 (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);
    }
}

从我的休息控制器,我自动连接消息网关并调用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 并稍后异步抛出异常。有什么方法可以让我在发布时访问未来?

【问题讨论】:

    标签: java spring-boot google-cloud-pubsub


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2019-06-27
      • 2019-10-25
      • 2019-05-19
      • 2019-02-03
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多