Using Windows. I’m writing an application that opens a file using an editor that I specify
— either I specify this in the OS as a ‘default’, or I will use Runtime.exec to specify the software I want to open the file with.
That file is a Java file itself. It will contain a class with a main method, and an unimplemented method. I will implement that method, and run the main method, which does some validation of my implemented method. When I am done working with this file, I will simply close it via the editor.
When I close the file, I want the spring-shell process to get a callback that says ‘that file you opened earlier is now closed, run some code in response – i.e., write something to a database, stop a timer, etc.’
— I was looking at shutdown hooks for this, but it seems to not work for my use case. I don’t want this to run when I close the spring-shell app, I want it to run when the file that gets opened by my spring-shell app gets closed.
Is there a pattern I can use to accomplish this?
My plan so far:
-
provide a command-line interface that allows me to bind commands to programmatic actions executed by Java methods (hence the usage of spring-shell).
-
Based on arguments I specify to spring-shell, open a file in the system-default text editor
— I plan to use java.awt.Desktop to do this.
- Here is where I’m stuck: When I close the file, I want the spring-shell process to get a callback that says ‘that file you opened earlier is now closed, run some code in response – i.e., write something to a database, stop a timer, etc.’
— I was looking at shutdown hooks for this, but it seems to not work for my use case. I don’t want this to run when I close the spring-shell app, I want it to run when the file that gets opened by my spring-shell app gets closed.
sdatt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.