1

I am new to javaFX and I am working on a desktop application. In a few words this application communicates with a server through rest (there is an autentication system). I want to communicate to the server the "logout" before every possible case of app closing. For now, i wrote this snippet in Application class:

@Override
public void stop(){
    Session.logout();
}

And it helps me to communicate the logout if the application is closing. But, for example, if I close the application from the IDE (I know that i n production it is not possible), the application does not fire the stop() method. The same is if I force the application to close from the OS. Is there anything to prevent the involuntary close of the application?

Stefano Miceli
  • 263
  • 1
  • 4
  • 17
  • Your question is impossible to answer. If someone asked you this question, what would you need to know in order to answer it? – nicomp Jan 10 '18 at 14:32
  • 4
    There may always be a chance that your application will crash and stop without logging out from the server. So, perhaps you should have your Session expire after a certain amount of inactivity instead of requiring a logout? – MMAdams Jan 10 '18 at 14:37
  • Right. I think I would avoid the possible crashes caused by some internal Exception and i'd focus on the os forced exit (is the only one that I have in mind in this moment). Should I deal with the prevention of object destroy? – Stefano Miceli Jan 10 '18 at 14:39
  • 1
    I don't know if this will help, but have you also tried to use `stage.setOnCloseRequest()`? – SedJ601 Jan 10 '18 at 14:39
  • 4
    There's no way you can account for all possible scenarios. If the OS forcibly terminates the JVM immediately, nothing you write in Java will matter - there won't be a JVM to execute your code. (And similarly, if the client just turns off the network router, even if your code executed you'd be unable to send a logout message to the server.) The only other thing you might consider is using a shutdown hook. See [this discussion](https://stackoverflow.com/questions/42598097/using-javafx-application-stop-method-over-shutdownhook) – James_D Jan 10 '18 at 14:46
  • 3
    Consider the possibility of a power outage which stops your application while the server keeps running remotely, or consider what happens with a simple connection failure or if the network goes down, when the application cannot effectively communicate to the server to logout, even though by any reasonable definition the two are no longer connected. I would strongly suggest your sessions simply expire after a while. – MMAdams Jan 10 '18 at 14:46
  • 1
    @Sedrick The OP is asking about the application exiting, not an individual window closing. – James_D Jan 10 '18 at 14:46
  • I wasn't sure if that would help. I was just thinking that if `setOnCloseRequest()` was set on the `primaryStage`, that would catch one situation. – SedJ601 Jan 10 '18 at 14:50
  • I used shutdownhook on the runtime and the app execute the command when I stop from the IDE, that is good enough. I will implemen the session expire and if I find other situation I'll write here! – Stefano Miceli Jan 10 '18 at 15:25

0 Answers0