package de.avatic.lcc.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class ShutdownListener {

    private static final Logger log = LoggerFactory.getLogger(ShutdownListener.class);

    @EventListener
    public void onShutdown(ContextClosedEvent event) {
        log.error("Application shutdown. Context: {}, Thread: {}", event.getApplicationContext(), Thread.currentThread().getName());

        Runtime runtime = Runtime.getRuntime();
        long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
        log.info("Memory: {} used, {} total, {} free, {} max ", usedMemory, runtime.totalMemory() / 1024 / 1024, runtime.freeMemory() / 1024 / 1024, runtime.maxMemory() / 1024 / 1024);



          log.error("Thread stack dump:");
        Thread.dumpStack();
    }
}