Search in sources :

Example 1 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project jdk8u_jdk by JetBrains.

the class Main method setupServer.

/**
     * Setup benchmark server.
     */
static void setupServer() {
    switch(runmode) {
        case SAMEVM:
            try {
                serverImpl = new BenchServerImpl();
                server = (BenchServer) RemoteObject.toStub(serverImpl);
            } catch (RemoteException e) {
                die("Error: failed to create local server: " + e);
            }
            if (verbose)
                System.out.println("Benchmark server created locally");
            break;
        case CLIENT:
            try {
                Registry reg = LocateRegistry.getRegistry(host, port);
                server = (BenchServer) reg.lookup(REGNAME);
            } catch (NotBoundException | RemoteException e) {
                die("Error: failed to connect to server: " + e);
            }
            if (server == null) {
                die("Error: server not found");
            }
            if (verbose) {
                System.out.println("Connected to benchmark server on " + host + ":" + port);
            }
            break;
        case SERVER:
            try {
                Registry reg = LocateRegistry.createRegistry(port);
                serverImpl = new BenchServerImpl();
                reg.bind(REGNAME, serverImpl);
            } catch (AlreadyBoundException | RemoteException e) {
                die("Error: failed to initialize server: " + e);
            }
            if (verbose) {
                System.out.println("Benchmark server started on port " + port);
            }
            break;
        default:
            throw new InternalError("illegal runmode");
    }
}
Also used : AlreadyBoundException(java.rmi.AlreadyBoundException) NotBoundException(java.rmi.NotBoundException) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RemoteException(java.rmi.RemoteException)

Example 2 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project jackrabbit-oak by apache.

the class RepositoryStartupServlet method registerRMI.

/**
 * Registers the repository to an RMI registry configured in the web
 * application. See <a href="#registerAlgo">Registration with RMI</a> in the
 * class documentation for a description of the algorithms used to register
 * the repository with an RMI registry.
 * @throws ServletException if an error occurs.
 */
private void registerRMI() {
    RMIConfig rc = config.getRmiConfig();
    if (!rc.isValid() || !rc.enabled()) {
        return;
    }
    // try to create remote repository
    Remote remote;
    try {
        Class<?> clazz = Class.forName(getRemoteFactoryDelegaterClass());
        RemoteFactoryDelegater rmf = (RemoteFactoryDelegater) clazz.newInstance();
        remote = rmf.createRemoteRepository(repository);
    } catch (RemoteException e) {
        log.warn("Unable to create RMI repository.", e);
        return;
    } catch (Throwable t) {
        log.warn("Unable to create RMI repository." + " The jcr-rmi jar might be missing.", t);
        return;
    }
    try {
        System.setProperty("java.rmi.server.useCodebaseOnly", "true");
        Registry reg = null;
        // or if the rmiHost is not local
        try {
            // find the server socket factory: use the default if the
            // rmiHost is not configured
            RMIServerSocketFactory sf;
            if (rc.getRmiHost().length() > 0) {
                log.debug("Creating RMIServerSocketFactory for host " + rc.getRmiHost());
                InetAddress hostAddress = InetAddress.getByName(rc.getRmiHost());
                sf = getRMIServerSocketFactory(hostAddress);
            } else {
                // have the RMI implementation decide which factory is the
                // default actually
                log.debug("Using default RMIServerSocketFactory");
                sf = null;
            }
            // create a registry using the default client socket factory
            // and the server socket factory retrieved above. This also
            // binds to the server socket to the rmiHost:rmiPort.
            reg = LocateRegistry.createRegistry(rc.rmiPort(), null, sf);
            rmiRegistry = reg;
        } catch (UnknownHostException uhe) {
            // thrown if the rmiHost cannot be resolved into an IP-Address
            // by getRMIServerSocketFactory
            log.info("Cannot create Registry", uhe);
        } catch (RemoteException e) {
            // thrown by createRegistry if binding to the rmiHost:rmiPort
            // fails, for example due to rmiHost being remote or another
            // application already being bound to the port
            log.info("Cannot create Registry", e);
        }
        // registry is actually accessible.
        if (reg == null) {
            log.debug("Trying to access existing registry at " + rc.getRmiHost() + ":" + rc.getRmiPort());
            try {
                reg = LocateRegistry.getRegistry(rc.getRmiHost(), rc.rmiPort());
            } catch (RemoteException re) {
                log.warn("Cannot create the reference to the registry at " + rc.getRmiHost() + ":" + rc.getRmiPort(), re);
            }
        }
        // rmiName
        if (reg != null) {
            log.debug("Registering repository as " + rc.getRmiName() + " to registry " + reg);
            reg.bind(rc.getRmiName(), remote);
            // when successfull, keep references
            this.rmiRepository = remote;
            log.info("Repository bound via RMI with name: " + rc.getRmiUri());
        } else {
            log.info("RMI registry missing, cannot bind repository via RMI");
        }
    } catch (RemoteException e) {
        log.warn("Unable to bind repository via RMI: " + rc.getRmiUri(), e);
    } catch (AlreadyBoundException e) {
        log.warn("Unable to bind repository via RMI: " + rc.getRmiUri(), e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) AlreadyBoundException(java.rmi.AlreadyBoundException) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) Remote(java.rmi.Remote) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) PojoServiceRegistry(org.apache.felix.connect.launch.PojoServiceRegistry) RemoteException(java.rmi.RemoteException) InetAddress(java.net.InetAddress)

