0

I have a Java process that runs in the background. How can I quickly signal the process to show its window? I want a really light-weight script that can do this and can be launched from the Start Menu.

I think maybe a BAT file that checks if the lock file has been touched in the last few seconds, signal the process, otherwise, create a new one. The signal could be by creating another file that the process would be listening for.

That works, but it seems inefficient. Hesitation sounds unavoidable.

I could use Java instead of a BAT file, still that leaves the question of how to signal the background process. This only has to work in Windows, but I am good with Java so that is what I am using.

Any ideas?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
700 Software
  • 85,281
  • 83
  • 234
  • 341

2 Answers2

3

One option would be to have that process having a listener on a port (as an example 8888), then you could send a message to that port (or do something like telnet localhost 8888). The running processes could have a separate thread listening on that port.

Another option would be to use JMX communication with the JVM - see http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html

BZ.
  • 1,928
  • 2
  • 17
  • 26
  • If it did not have so many requirements, I would use JMX. It seems a little complex. I will use the listener method. Not using port 8888 because I want to prevent conflicts. Probably something random. My accepted method here: http://stackoverflow.com/questions/6345153/bat-if-statement-based-on-modify-date-of-file/6388780#6388780 – 700 Software Jun 17 '11 at 20:29
1

Is there anything preventing you from checking the lock file from your Java process? You could use the Observer pattern to alert the main thread (or which ever thread) to changes in the file.

For example:


public class FileWatcher implements Observable {

  private long lastModified;
  private final File file;

  public FileWatcher(File f) {
     this.file = f;
     this.lastModified = file.lastModified();

     Thread t = new Thread() {
        public void run() {
           while(!stopThread) {
           if(lastModified < file.lastModified()) {
              lastModified = file.lastModified();
              setChanged();
              notifyObservers();

           }    
           Thread.currentThread().sleep(5);      
           }
        }
     };

     t.start();
  }
}

DISCLAIMER: not tested or verified at all, but I'm sure you get the idea.

EDIT: oops, forgot the loop.

EDIT: new idea.

I have another idea. (I know you already accepted an answer, but I wanted to throw this out there.) Would it be possible to use the select function? In my very limited skim of the MSDN docs, this is only mentioned this in the context of sockets. I know the Linux equivalent is applicable to any file descriptor.

Instead of simply polling the file in the thread I mentioned above, let the OS do it! Pass the file into the writefds set to select and then it'll return when the file is modified. This means your process isn't spending valuable CPU time waiting for changes to the file.

I haven't verified whether or not Java exposes this call in the JDK, so it might require writing a JNI interface to get it to work. My knowledge in this area is a little fuzzy, sorry.

EDIT again again:

Found it! Java's Selector class looks like it implements select. Unfortunately, FileChannel isn't selectable, which is probably required in this case. :(

Tom
  • 18,685
  • 15
  • 71
  • 81
  • I want to avoid tapping a file periodically. That was the idea I mentioned already. *"The signal could be by creating another file that the process would be listening for. That works, but it seems inefficient. Hesitation sounds unavoidable."*. In your above example, you are checking the file 200 times a second. That sounds like it would slow down the PC, although, it would remove the hesitation :) – 700 Software Jun 17 '11 at 20:27
  • That's a good point, but what triggers the signal? For example, if you're waiting for someone to modify the file (and you're just polling using a script) don't you have the same problem? – Tom Jun 17 '11 at 22:04
  • Also, I had another wild idea--see the 2nd EDIT if you're curious. :) – Tom Jun 17 '11 at 22:11
  • I am not sure what you are asking in your first comment. The *"idea I mentioned already"* was the one I was trying to avoid. The item that triggers the signal is a shortcut on the desktop that runs a command (touch a file, or in my accepted solution, send a UDP packet). The user clicks the shortcut and that brings my app to the front. Thanks for your new idea anyway... – 700 Software Jun 17 '11 at 22:59
  • Oh, I see. Sorry--I misunderstood. – Tom Jun 17 '11 at 23:03