Serializing Services
It is often useful, especially in a web-based application, to serialize services. In HiveMind 1.0, objects that had references to HiveMind services could not be serialized ... the services were not serializable. You could make your service implemention implement Serializable, but that's not what client code sees ... client code sees the service proxy, which did not implement Serializable. End of story.
Starting with HiveMind 1.1, service proxies are, themselves, serializable. If you create objects that have references to HiveMind services, those objects can be serialized. For example:
public class MyDataObject implements Serializable { private MyService _myService; . . . }
Here, the MyDataObject class has a reference to a HiveMind service in its _myService instance variable. An instance of MyDataObject may be serialized in one JVM and de-serialized in another. In the new JVM, the _myService instance variable will point to an equivalent HiveMind service.
It is the service proxy that is serializable, not the service implementation. That means that a stateful service (one that uses the threaded or pooled service model) will likely not be in the same state in the new JVM. Again, the internal state of the service is not serialized ... just the service's id.
If your service acts as a factory (or otherwise provides access to itself), you must be careful to share the service proxy, not the implementation itself. For example:
public class MyServiceImpl implements MyService { private void MyService _myProxy; public void setMyProxy(MyService myProxy) { _myProxy = myProxy; } // Service method public MyDataObject newDataObject() { return new MyDataObject(_myProxy); } . . . }
Here, the implementation acts as a factory for MyDataObject instances. It connects each data object to the service proxy ( new MyDataObject(_myProxy) rather than new MyDataObject(this)). You will need to use the hivemind.BuilderFactory and include a <set-service> element to connect the implementation's myProxy property to the service proxy.
A final note: Services using the primitive service model do not have service proxies and therefore they are not serializable, even in release 1.1. The other service models (including the default service model, singleton), do use a proxy and are serializable, as discussed in this document.