org.springframework.remoting.rmi
Class RmiServiceExporter

java.lang.Object
  extended by org.springframework.remoting.support.RemoteExporter
      extended by org.springframework.remoting.support.RemoteInvocationBasedExporter
          extended by org.springframework.remoting.rmi.RmiServiceExporter
All Implemented Interfaces:
DisposableBean, InitializingBean

public class RmiServiceExporter
extends RemoteInvocationBasedExporter
implements InitializingBean, DisposableBean

RMI exporter that exposes the specified service as RMI object with the specified name. Such services can be accessed via plain RMI or via RmiProxyFactoryBean. Also supports exposing any non-RMI service via RMI invokers, to be accessed via RmiClientInterceptor/RmiProxyFactoryBean's automatic detection of such invokers.

With an RMI invoker, RMI communication works on the RmiInvocationHandler level, needing only one stub for any service. Service interfaces do not have to extend java.rmi.Remote or throw RemoteException on all methods, but in and out parameters have to be serializable.

The major advantage of RMI, compared to Hessian and Burlap, is serialization. Effectively, any serializable Java object can be transported without hassle. Hessian and Burlap have their own (de-)serialization mechanisms, but are HTTP-based and thus much easier to setup than RMI. Alternatively, consider Spring's HTTP invoker to combine Java serialization with HTTP-based transport.

Note: RMI makes a best-effort attempt to obtain the fully qualified host name. If one cannot be determined, it will fall back and use the IP address. Depending on your network configuration, in some cases it will resolve the IP to the loopback address. Ensuring that RMI will use the host name bound to the correct network interface you should pass the java.rmi.server.hostname property to the JVM that will export the registry and/or the service using the "-D" JVM argument. For example: -Djava.rmi.server.hostname=myserver.com

Since:
13.05.2003
Author:
Juergen Hoeller
See Also:
RmiClientInterceptor, RmiProxyFactoryBean, Remote, RemoteException, HessianServiceExporter, BurlapServiceExporter, HttpInvokerServiceExporter

Field Summary
 
Fields inherited from class org.springframework.remoting.support.RemoteExporter
logger
 
Constructor Summary
RmiServiceExporter()
           
 
Method Summary
 void afterPropertiesSet()
          Register the service as RMI object.
 void destroy()
          Unbind the RMI service from the registry on bean factory shutdown.
protected  Remote getObjectToExport()
          Determine the object to export: either the service object itself or a RmiInvocationWrapper in case of a non-RMI service object.
protected  Registry getRegistry(int registryPort)
          Locate or create the RMI registry for this exporter.
protected  Registry getRegistry(int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory)
          Locate or create the RMI registry for this exporter.
protected  Registry getRegistry(String registryHost, int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory)
          Locate or create the RMI registry for this exporter.
protected  Object invoke(RemoteInvocation invocation, Object targetObject)
          Redefined here to be visible to RmiInvocationWrapper.
 void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory)
          Set a custom RMI client socket factory to use for exporting the service.
 void setRegistry(Registry registry)
          Specify the RMI registry to register the exported service with.
 void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory)
          Set a custom RMI client socket factory to use for the RMI registry.
 void setRegistryHost(String registryHost)
          Set the port of the registry for the exported RMI service, i.e.
 void setRegistryPort(int registryPort)
          Set the port of the registry for the exported RMI service, i.e.
 void setRegistryServerSocketFactory(RMIServerSocketFactory registryServerSocketFactory)
          Set a custom RMI server socket factory to use for the RMI registry.
 void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory)
          Set a custom RMI server socket factory to use for exporting the service.
 void setServiceName(String serviceName)
          Set the name of the exported RMI service, i.e.
 void setServicePort(int servicePort)
          Set the port that the exported RMI service will use.
protected  void testRegistry(Registry registry)
          Test the given RMI registry, calling some operation on it to check whether it is still active.
 
Methods inherited from class org.springframework.remoting.support.RemoteInvocationBasedExporter
getRemoteInvocationExecutor, invokeAndCreateResult, setRemoteInvocationExecutor
 
Methods inherited from class org.springframework.remoting.support.RemoteExporter
checkService, checkServiceInterface, getExporterName, getProxyForService, getService, getServiceInterface, isRegisterTraceInterceptor, setRegisterTraceInterceptor, setService, setServiceInterface
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RmiServiceExporter

public RmiServiceExporter()
Method Detail

setServiceName

public void setServiceName(String serviceName)
Set the name of the exported RMI service, i.e. rmi://host:port/NAME


setServicePort

public void setServicePort(int servicePort)
Set the port that the exported RMI service will use.

Default is 0 (anonymous port).


setClientSocketFactory

public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory)
Set a custom RMI client socket factory to use for exporting the service.

If the given object also implements java.rmi.server.RMIServerSocketFactory, it will automatically be registered as server socket factory too.

See Also:
setServerSocketFactory(java.rmi.server.RMIServerSocketFactory), RMIClientSocketFactory, RMIServerSocketFactory, UnicastRemoteObject.exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory)

setServerSocketFactory

public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory)
Set a custom RMI server socket factory to use for exporting the service.

Only needs to be specified when the client socket factory does not implement java.rmi.server.RMIServerSocketFactory already.

