C'è un modo in Java per gestire un SIGTERM ricevuto?Come gestire un SIGTERM
risposta
Sì, è possibile registrare un gancio di arresto con Runtime.addShutdownHook()
.
È possibile aggiungere un shutdown hook per eseguire qualsiasi operazione di pulizia.
Ti piace questa:
public class myjava{
public static void main(String[] args){
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Inside Add Shutdown Hook");
}
});
System.out.println("Shut Down Hook Attached.");
System.out.println(5/0); //Operating system sends SIGFPE to the JVM
//the JVM catches it and constructs a
//ArithmeticException class, and since you
//don't catch this with a try/catch, dumps
//it to screen and terminates. The shutdown
//hook is triggered, doing final cleanup.
}
}
quindi eseguirlo:
[email protected]:~$ javac myjava.java
[email protected]:~$ java myjava
Shut Down Hook Attached.
Exception in thread "main" java.lang.ArithmeticException:/by zero
at myjava.main(myjava.java:11)
Inside Add Shutdown Hook
Un altro modo per gestire segnali in Java è tramite il pacchetto sun.misc.Signal. Fare riferimento a http://www.ibm.com/developerworks/java/library/i-signalhandling/ per capire come usarlo.
NOTA: La funzionalità nel pacchetto sole. * Significherebbe anche che potrebbe non essere portabile/comportarsi allo stesso modo su tutti i sistemi operativi. Ma potresti volerlo provare.
Ma in effetti, fa lo stesso? –
No, non esattamente. L'API di gestione dei segnali che ho menzionato può essere utilizzata per gestire la maggior parte dei segnali del sistema operativo nativo (trap, intr, kill, ecc.) All'interno delle app Java. D'altra parte ShutdownHook viene richiamato solo quando JVM sta finalmente terminando (dopo che è stato ricevuto un segnale di kill). – arcamax
@arcamax il collegamento sembra essere rotto – GreenGiant
- 1. bash pipe & SIGTERM
- 2. parentesi haskells su SIGTERM
- 3. Come gestire e mantenere correttamente l'arresto del sistema (e SIGTERM) per completare il lavoro in Python?
- 4. Win32 API analogico di invio/cattura SIGTERM
- 5. Perché spark executor riceve SIGTERM?
- 6. Registri di Heroku FATAL SignalException: SIGTERM
- 7. Come eseguire l'arresto personalizzato su SIGTERM con Akka?
- 8. Come gestire un IEnumerable "infinito"?
- 9. Pyramid come gestire un xhr.abort()
- 10. come posso uccidere un processo Linux in java con SIGKILL Process.destroy() fa SIGTERM
- 11. È possibile che un programma acquisisca SIGTERM generato dalla chiamata di sistema di uscita?
- 12. Git: gestire un appcache?
- 13. Ripristino pulito da Resque :: TermException o SIGTERM su Heroku
- 14. Come gestire le traduzioni per un ActiveModel?
- 15. flask-jwt Come gestire un token?
- 16. Come gestire KeyEvents in un DataGridViewCell?
- 17. Come gestire un difetto in System.Data.DataTableExtensions.CopyToDataTable()
- 18. come gestire un asintoto/discontinuità con Matplotlib
- 19. Come gestire valori errati in un costruttore?
- 20. Come gestire un DialogFragment con RxJava?
- 21. Come gestire i divisori in un PreferenceFragment?
- 22. Come gestire spegnimento pulsante su un notebook
- 23. Come gestire eccezione su un argomento comportamento
- 24. Come gestire un repository IDisposable con Unity?
- 25. Come gestire EINTR (chiamata di sistema interrotta)
- 26. Come gestire MaxUploadSizeExceededException
- 27. Backbone.js - come gestire "login"?
- 28. Come gestire l'errore AsyncTask
- 29. Come gestire ERR_INSECURE_RESPONSE?
- 30. Come gestire l'importazione incrociata?
Il post di blog è 404 – luckydonald