Example 3 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project mlib by myshzzx.

the class RMITest2 method server.

@Test
public void server() throws RemoteException, AlreadyBoundException, InterruptedException {
    Registry registry = LocateRegistry.createRegistry(port);
    BlockingQueue<Socket> socks = new LinkedBlockingQueue<>();
    RIImpl ro = new RIImpl();
    Remote expRO = UnicastRemoteObject.exportObject(ro, port + 1, null, p -> new ServerSocket(p) {

        @Override
        public Socket accept() throws IOException {
            final Socket accept = super.accept();
            socks.offer(accept);
            return accept;
        }
    });
    registry.bind("ro", expRO);
    new Thread() {

        @Override
        public void run() {
            Socket sock;
            while (true) {
                try {
                    Thread.sleep(500);
                    sock = socks.take();
                    if (sock.isClosed())
                        System.out.println("sock closed");
                    else
                        socks.offer(sock);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    Thread.sleep(100000000);
}
Also used : Remote(java.rmi.Remote) ServerSocket(java.net.ServerSocket) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) IOException(java.io.IOException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AlreadyBoundException(java.rmi.AlreadyBoundException) NotBoundException(java.rmi.NotBoundException) Test(org.junit.Test)

Example 4 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project scheduling by ow2-proactive.

the class SchedulerStarter method startResourceManager.

private static void startResourceManager(final int numberLocalNodes, final int nodeTimeoutValue) {
    final Thread rmStarter = new Thread() {

        public void run() {
            try {
                // Starting a local RM using default deployment descriptor
                RMFactory.setOsJavaProperty();
                LOGGER.info("Starting the resource manager...");
                RMAuthentication rmAuth = RMFactory.startLocal();
                if (numberLocalNodes > 0) {
                    addLocalNodes(rmAuth, numberLocalNodes, nodeTimeoutValue);
                }
                LOGGER.info("The resource manager with " + numberLocalNodes + " local nodes created on " + rmAuth.getHostURL());
            } catch (AlreadyBoundException abe) {
                LOGGER.error("The resource manager already exists on local host", abe);
                System.exit(4);
            } catch (Exception aoce) {
                LOGGER.error("Unable to create local resource manager", aoce);
                System.exit(5);
            }
        }
    };
    rmStarter.start();
}
Also used : RMAuthentication(org.ow2.proactive.resourcemanager.authentication.RMAuthentication) AlreadyBoundException(java.rmi.AlreadyBoundException) LoginException(javax.security.auth.login.LoginException) KeyException(java.security.KeyException) URISyntaxException(java.net.URISyntaxException) SchedulerConfigurationException(org.ow2.proactive.scheduler.common.exception.SchedulerConfigurationException) InternalSchedulerException(org.ow2.proactive.scheduler.common.exception.InternalSchedulerException) ParseException(org.apache.commons.cli.ParseException) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ProActiveException(org.objectweb.proactive.core.ProActiveException) AlreadyBoundException(java.rmi.AlreadyBoundException)

Example 5 with AlreadyBoundException

use of java.rmi.AlreadyBoundException in project che by eclipse.

the class RmiServer method start.

protected static void start(Remote remote) throws RemoteException {
    System.setProperty("java.rmi.server.hostname", "localhost");
    System.setProperty("java.rmi.server.disableHttp", "true");
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.eclipse.che.rmi.JNDI");
    if (RmiServer.remote != null) {
        throw new AssertionError("This server is already started!");
    }
    RmiServer.remote = remote;
    int port = -1;
    Random random = new Random();
    Registry registry = null;
    while (port == -1) {
        int tmpPort = random.nextInt(65535);
        if (tmpPort < 4000) {
            continue;
        }
        try {
            registry = LocateRegistry.createRegistry(tmpPort);
            port = tmpPort;
        } catch (ExportException ignored) {
        }
    }
    Remote exportObject = UnicastRemoteObject.exportObject(remote, port);
    String exportName = remote.getClass().getSimpleName() + Integer.toHexString(exportObject.hashCode());
    try {
        registry.bind(exportName, exportObject);
        String portName = port + "/" + exportName;
        System.out.println("Port/Name:" + portName);
        int twoMinutes = 2 * 60 * 1000;
        Object lock = new Object();
        while (true) {
            synchronized (lock) {
                lock.wait(twoMinutes);
            }
        // TODO add ping
        }
    } catch (AlreadyBoundException | InterruptedException e) {
        e.printStackTrace();
        System.exit(42);
    }
}
Also used : Random(java.util.Random) AlreadyBoundException(java.rmi.AlreadyBoundException) Remote(java.rmi.Remote) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) ExportException(java.rmi.server.ExportException)

Aggregations

AlreadyBoundException (java.rmi.AlreadyBoundException)9 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7 RemoteException (java.rmi.RemoteException)6 IOException (java.io.IOException)5 Remote (java.rmi.Remote)4 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)3 JMXServiceURL (javax.management.remote.JMXServiceURL)3 RMIConnectorServer (javax.management.remote.rmi.RMIConnectorServer)3 RMIJRMPServerImpl (javax.management.remote.rmi.RMIJRMPServerImpl)3 MalformedURLException (java.net.MalformedURLException)2 NotBoundException (java.rmi.NotBoundException)2 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)2 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 URISyntaxException (java.net.URISyntaxException)1 ExportException (java.rmi.server.ExportException)1