3

I would like to restrict my Eclipse-RCP application to a single instance. By this, I mean that once a user opens the application for the first time, it listens on a port and for the second access it should open the previous instance instead of showing a warning message like "already an instance is running"

My RCP Application code:

ApplicationInstanceListener.java interface code

public interface ApplicationInstanceListener
{
    public void newInstanceCreated();
}

ApplicationInstanceManager.java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class ApplicationInstanceManager {
        private static ApplicationInstanceListener subListener;

    /** Randomly chosen, but static, high socket number */
    public static final int SINGLE_INSTANCE_NETWORK_SOCKET = 2020;

    /** Must end with newline */
    public static final String SINGLE_INSTANCE_SHARED_KEY = "$$NewInstance$$\n";

    /**
     * Registers this instance of the application.
     *
     * @return true if first instance, false if not.
     */
    public static boolean registerInstance() {
        // returnValueonerror should be true if lenient (allows app to run on network error) or false if strict.
        boolean returnValueonerror = true;
        // try to open network socket
        // if success, listen to socket for new instance message, return true
        // if unable to open, connect to existing and send new instance message, return false
        try {
            final ServerSocket socket = new ServerSocket(SINGLE_INSTANCE_NETWORK_SOCKET, 10, InetAddress
                    .getLocalHost());
           System.out.println("Listening for application instances on socket " + SINGLE_INSTANCE_NETWORK_SOCKET);
            Thread instanceListenerThread = new Thread(new Runnable() {
                public void run() {
                    boolean socketClosed = false;
                    while (!socketClosed) {
                        if (socket.isClosed()) {
                            socketClosed = true;
                        } else {
                            try {
                                Socket client = socket.accept();
                                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                                String message = in.readLine();
                                if (SINGLE_INSTANCE_SHARED_KEY.trim().equals(message.trim())) {
                                        System.out.println("Shared key matched - new application instance found");
                                    fireNewInstance();
                                }
                                in.close();
                                client.close();
                            } catch (IOException e) {
                                socketClosed = true;
                            }
                        }
                    }
                }
            });
            instanceListenerThread.start();
            // listen
        } catch (UnknownHostException e) {
                System.out.println(e.getMessage());
            return returnValueonerror;
        } catch (IOException e) {
                System.out.println("Port is already taken.  Notifying first instance.");
            try {
                Socket clientSocket = new Socket(InetAddress.getLocalHost(), SINGLE_INSTANCE_NETWORK_SOCKET);
                OutputStream out = clientSocket.getOutputStream();
                out.write(SINGLE_INSTANCE_SHARED_KEY.getBytes());
                out.close();
                clientSocket.close();
                System.out.println("Successfully notified first instance.");
                return false;
            } catch (UnknownHostException e1) {
                System.out.println(e.getMessage());
                return returnValueonerror;
            } catch (IOException e1) {
                System.out.println("Error connecting to local port for single instance notification");
                System.out.println(e1.getMessage());
                return returnValueonerror;
            }

        }
        return true;
    }

    public static void setApplicationInstanceListener(ApplicationInstanceListener listener) {
        subListener = listener;
    }

    private static void fireNewInstance() {
      if (subListener != null) {
        subListener.newInstanceCreated();
      }
  }
}

Application.java code

import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

        /*
         * (non-Javadoc)
         *
         * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
         * IApplicationContext)
         */
        public Object start(IApplicationContext context) throws Exception {
                if (!ApplicationInstanceManager.registerInstance()) {
                        // instance already running.
                        System.out
                                        .println("Another instance of this application is already running.  Exiting.");
                        MessageDialog
                                        .openInformation(new Shell(), "Information",
                                                        "Another instance of this application is already running.  Exiting.");
                        System.exit(0);
                }
                Display display = PlatformUI.createDisplay();
                try {
                        int returnCode = PlatformUI.createAndRunWorkbench(display,
                                        new ApplicationWorkbenchAdvisor());
                        if (returnCode == PlatformUI.RETURN_RESTART)
                                return IApplication.EXIT_RESTART;
                        else
                                return IApplication.EXIT_OK;
                } finally {
                        display.dispose();
                }

        }

        /*
         * (non-Javadoc)
         *
         * @see org.eclipse.equinox.app.IApplication#stop()
         */
        public void stop() {
                if (!PlatformUI.isWorkbenchRunning())
                        return;
                final IWorkbench workbench = PlatformUI.getWorkbench();
                final Display display = workbench.getDisplay();
                display.syncExec(new Runnable() {
                        public void run() {
                                if (!display.isDisposed())
                                        workbench.close();
                        }
                });
        }
}

I've taken a simple RCP application with view as a template. The above code works fine but doesn't open previous instance like skype or windows media player despite it shows an alert like below enter image description here

How can I show or open the previous instance upon second run of the application?

Animesh
  • 4,926
  • 14
  • 68
  • 110
srk
  • 4,857
  • 12
  • 65
  • 109

4 Answers4

2

I have an app that does this same thing. The trick is that the new instance can't bring the old instance to the front. But, the old instance can bring itself to the front after it contacts the new instance.

So your old instance needs to call

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().forceActive();

after it notifies the new instance. For my app, the new instance doesn't show an error message, it just closes transparently and the old instance pops itself back up.

Ned Twigg
  • 2,159
  • 2
  • 22
  • 38
0

Basically you can have functionality like eclipse. Eclipse maintains a .lock file to lock the workspace. You can similarly create an empty .lock file in your workspace.

On starting every instance, you should check if .lock file is present and then proceed further accordingly. If file is not present you should create it so that other instance will find that workspace is locked.

SaurabhJinturkar
  • 554
  • 7
  • 20
0

Have a look at this article: Single instance of RCP application. The author describes the same pattern of using a server socket which you are asking about.

Kai
  • 38,985
  • 14
  • 88
  • 103
  • @raghav: You are right, just create one at the beginning of the `start()` method: `WorkbenchAdvisor advisor = new ApplicationWorkbenchAdvisor();` – Kai Dec 27 '11 at 09:18
0

i think you should just alternate to you already running instance.
i don't know if this or this link could help, but thats all i got

really hope it helps

Community
  • 1
  • 1
Luiz E.
  • 6,769
  • 10
  • 58
  • 98