public class RmiHostedService extends AbstractHostedService
Objects are shared by adding them to the RmiRegistry with one of the share() methods. Shared objects must have a separate interface and implementation. The interface is what the other end of the connection will use to interact with the object and that interface class must be available on both ends of the connection. The implementing class need only be on the sharing end.
Shared objects can be accessed on the other end of the connection by using one of the RmiRegistry's getRemoteObject() methods. These can be used to lookup an object by class if it is a shared singleton or by name if it was registered with a name.
On the hosting side, a special shardGlobal() method is provided that will register shared objects that will automatically be provided to every new joining client and they will all be calling the same server-side instance. Normally, shared objects themselves are connection specific and handled at the connection layer. The shareGlobal() space is a way to have global resources passed directly though the need is relatively rare.
Note: This RMI implementation is not as advanced as Java's regular RMI as it won't marshall shared references, ie: you can't pass a shared objects as an argument to another shared object's method.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ATTRIBUTE_NAME |
Constructor and Description |
---|
RmiHostedService() |
RmiHostedService(byte defaultChannel) |
RmiHostedService(short rmiId,
byte defaultChannel,
boolean autoHost) |
Modifier and Type | Method and Description |
---|---|
void |
connectionAdded(Server server,
HostedConnection hc)
Called internally when a new connection is detected for
the server.
|
void |
connectionRemoved(Server server,
HostedConnection hc)
Called internally when an existing connection is leaving
the server.
|
boolean |
getAutoHost()
Returns true if RMI hosting is automatically started for all new connections.
|
RmiRegistry |
getRmiRegistry(HostedConnection hc)
Returns the RMI registry for the specific HostedConnection.
|
protected void |
onInitialize(HostedServiceManager s)
Called during initialize() for the subclass to perform
implementation specific initialization.
|
void |
setAutoHost(boolean b)
Set to true if all new connections should automatically have RMI hosting started.
|
<T> void |
shareGlobal(byte channel,
java.lang.String name,
T object,
java.lang.Class<? super T> type)
Shares a server-wide object associated with the specified name over the specified
channel.
|
<T> void |
shareGlobal(java.lang.String name,
T object,
java.lang.Class<? super T> type)
Shares a server-wide object associated with the specified name.
|
<T> void |
shareGlobal(T object,
java.lang.Class<? super T> type)
Shares a server-wide object associated with the specified type.
|
void |
startHostingOnConnection(HostedConnection hc)
Sets up RMI hosting services for the hosted connection allowing
getRmiRegistry() to return a valid RmiRegistry object.
|
void |
stopHostingOnConnection(HostedConnection hc)
Removes any RMI hosting services associated with the specified
connection.
|
getServer
getService, getServiceManager, initialize, start, stop, terminate, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
initialize, start, stop, terminate
public static final java.lang.String ATTRIBUTE_NAME
public RmiHostedService()
public RmiHostedService(byte defaultChannel)
public RmiHostedService(short rmiId, byte defaultChannel, boolean autoHost)
public <T> void shareGlobal(T object, java.lang.Class<? super T> type)
public <T> void shareGlobal(java.lang.String name, T object, java.lang.Class<? super T> type)
public <T> void shareGlobal(byte channel, java.lang.String name, T object, java.lang.Class<? super T> type)
public void setAutoHost(boolean b)
public boolean getAutoHost()
public RmiRegistry getRmiRegistry(HostedConnection hc)
public void startHostingOnConnection(HostedConnection hc)
public void stopHostingOnConnection(HostedConnection hc)
protected void onInitialize(HostedServiceManager s)
AbstractService
onInitialize
in class AbstractService<HostedServiceManager>
public void connectionAdded(Server server, HostedConnection hc)
connectionAdded
in interface ConnectionListener
connectionAdded
in class AbstractHostedService
public void connectionRemoved(Server server, HostedConnection hc)
connectionRemoved
in interface ConnectionListener
connectionRemoved
in class AbstractHostedService