HiveMind: HelloWorld

I’ve been playing around with HiveMind, which seems like a very powerful framework for building applications. Unfortunately there seems to be little documentation on how to use it, and the learning curve is steep to say the least. So in an attempt to demystify it, here’s my take on “Hello World” in HiveMind.

Environment

The first thing to do is to get your environment working, so you need to download HiveMind. In addition to the HiveMind JAR file you need a number of additional JAR files in your classpath to run a application. For my environment I’m using have the following:

Configuration File

HiveMind configuration files are an XML document with a root element of <module>, all other pieces of configuration are children of this tag:

<module id="HelloWorldModule">
NOTE: Service definitions go here!
</module>

Next we define a service point called HelloWorld which conforms to the Java interface IService by adding the following to the configuration:

<service-point id="HelloWorld" interface="IHelloWorldService" />

This allows other services to find and understand the capabilities of this service, but doesn’t define how it is implemented. Note that if the interface attribute is omitted from a <service-point> definition, HiveMind assumes that the id attribute specifies the name of the Java interface.

<implementation service-id="HelloWorld">
<create-instance class="HelloWorldService" />
</implementation>

This <implementation> tag defines how the HelloWorld service is implemented. In this case, the service is created using the built-in <create-instance> ServiceModel, which simply instantiates an object of the given class, HelloWorldService. Since HelloWorldService needs no configuration, this is fine.

For convenience, the service definition and implementation can be performed in one step using the following:
<service-point id="HelloWorld" interface="IHelloWorldService">
<create-instance class="HelloWorldService" />
</service-point>

Code

Now that the service is defined, we can write the Java code. First we define the interface for the service:

public interface IHelloWorldService {
public void sayHello();
}

Next, a concrete implementation of this service:

public class HelloWorldService implements IHelloWorldService {
public void sayHello() {
System.out.println("Hello World!");
}
}

Next, we need to invoke our service. So we read in our configuration file, helloWorld.xml, which defines a module, and add that module to a registry:

ClassResolver resolver = new DefaultClassResolver();
ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(
resolver, new FileResource("helloWorld.xml"));
RegistryBuilder builder = new RegistryBuilder();
builder.addModuleDescriptorProvider(new XmlModuleDescriptorProvider(resolver));
builder.addModuleDescriptorProvider(provider);

Now that we have our services defined, we can ask the registry for our service, get a reference to it and call it.

Registry r = builder.constructRegistry(Locale.getDefault());
IHelloWorldService service = (IHelloWorldService)r.getService(
"HelloWorldModule.HelloWorld",
IHelloWorldService.class);
service.sayHello();

In this example we ask for the service by id using getService(String, Class). The services id is simply a concatenation of the module it is defined in, with the service point id. Hence we ask the registry for the service HelloWorldModule.HelloWorld.
If we don’t know the id of the service we want, we could equally ask for any service that implements some interface using the getService(Class) method in the Registry object.

Under the covers here, HiveMind is finding and instantiating the service for us, and returning it to us. In reality the returned object is not actually our service implementation class, it is a proxy to our class created at runtime. This mechanism allows HiveMind to manage the services and do lazy instantiation of the objects, and interconnect services without having to worry so much about the order in which they are created or used. This is obviously not in this example, and is do so transparently, but with more complex examples this becomes one of the many benefits of using HiveMind.

2 Comments

  • Hi Paul,
    I´m also trying to get my head around HiveMind (HM)!!
    Have you programmed anything using HM?
    There are very few real-world examples for HM - I´m looking to build a client-server swing app. which uses HM. Have you found anything which might help?
    Thanks for any help.
    Regards,
    Mark

  • I haven’t used HiveMind much since writing this post. It actually didn’t prove out to do what we wanted for the project I was working on at the time, and we went in a different direction with some home grown code.

RSS feed for comments on this post. TrackBack URL

Leave a comment