在 GCP PubSub 和 Spring Boot 中捕获发布错误
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);
}
}
从我的休息控制器,我自动连接消息网关并调用 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);
}
}
这可行,但是由于我们的特殊用例,如果发布失败,我想以错误响应。例如,如果我配置了一个不存在的主题,我想 return 一个 500 错误,而它当前 returns 200 并稍后异步抛出异常。有什么方法可以让我在发布时访问未来?
Spring Cloud GCP PubSub 实现使用 Spring 集成框架并依赖它。为此,您发送到 PubSub 的方法必须抛出异常,如 Spring integration documentation
中所述
@MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
public interface PubsubOutboundGateway {
void sendToPubsub(String text) throws MessagingException;
}
我有一个 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);
}
}
从我的休息控制器,我自动连接消息网关并调用 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);
}
}
这可行,但是由于我们的特殊用例,如果发布失败,我想以错误响应。例如,如果我配置了一个不存在的主题,我想 return 一个 500 错误,而它当前 returns 200 并稍后异步抛出异常。有什么方法可以让我在发布时访问未来?
Spring Cloud GCP PubSub 实现使用 Spring 集成框架并依赖它。为此,您发送到 PubSub 的方法必须抛出异常,如 Spring integration documentation
中所述 @MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
public interface PubsubOutboundGateway {
void sendToPubsub(String text) throws MessagingException;
}