See Also:
setClientSocketFactory(java.rmi.server.RMIClientSocketFactory), RMIClientSocketFactory, RMIServerSocketFactory, UnicastRemoteObject.exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory)

setRegistry

public void setRegistry(Registry registry)
Specify the RMI registry to register the exported service with. Typically used in combination with RmiRegistryFactoryBean.

Alternatively, you can specify all registry properties locally. This exporter will then try to locate the specified registry, automatically creating a new local one if appropriate.

Default is a local registry at the default port (1099), created on the fly if necessary.

See Also:
RmiRegistryFactoryBean, setRegistryHost(java.lang.String), setRegistryPort(int), setRegistryClientSocketFactory(java.rmi.server.RMIClientSocketFactory), setRegistryServerSocketFactory(java.rmi.server.RMIServerSocketFactory)

setRegistryHost

public void setRegistryHost(String registryHost)
Set the port of the registry for the exported RMI service, i.e. rmi://HOST:port/name

Default is localhost.


setRegistryPort

public void setRegistryPort(int registryPort)
Set the port of the registry for the exported RMI service, i.e. rmi://host:PORT/name

Default is Registry.REGISTRY_PORT (1099).

See Also:
Registry.REGISTRY_PORT

setRegistryClientSocketFactory

public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory)
Set a custom RMI client socket factory to use for the RMI registry.

If the given object also implements java.rmi.server.RMIServerSocketFactory, it will automatically be registered as server socket factory too.

See Also:
setRegistryServerSocketFactory(java.rmi.server.RMIServerSocketFactory), RMIClientSocketFactory, RMIServerSocketFactory, LocateRegistry.getRegistry(String, int, RMIClientSocketFactory)

setRegistryServerSocketFactory

public void setRegistryServerSocketFactory(RMIServerSocketFactory registryServerSocketFactory)
Set a custom RMI server socket factory to use for the RMI registry.

Only needs to be specified when the client socket factory does not implement java.rmi.server.RMIServerSocketFactory already.

See Also:
setRegistryClientSocketFactory(java.rmi.server.RMIClientSocketFactory), RMIClientSocketFactory, RMIServerSocketFactory, LocateRegistry.createRegistry(int, RMIClientSocketFactory, RMIServerSocketFactory)

afterPropertiesSet

public void afterPropertiesSet()
                        throws RemoteException
Register the service as RMI object. Creates an RMI registry on the specified port if none exists.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
RemoteException

getRegistry

protected Registry getRegistry(String registryHost,
                               int registryPort,
                               RMIClientSocketFactory clientSocketFactory,
                               RMIServerSocketFactory serverSocketFactory)
                        throws RemoteException
Locate or create the RMI registry for this exporter.

Parameters:
registryHost - the registry host to use (if this is specified, no implicit creation of a RMI registry will happen)
registryPort - the registry port to use
clientSocketFactory - the RMI client socket factory for the registry (if any)
serverSocketFactory - the RMI server socket factory for the registry (if any)
Returns:
the RMI registry
Throws:
RemoteException - if the registry couldn't be located or created

getRegistry

protected Registry getRegistry(int registryPort,
                               RMIClientSocketFactory clientSocketFactory,
                               RMIServerSocketFactory serverSocketFactory)
                        throws RemoteException
Locate or create the RMI registry for this exporter.

Parameters:
registryPort - the registry port to use
clientSocketFactory - the RMI client socket factory for the registry (if any)
serverSocketFactory - the RMI server socket factory for the registry (if any)
Returns:
the RMI registry
Throws:
RemoteException - if the registry couldn't be located or created

getRegistry

protected Registry getRegistry(int registryPort)
                        throws RemoteException
Locate or create the RMI registry for this exporter.

Parameters:
registryPort - the registry port to use
Returns:
the RMI registry
Throws:
RemoteException - if the registry couldn't be located or created

testRegistry

protected void testRegistry(Registry registry)
                     throws RemoteException
Test the given RMI registry, calling some operation on it to check whether it is still active.

Default implementation calls Registry.list().

Parameters:
registry - the RMI registry to test
Throws:
RemoteException - if thrown by registry methods
See Also:
Registry.list()

getObjectToExport

protected Remote getObjectToExport()
Determine the object to export: either the service object itself or a RmiInvocationWrapper in case of a non-RMI service object.

Returns:
the RMI object to export

invoke

protected Object invoke(RemoteInvocation invocation,
                        Object targetObject)
                 throws NoSuchMethodException,
                        IllegalAccessException,
                        InvocationTargetException
Redefined here to be visible to RmiInvocationWrapper. Simply delegates to the corresponding superclass method.

Overrides:
invoke in class RemoteInvocationBasedExporter
Parameters:
invocation - the remote invocation
targetObject - the target object to apply the invocation to
Returns:
the invocation result
Throws:
NoSuchMethodException - if the method name could not be resolved
IllegalAccessException - if the method could not be accessed
InvocationTargetException - if the method invocation resulted in an exception
See Also:
RemoteInvocationExecutor.invoke(org.springframework.remoting.support.RemoteInvocation, java.lang.Object)

destroy

public void destroy()
             throws RemoteException,
                    NotBoundException
Unbind the RMI service from the registry on bean factory shutdown.

Specified by:
destroy in interface DisposableBean
Throws:
RemoteException
NotBoundException


Copyright (c) 2002-2005 The Spring Framework Project.