17 Temmuz 2023 Pazartesi

SpringContext ApplicationReadyEvent Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.springframework.boot.context.event.ApplicationReadyEvent;
Açıklaması şöyle
Event published as late as conceivably possible to indicate that the application is ready to service requests. The source of the event is the SpringApplication itself, but beware of modifying its internal state since all initialization steps will have been completed by then.
Örnek
Şöyle yaparız. Burada temizlik işi yaptıktan sonra uygulama kapatılıyor. Bu tazr şeyler Kubertenes Cronjob ile kullanılabilir
@Component
public class RunAfterStartup {
   
  @Autowired
  private BusinessService businessService;
    
  @Autowired
  private ApplicationContext context;

  @EventListener(ApplicationReadyEvent.class)
  public void runAfterStartup() {
    businessService.performCleanup();
    shutdown();
  }

  private void shutdown() {
    int exitCode = SpringApplication.exit(context, () -> 0);
    System.exit(exitCode);
  }
}

Hiç yorum yok:

Yorum Gönder