Example usage for java.rmi.server RMISocketFactory RMISocketFactory

List of usage examples for java.rmi.server RMISocketFactory RMISocketFactory

Introduction

In this page you can find the example usage for java.rmi.server RMISocketFactory RMISocketFactory.

Prototype

public RMISocketFactory() 

Source Link

Document

Constructs an RMISocketFactory.

Usage

From source file:org.apache.jcs.auxiliary.remote.RemoteCache.java

/**
 * Constructor for the RemoteCache object. This object communicates with a remote cache server.
 * One of these exists for each region. This also holds a reference to a listener. The same
 * listener is used for all regions for one remote server. Holding a reference to the listener
 * allows this object to know the listener id assigned by the remote cache.
 * <p>/*from w  w w  .jav  a 2 s .co m*/
 * @param cattr
 * @param remote
 * @param listener
 */
public RemoteCache(IRemoteCacheAttributes cattr, IRemoteCacheService remote, IRemoteCacheListener listener) {
    this.irca = cattr;
    this.cacheName = cattr.getCacheName();
    this.remote = remote;
    this.listener = listener;

    if (log.isDebugEnabled()) {
        log.debug("Construct> cacheName=" + cattr.getCacheName());
        log.debug("irca = " + irca);
        log.debug("remote = " + remote);
        log.debug("listener = " + listener);
    }

    // use a pool if it is greater than 0
    if (log.isDebugEnabled()) {
        log.debug("GetTimeoutMillis() = " + irca.getGetTimeoutMillis());
    }

    if (irca.getGetTimeoutMillis() > 0) {
        pool = ThreadPoolManager.getInstance().getPool(irca.getThreadPoolName());
        if (log.isDebugEnabled()) {
            log.debug("Thread Pool = " + pool);
        }
        if (pool != null) {
            usePoolForGet = true;
        }
    }

    try {
        // Don't set a socket factory if the setting is -1
        if (irca.getRmiSocketFactoryTimeoutMillis() > 0) {
            // TODO make configurable.
            // use this socket factory to add a timeout.
            RMISocketFactory.setSocketFactory(new RMISocketFactory() {
                public Socket createSocket(String host, int port) throws IOException {
                    Socket socket = new Socket(host, port);
                    socket.setSoTimeout(irca.getRmiSocketFactoryTimeoutMillis());
                    socket.setSoLinger(false, 0);
                    return socket;
                }

                public ServerSocket createServerSocket(int port) throws IOException {
                    return new ServerSocket(port);
                }
            });
        }
    } catch (Exception e) {
        // TODO change this so that we only try to do it once. Otherwise we
        // genreate errors for each region on construction.
        log.info(e.getMessage());
    }
}

From source file:org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory.java

/**
 * Starts up the remote cache server on this JVM, and binds it to the registry on the given host
 * and port./*from ww  w.  ja v  a2  s.  c o  m*/
 * A remote cache is either a local cache or a cluster cache
 * @param host
 * @param port
 * @param propFile
 * @throws IOException
 */
public static void startup(String host, int port, String propFile) throws IOException {
    if (remoteCacheServer != null) {
        throw new IllegalArgumentException("Server already started.");
    }

    synchronized (RemoteCacheServer.class) {
        if (remoteCacheServer != null) {
            return;
        }

        if (log.isInfoEnabled()) {
            log.info("ConfigFileName = [" + propFile + "]");
        }

        try {
            // TODO make configurable.
            // use this socket factory to add a timeout.
            RMISocketFactory.setSocketFactory(new RMISocketFactory() {
                public Socket createSocket(String host, int port) throws IOException {
                    Socket socket = new Socket(host, port);
                    socket.setSoTimeout(DEFAULT_RMI_SOCKET_FACTORY_TIMEOUT_MS);
                    socket.setSoLinger(false, 0);
                    return socket;
                }

                public ServerSocket createServerSocket(int port) throws IOException {
                    return new ServerSocket(port);
                }
            });
        } catch (Exception e) {
            log.error("Problem setting custom RMI Socket Factory.", e);
        }

        // TODO: make automatic
        RemoteCacheServerAttributes rcsa = new RemoteCacheServerAttributes();
        rcsa.setConfigFileName(propFile);

        Properties prop = RemoteUtils.loadProps(propFile);
        // Properties prop = PropertyLoader.loadProperties( propFile );

        String servicePortStr = prop.getProperty(REMOTE_CACHE_SERVICE_PORT);
        int servicePort = -1;
        try {
            servicePort = Integer.parseInt(servicePortStr);

            rcsa.setServicePort(servicePort);
            log.debug("Remote cache service uses port number " + servicePort + ".");
        } catch (NumberFormatException ignore) {
            log.debug("Remote cache service port property " + REMOTE_CACHE_SERVICE_PORT
                    + " not specified.  An anonymous port will be used.");
        }

        String lccStr = prop.getProperty(REMOTE_LOCAL_CLUSTER_CONSISTENCY);
        if (lccStr == null) {
            lccStr = "true";
        }
        boolean lcc = Boolean.valueOf(lccStr).booleanValue();
        rcsa.setLocalClusterConsistency(lcc);

        String acgStr = prop.getProperty(REMOTE_ALLOW_CLUSTER_GET);
        if (acgStr == null) {
            acgStr = "true";
        }
        boolean acg = Boolean.valueOf(acgStr).booleanValue();
        rcsa.setAllowClusterGet(acg);

        if (log.isInfoEnabled()) {
            log.info("Creating server with these attributes " + rcsa);
        }

        // CREATE SERVER
        remoteCacheServer = new RemoteCacheServer(rcsa);

        if (host == null) {
            host = "";
        }
        // Register the RemoteCacheServer remote object in the registry.
        serviceName = prop.getProperty(REMOTE_CACHE_SERVICE_NAME, REMOTE_CACHE_SERVICE_VAL).trim();

        if (log.isInfoEnabled()) {
            log.info("Binding server to " + host + ":" + port + " with the name " + serviceName);
        }
        try {
            Naming.rebind("//" + host + ":" + port + "/" + serviceName, remoteCacheServer);
        } catch (MalformedURLException ex) {
            // impossible case.
            throw new IllegalArgumentException(ex.getMessage() + "; host=" + host + ", port=" + port);
        }
    }
}