Search in sources :

Example 71 with Registry

use of java.rmi.registry.Registry in project groovy by apache.

the class JmxConnectorHelper method createRmiRegistry.

public static Map createRmiRegistry(int initPort) {
    Map<String, Object> result = new HashMap<String, Object>(2);
    int counter = 0;
    int port = initPort;
    Registry reg = null;
    while (reg == null && counter <= 4) {
        try {
            reg = LocateRegistry.createRegistry(port);
            result.put("registry", reg);
            result.put("port", port);
            break;
        } catch (RemoteException ex) {
            counter++;
            port = port + 1;
            System.out.println("JmxBuilder - *** FAILED *** to create RMI Registry - Will Retry on port [" + port + "].");
            try {
                Thread.currentThread().sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) RemoteException(java.rmi.RemoteException)

Example 72 with Registry

use of java.rmi.registry.Registry in project intellij-community by JetBrains.

the class RemoteProcessSupport method acquire.

private EntryPoint acquire(final RunningInfo port) throws Exception {
    EntryPoint result = RemoteUtil.executeWithClassLoader(() -> {
        Registry registry = LocateRegistry.getRegistry("localhost", port.port);
        Remote remote = ObjectUtils.assertNotNull(registry.lookup(port.name));
        if (Remote.class.isAssignableFrom(myValueClass)) {
            EntryPoint entryPoint = narrowImpl(remote, myValueClass);
            if (entryPoint == null)
                return null;
            return RemoteUtil.substituteClassLoader(entryPoint, myValueClass.getClassLoader());
        } else {
            return RemoteUtil.castToLocal(remote, myValueClass);
        }
    }, // should be the loader of client plugin
    getClass().getClassLoader());
    // init hard ref that will keep it from DGC and thus preventing from System.exit
    port.entryPointHardRef = result;
    return result;
}
Also used : Remote(java.rmi.Remote) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry)

Example 73 with Registry

use of java.rmi.registry.Registry in project intellij-community by JetBrains.

the class RemoteServer method start.

@SuppressWarnings("UseOfSystemOutOrSystemErr")
protected static void start(Remote remote) throws Exception {
    setupRMI();
    banJNDI();
    setupSSL();
    setupDomainAuth();
    if (ourRemote != null)
        throw new AssertionError("Already started");
    ourRemote = remote;
    Registry registry;
    int port;
    for (Random random = new Random(); ; ) {
        port = random.nextInt(0xffff);
        if (port < 4000)
            continue;
        try {
            registry = LocateRegistry.createRegistry(port);
            break;
        } catch (ExportException ignored) {
        }
    }
    try {
        Remote stub = UnicastRemoteObject.exportObject(ourRemote, 0);
        final String name = remote.getClass().getSimpleName() + Integer.toHexString(stub.hashCode());
        registry.bind(name, stub);
        String id = port + "/" + name;
        System.out.println("Port/ID: " + id);
        long waitTime = RemoteDeadHand.PING_TIMEOUT;
        Object lock = new Object();
        //noinspection InfiniteLoopStatement
        while (true) {
            //noinspection SynchronizationOnLocalVariableOrMethodParameter
            synchronized (lock) {
                lock.wait(waitTime);
            }
            RemoteDeadHand deadHand = (RemoteDeadHand) registry.lookup(RemoteDeadHand.BINDING_NAME);
            waitTime = deadHand.ping(id);
        }
    } catch (Throwable e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}
Also used : Random(java.util.Random) Remote(java.rmi.Remote) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) ExportException(java.rmi.server.ExportException)

Example 74 with Registry

use of java.rmi.registry.Registry in project opennms by OpenNMS.

the class JmxRemoteAdminIT method canConnect.

@Test
public void canConnect() throws Exception {
    final InetSocketAddress addr = m_testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 18980);
    final String hostString = "localhost".equals(addr.getHostString()) ? "127.0.0.1" : addr.getHostString();
    final int port = addr.getPort();
    final RMISocketFactory socketFactory = new JMXTestClientSocketFactory();
    System.setProperty("sun.rmi.transport.tcp.responseTimeout", "5000");
    final Callable<Integer> getRmiConnection = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            LOG.debug("getRmiConnection({}:{})", hostString, port);
            try {
                final Registry registry = LocateRegistry.getRegistry(hostString, port, socketFactory);
                final String[] bound = registry.list();
                LOG.debug("bound={}", Arrays.asList(bound));
                if (bound.length > 0) {
                    return bound.length;
                }
            } catch (final Exception e) {
            }
            return null;
        }
    };
    await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getRmiConnection, greaterThanOrEqualTo(1));
    final Callable<Integer> getJmxConnection = new Callable<Integer>() {

        @Override
        public Integer call() throws Exception {
            LOG.debug("getJmxConnection({}:{})", hostString, port);
            try {
                RMISocketFactory.setSocketFactory(socketFactory);
                final Map<String, Object> env = new HashMap<>();
                final String[] credentials = { "admin", "admin" };
                env.put(JMXConnector.CREDENTIALS, credentials);
                env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, socketFactory);
                final String urlString = String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", hostString, port);
                LOG.debug("getJmxConnection(): connecting to {}", urlString);
                final JMXServiceURL url = new JMXServiceURL(urlString);
                final JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
                final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
                LOG.debug("mbeanCount={}", mbsc.getMBeanCount());
                if (mbsc.getMBeanCount() > 0) {
                    return mbsc.getMBeanCount();
                }
            } catch (final Exception e) {
            }
            return null;
        }
    };
    await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getJmxConnection, greaterThanOrEqualTo(1));
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RMISocketFactory(java.rmi.server.RMISocketFactory) Callable(java.util.concurrent.Callable) IOException(java.io.IOException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 75 with Registry

use of java.rmi.registry.Registry in project camel by apache.

the class JmxInstrumentationWithConnectorTest method testRmiRegistryUnexported.

public void testRmiRegistryUnexported() throws Exception {
    Registry registry = LocateRegistry.getRegistry(registryPort);
    // before we stop the context the registry is still exported, so list() should work
    Exception e;
    try {
        registry.list();
        e = null;
    } catch (NoSuchObjectException nsoe) {
        e = nsoe;
    }
    assertNull(e);
    // stop the Camel context
    context.stop();
    // stopping the Camel context unexported the registry, so list() should fail
    Exception e2;
    try {
        registry.list();
        e2 = null;
    } catch (NoSuchObjectException nsoe) {
        e2 = nsoe;
    }
    assertNotNull(e2);
}
Also used : NoSuchObjectException(java.rmi.NoSuchObjectException) LocateRegistry(java.rmi.registry.LocateRegistry) Registry(java.rmi.registry.Registry) NoSuchObjectException(java.rmi.NoSuchObjectException)

Aggregations

Registry (java.rmi.registry.Registry)128 LocateRegistry (java.rmi.registry.LocateRegistry)119 RemoteException (java.rmi.RemoteException)43 IOException (java.io.IOException)20 Remote (java.rmi.Remote)16 NodeRegisterRMIStub (org.mobicents.tools.sip.balancer.NodeRegisterRMIStub)14 UnknownHostException (java.net.UnknownHostException)13 JMXServiceURL (javax.management.remote.JMXServiceURL)12 HashMap (java.util.HashMap)10 NotBoundException (java.rmi.NotBoundException)9 JMXConnector (javax.management.remote.JMXConnector)9 DistributedRMICounter (examples.mvc.rmi.duplex.DistributedRMICounter)8 SocketException (java.net.SocketException)8 AlreadyBoundException (java.rmi.AlreadyBoundException)8 MBeanServerConnection (javax.management.MBeanServerConnection)8 JMXConnectorServer (javax.management.remote.JMXConnectorServer)8 Test (org.junit.Test)8 MBeanServer (javax.management.MBeanServer)7 InetAddress (java.net.InetAddress)6 MalformedURLException (java.net.MalformedURLException)6