Skip to content

Getting started aka Hello World

Leon Rosenberg edited this page Jun 16, 2018 · 2 revisions

Getting started

This page explains how to write your very first service with DistributeMe. This involves some simple steps.

The first service

Our first service will be pretty straight forward. It does almost nothing, but prints something into console. DistributeMe is all about interfaces, and therefore we first have to define the interface: HelloWorldService.java

package org.distributeme.examples.helloworld;
 
import net.anotheria.anoprise.metafactory.Service;
import org.distributeme.annotation.DistributeMe;
 
@DistributeMe
public interface HelloWorldService extends Service{
    void printMessage(String message);
}

There are actually two things that differ from any other normal interface of a service:

  • @DistributeMe annotation above the interface declaration which tell the preprocessor to generate java code for this service and which can contain additional generation parameters we will care about later.
  • extends Service{ - which is needed by DistributeMe internal DI management. It's an empty interface, which signalizes that this component is a service and not of further interest now.

Service Implementation

Of course we have to implement the interface to make it work. The implementation is absolutely straight-forward: HelloWorldServiceImpl.java

package org.distributeme.examples.helloworld;
 
public class HelloWorldServiceImpl implements HelloWorldService{
  @Override
  public void printMessage(String message) {
    System.out.println(message);
  }
}

The client

Finally we need something that calls the service.

RemoteClient.java

package org.distributeme.examples.helloworld;
import java.util.Date;
import org.distributeme.core.ServiceLocator;
 
 
public class RemoteClient {
  public static void main(String a[]){
    HelloWorldService service = ServiceLocator.getRemote(HelloWorldService.class);
    String message = "Hello world at "+new Date(System.currentTimeMillis());
    System.out.println("Server should print out following message now: "+message);
    service.printMessage(message);
  }
}

The client is called remote client because it calls explicitly the remote instance. Usually you would only know the interface and let some DI mechanism decide which instance to return (for example: MetaFactory). However, for the test its easier to call the remote service explicitly.

So the only unusual thing in this client is the following call:

HelloWorldService service = ServiceLocator.getRemote(HelloWorldService.class);

This is an utility provided by distributeme, which knows the name of the class, you are looking for (but you can call new RemoteHelloWorldServiceStub() directly instead).

Now you have all you need to build and run the example.

Building

We use maven to build DistributeMe but it of course runs with ant or gradle as well. For maven pom checkout the distributeme-test module of the project.

Clone this wiki locally