public class RmiRegistryFactoryBean extends java.lang.Object implements FactoryBean<java.rmi.registry.Registry>, InitializingBean, DisposableBean
FactoryBean
that locates a Registry
and
exposes it for bean references. Can also create a local RMI registry
on the fly if none exists already.
Can be used to set up and pass around the actual Registry object to
applications objects that need to work with RMI. One example for such an
object that needs to work with RMI is Spring's RmiServiceExporter
,
which either works with a passed-in Registry reference or falls back to
the registry as specified by its local properties and defaults.
Also useful to enforce creation of a local RMI registry at a given port,
for example for a JMX connector. If used in conjunction with
ConnectorServerFactoryBean
,
it is recommended to mark the connector definition (ConnectorServerFactoryBean)
as "depends-on" the registry definition (RmiRegistryFactoryBean),
to guarantee starting up the registry first.
Note: The implementation of this class mirrors the corresponding logic
in RmiServiceExporter
, and also offers the same customization hooks.
RmiServiceExporter implements its own registry lookup as a convenience:
It is very common to simply rely on the registry defaults.
RmiServiceExporter.setRegistry(java.rmi.registry.Registry)
,
ConnectorServerFactoryBean
,
Registry
,
LocateRegistry
Constructor and Description |
---|
RmiRegistryFactoryBean() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
void |
destroy()
Unexport the RMI registry on bean factory shutdown,
provided that this bean actually created a registry.
|
java.lang.String |
getHost()
Return the host of the registry for the exported RMI service.
|
java.rmi.registry.Registry |
getObject()
Return an instance (possibly shared or independent) of the object
managed by this factory.
|
java.lang.Class<? extends java.rmi.registry.Registry> |
getObjectType()
Return the type of object that this FactoryBean creates,
or
null if not known in advance. |
int |
getPort()
Return the port of the registry for the exported RMI service.
|
protected java.rmi.registry.Registry |
getRegistry(int registryPort)
Locate or create the RMI registry.
|
protected java.rmi.registry.Registry |
getRegistry(int registryPort,
java.rmi.server.RMIClientSocketFactory clientSocketFactory,
java.rmi.server.RMIServerSocketFactory serverSocketFactory)
Locate or create the RMI registry.
|
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.
|
boolean |
isSingleton()
Is the object managed by this factory a singleton? That is,
will
FactoryBean.getObject() always return the same object
(a reference that can be cached)? |
void |
setAlwaysCreate(boolean alwaysCreate)
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 the RMI registry.
|
void |
setHost(java.lang.String host)
Set the host of the registry for the exported RMI service,
i.e.
|
void |
setPort(int port)
Set the port of the registry for the exported RMI service,
i.e.
|
void |
setServerSocketFactory(java.rmi.server.RMIServerSocketFactory serverSocketFactory)
Set a custom RMI server socket factory to use for the RMI registry.
|
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.
|
protected final Log logger
public void setHost(java.lang.String host)
rmi://HOST:port/name
Default is localhost.
public java.lang.String getHost()
public void setPort(int port)
rmi://host:PORT/name
Default is Registry.REGISTRY_PORT
(1099).
public int getPort()
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
,
LocateRegistry.getRegistry(String, int, java.rmi.server.RMIClientSocketFactory)
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
,
LocateRegistry.createRegistry(int, RMIClientSocketFactory, java.rmi.server.RMIServerSocketFactory)
public void setAlwaysCreate(boolean alwaysCreate)
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 afterPropertiesSet() throws java.lang.Exception
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
afterPropertiesSet
in interface InitializingBean
java.lang.Exception
- in the event of misconfiguration (such as failure to set an
essential property) or if initialization fails for any other reasonprotected 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 java.rmi.registry.Registry getObject() throws java.lang.Exception
FactoryBean
As with a BeanFactory
, this allows support for both the
Singleton and Prototype design pattern.
If this FactoryBean is not fully initialized yet at the time of
the call (for example because it is involved in a circular reference),
throw a corresponding FactoryBeanNotInitializedException
.
As of Spring 2.0, FactoryBeans are allowed to return null
objects. The factory will consider this as normal value to be used; it
will not throw a FactoryBeanNotInitializedException in this case anymore.
FactoryBean implementations are encouraged to throw
FactoryBeanNotInitializedException themselves now, as appropriate.
getObject
in interface FactoryBean<java.rmi.registry.Registry>
null
)java.lang.Exception
- in case of creation errorsFactoryBeanNotInitializedException
public java.lang.Class<? extends java.rmi.registry.Registry> getObjectType()
FactoryBean
null
if not known in advance.
This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
null
here. Therefore it is highly recommended to implement
this method properly, using the current state of the FactoryBean.
getObjectType
in interface FactoryBean<java.rmi.registry.Registry>
null
if not known at the time of the callListableBeanFactory.getBeansOfType(java.lang.Class<T>)
public boolean isSingleton()
FactoryBean
FactoryBean.getObject()
always return the same object
(a reference that can be cached)?
NOTE: If a FactoryBean indicates to hold a singleton object,
the object returned from getObject()
might get cached
by the owning BeanFactory. Hence, do not return true
unless the FactoryBean always exposes the same reference.
The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning false
does not
necessarily indicate that returned objects are independent instances.
An implementation of the extended SmartFactoryBean
interface
may explicitly indicate independent instances through its
SmartFactoryBean.isPrototype()
method. Plain FactoryBean
implementations which do not implement this extended interface are
simply assumed to always return independent instances if the
isSingleton()
implementation returns false
.
The default implementation returns true
, since a
FactoryBean
typically manages a singleton instance.
isSingleton
in interface FactoryBean<java.rmi.registry.Registry>
FactoryBean.getObject()
,
SmartFactoryBean.isPrototype()
public void destroy() throws java.rmi.RemoteException
destroy
in interface DisposableBean
java.rmi.RemoteException