Spring 在事务中启动正常关闭
Spring boot graceful shutdown mid-transaction
我正在开发一项执行敏感支付处理的 spring-boot 服务,并希望确保在不中断这些交易的情况下关闭应用程序。想知道如何在 spring-boot.
中最好地解决这个问题
我阅读了有关向 spring-boot 添加关闭挂钩的信息,并且我在想也许可以在 class 上使用 CountDownLatch
来检查线程是否已完成处理 - 某事像这样:
@Service
public class PaymentService {
private CountDownLatch countDownLatch;
private void resetLatch() {
this.countDownLatch = new CountDownLatch(1);
}
public void processPayment() {
this.resetLatch();
// do multi-step processing
this.CountDownLatch.countDown();
}
public void shutdown() {
// blocks until latch is available
this.countDownLatch.await();
}
}
// ---
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// init app and get context
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
// retrieve bean needing special shutdown care
PaymentService paymentService = context.getBean(PaymentService.class);
Runtime.getRuntime().addShutdownHook(new Thread(paymentService::shutdown));
}
}
非常感谢建设性的反馈 - 谢谢。
我最终在关机方法上使用了 @PreDestroy
annotation:
@Service
public class PaymentService {
private CountDownLatch countDownLatch;
private synchronized void beginTransaction() {
this.countDownLatch = new CountDownLatch(1);
}
private synchronized void endTransaction() {
this.countDownLatch.countDown();
}
public void processPayment() {
try {
this.beginTransaction();
// - - - -
// do multi-step processing
// - - - -
} finally {
this.endTransaction();
}
}
@PreDestroy
public void shutdown() {
// blocks until latch is available
this.countDownLatch.await();
}
}
我正在开发一项执行敏感支付处理的 spring-boot 服务,并希望确保在不中断这些交易的情况下关闭应用程序。想知道如何在 spring-boot.
中最好地解决这个问题我阅读了有关向 spring-boot 添加关闭挂钩的信息,并且我在想也许可以在 class 上使用 CountDownLatch
来检查线程是否已完成处理 - 某事像这样:
@Service
public class PaymentService {
private CountDownLatch countDownLatch;
private void resetLatch() {
this.countDownLatch = new CountDownLatch(1);
}
public void processPayment() {
this.resetLatch();
// do multi-step processing
this.CountDownLatch.countDown();
}
public void shutdown() {
// blocks until latch is available
this.countDownLatch.await();
}
}
// ---
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// init app and get context
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
// retrieve bean needing special shutdown care
PaymentService paymentService = context.getBean(PaymentService.class);
Runtime.getRuntime().addShutdownHook(new Thread(paymentService::shutdown));
}
}
非常感谢建设性的反馈 - 谢谢。
我最终在关机方法上使用了 @PreDestroy
annotation:
@Service
public class PaymentService {
private CountDownLatch countDownLatch;
private synchronized void beginTransaction() {
this.countDownLatch = new CountDownLatch(1);
}
private synchronized void endTransaction() {
this.countDownLatch.countDown();
}
public void processPayment() {
try {
this.beginTransaction();
// - - - -
// do multi-step processing
// - - - -
} finally {
this.endTransaction();
}
}
@PreDestroy
public void shutdown() {
// blocks until latch is available
this.countDownLatch.await();
}
}