java-gnome version 4.0.19

org.gnome.unique
Class Application

Object
  extended by org.freedesktop.bindings.Pointer
      extended by org.freedesktop.bindings.Proxy
          extended by org.gnome.glib.Object
              extended by org.gnome.unique.Application

public class Application
extends Object

Facilities for ensuring that only one unique instance of an application is running.

If you're not the first, go away

The basic algorithm is to create an Application instance with the unique name corresponding to your prgram, and then check to see if there is another already running. If so, then there is no need for this instance and it should terminate.

 app = new Application("com.example.Program", null);
 
 if (app.isRunning()) {
     System.exit(1);
 }
 

Sending commands

There is a message-passing facility built into LibUnique which allows you to send basic commands to the other running instance before you terminate this one.

 app.sendMessage(Command.ACTIVATE, null);
 

Keep in mind that this is not really meant as a generic all-powerful n to m interprocess communication mechanism. In fact, the entire LibUnique library is mostly about ensuring you only have one master copy of an application running. But once you've established that, the messaging subsystem can be used to convey simple requests to of that unique instance.

Since:
4.0.12
Author:
Andrew Cowie

Nested Class Summary
static interface Application.MessageReceived
          The signal emitted when another instance sends a message to the unique instance.
 
Constructor Summary
Application(String name, String id)
          Construct an Application object for the specified unique service.
 
Method Summary
 void connect(Application.MessageReceived handler)
          Hookup a Application.MessageReceived handler.
 String getName()
          Get the application name that was used when this Application was constructed.
 boolean isRunning()
          Is some other instance of this program (ie the application with this name) already running?
 Response sendMessage(Command cmd, MessageData data)
          Send a message to the other instance (assuming there is one).
 
Methods inherited from class org.freedesktop.bindings.Pointer
toString
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Application

public Application(String name,
                   String id)
Construct an Application object for the specified unique service.

By convention, the name chosen to identify a unique application should follow the application naming conventions used by DBus (these are similar to the domain name -esque conventions used in Java package space). Some examples are:

Since:
4.0.12
Method Detail

connect

public void connect(Application.MessageReceived handler)
Hookup a Application.MessageReceived handler.

Since:
4.0.12

getName

public String getName()
Get the application name that was used when this Application was constructed.

Since:
4.0.12

isRunning

public boolean isRunning()
Is some other instance of this program (ie the application with this name) already running?

If so, then really you probably want to be terminating this program. If not, then carry on with your normal application initialization and start-up.

Since:
4.0.12

sendMessage

public Response sendMessage(Command cmd,
                            MessageData data)
Send a message to the other instance (assuming there is one).

This method blocks.

If you have specific information to convey to the other instance, you can create a MessageData object and set its payload accordingly. You can also create custom Commands through subclassing, although that's rarely necessary.

You can get a number of reponses back from the other instance. You should reasonably expect OK. Indeed, if it's not ok there isn't much you can do about it.

Since:
4.0.12


java-gnome