public class RmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean
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 java.rmi.RemoteException
on all methods, but in and out parameters have to be serializable.
The major advantage of RMI, compared to Hessian, is serialization. Effectively, any serializable Java object can be transported without hassle. Hessian has its own (de-)serialization mechanisms, but is 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. To ensure 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
RmiClientInterceptor
,
RmiProxyFactoryBean
,
Remote
,
RemoteException
,
HessianServiceExporter
,
HttpInvokerServiceExporter
logger
Constructor and Description |
---|
RmiServiceExporter() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
void |
destroy()
Unbind the RMI service from the registry on bean factory shutdown.
|
protected java.rmi.registry.Registry |
getRegistry(int registryPort)
Locate or create the RMI registry for this exporter.
|
protected java.rmi.registry.Registry |
getRegistry(int registryPort,
java.rmi.server.RMIClientSocketFactory clientSocketFactory,
java.rmi.server.RMIServerSocketFactory serverSocketFactory)
Locate or create the RMI registry for this exporter.
|
protected java.rmi.registry.Registry |
getRegistry(java.lang.String registryHost,
int registryPort,
java.rmi.server.RMIClientSocketFactory clientSocketFactory,
java.rmi.server.RMIServerSocketFactory serverSocketFactory)
Locate or create the RMI registry for this exporter.
|
void |
prepare()
Initialize this service exporter, registering the service as RMI object.
|
void |
setAlwaysCreateRegistry(boolean alwaysCreateRegistry)
Set whether to always create the registry in-process,
not attempting to locate an existing registry at the specified port.
|
void |
setClientSocketFactory(java.rmi.server.RMIClientSocketFactory clientSocketFactory)
Set a custom RMI client socket factory to use for exporting the service.
|
void |
setRegistry(java.rmi.registry.Registry registry)
Specify the RMI registry to register the exported service with.
|
void |
setRegistryClientSocketFactory(java.rmi.server.RMIClientSocketFactory registryClientSocketFactory)
Set a custom RMI client socket factory to use for the RMI registry.
|
void |
setRegistryHost(java.lang.String registryHost)
Set the host 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(java.rmi.server.RMIServerSocketFactory registryServerSocketFactory)
Set a custom RMI server socket factory to use for the RMI registry.
|
void |
setReplaceExistingBinding(boolean replaceExistingBinding)
Set whether to replace an existing binding in the RMI registry,
that is, whether to simply override an existing binding with the
specified service in case of a naming conflict in the registry.
|
void |
setServerSocketFactory(java.rmi.server.RMIServerSocketFactory serverSocketFactory)
Set a custom RMI server socket factory to use for exporting the service.
|
void |
setServiceName(java.lang.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(java.rmi.registry.Registry registry)
Test the given RMI registry, calling some operation on it to
check whether it is still active.
|
getObjectToExport, invoke
getRemoteInvocationExecutor, invokeAndCreateResult, setRemoteInvocationExecutor
checkService, checkServiceInterface, getExporterName, getProxyForService, getService, getServiceInterface, setInterceptors, setRegisterTraceInterceptor, setService, setServiceInterface
getBeanClassLoader, overrideThreadContextClassLoader, resetThreadContextClassLoader, setBeanClassLoader
public void setServiceName(java.lang.String serviceName)
rmi://host:port/NAME
public void setServicePort(int servicePort)
Default is 0 (anonymous port).
public void setClientSocketFactory(java.rmi.server.RMIClientSocketFactory clientSocketFactory)
If the given object also implements java.rmi.server.RMIServerSocketFactory
,
it will automatically be registered as server socket factory too.
setServerSocketFactory(java.rmi.server.RMIServerSocketFactory)
,
RMIClientSocketFactory
,
RMIServerSocketFactory
,
UnicastRemoteObject.exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory)
public void setServerSocketFactory(java.rmi.server.RMIServerSocketFactory serverSocketFactory)
Only needs to be specified when the client socket factory does not
implement java.rmi.server.RMIServerSocketFactory
already.
setClientSocketFactory(java.rmi.server.RMIClientSocketFactory)
,
RMIClientSocketFactory
,
RMIServerSocketFactory
,
UnicastRemoteObject.exportObject(Remote, int, RMIClientSocketFactory, RMIServerSocketFactory)
public void setRegistry(java.rmi.registry.Registry registry)
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.
public void setRegistryHost(java.lang.String registryHost)
rmi://HOST:port/name
Default is localhost.
public void setRegistryPort(int registryPort)
rmi://host:PORT/name
Default is Registry.REGISTRY_PORT
(1099).
Registry.REGISTRY_PORT
public void setRegistryClientSocketFactory(java.rmi.server.RMIClientSocketFactory registryClientSocketFactory)
If the given object also implements java.rmi.server.RMIServerSocketFactory
,
it will automatically be registered as server socket factory too.
setRegistryServerSocketFactory(java.rmi.server.RMIServerSocketFactory)
,
RMIClientSocketFactory
,
RMIServerSocketFactory
,
LocateRegistry.getRegistry(String, int, RMIClientSocketFactory)
public void setRegistryServerSocketFactory(java.rmi.server.RMIServerSocketFactory registryServerSocketFactory)
Only needs to be specified when the client socket factory does not
implement java.rmi.server.RMIServerSocketFactory
already.
setRegistryClientSocketFactory(java.rmi.server.RMIClientSocketFactory)
,
RMIClientSocketFactory
,
RMIServerSocketFactory
,
LocateRegistry.createRegistry(int, RMIClientSocketFactory, RMIServerSocketFactory)
public void setAlwaysCreateRegistry(boolean alwaysCreateRegistry)
Default is "false". Switch this flag to "true" in order to avoid the overhead of locating an existing registry when you always intend to create a new registry in any case.
public void setReplaceExistingBinding(boolean replaceExistingBinding)
Default is "true", assuming that an existing binding for this exporter's service name is an accidental leftover from a previous execution. Switch this to "false" to make the exporter fail in such a scenario, indicating that there was already an RMI object bound.
public void afterPropertiesSet() throws java.rmi.RemoteException
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
java.rmi.RemoteException
public void prepare() throws java.rmi.RemoteException
Creates an RMI registry on the specified port if none exists.
java.rmi.RemoteException
- if service registration failedprotected java.rmi.registry.Registry getRegistry(java.lang.String registryHost, int registryPort, @Nullable java.rmi.server.RMIClientSocketFactory clientSocketFactory, @Nullable java.rmi.server.RMIServerSocketFactory serverSocketFactory) throws java.rmi.RemoteException
registryHost
- the registry host to use (if this is specified,
no implicit creation of a RMI registry will happen)registryPort
- the registry port to useclientSocketFactory
- the RMI client socket factory for the registry (if any)serverSocketFactory
- the RMI server socket factory for the registry (if any)java.rmi.RemoteException
- if the registry couldn't be located or createdprotected java.rmi.registry.Registry getRegistry(int registryPort, @Nullable java.rmi.server.RMIClientSocketFactory clientSocketFactory, @Nullable java.rmi.server.RMIServerSocketFactory serverSocketFactory) throws java.rmi.RemoteException
registryPort
- the registry port to useclientSocketFactory
- the RMI client socket factory for the registry (if any)serverSocketFactory
- the RMI server socket factory for the registry (if any)java.rmi.RemoteException
- if the registry couldn't be located or createdprotected java.rmi.registry.Registry getRegistry(int registryPort) throws java.rmi.RemoteException
registryPort
- the registry port to usejava.rmi.RemoteException
- if the registry couldn't be located or createdprotected void testRegistry(java.rmi.registry.Registry registry) throws java.rmi.RemoteException
Default implementation calls Registry.list()
.
registry
- the RMI registry to testjava.rmi.RemoteException
- if thrown by registry methodsRegistry.list()
public void destroy() throws java.rmi.RemoteException
destroy
in interface DisposableBean
java.rmi.RemoteException