Eclipse Communication Framework (ECF)

The Classic Hello World Example for ECF

Overview

As a simple first look at ECF, take a look at implementing a simple "Hello World" plugin that can send messages to other instances of the example app. As a messaging application, or example is only really useful when multiple instances are running.

The Plugin Perspective

So what we are going to do is create an Eclipse plugin that contributes to the workbench via the actionSets extension point. We will create a simple action that will trigger the instantiation of our Hello World client class:

public void run(IAction action) {
        HelloClient client = new HelloClient();
        try {
            client.createAndConnect(IDFactory.createStringID("ecftcp://localhost:3282/server"));
        } catch (Exception e) {
            e.printStackTrace();
        }
}
From the example source code provided, you can see the Eclipse plugin infrastructure code in the HelloPlugin and HelloAction classes. These classes will not be discussed in this tutorial as they are not relevent to ECF.

The ECF Perspective

When we create our client class, HelloClient, from our action, we will provide the class with some setup information it needs to connect to an ECF server:

public class HelloClient {
    
    public static final String DEFAULT_CONTAINER_TYPE = "ecf.generic.client";
    public static final String SHARED_OBJECT_ID = "myobject";
    
    public HelloClient() {
        super();
    }

    public ISharedObjectContainer createAndConnect(ID groupID) {
        ISharedObjectContainer container = null;
        try {
            // Create container instance via ECF container factory
            container = SharedObjectContainerFactory.getDefault().createSharedObjectContainer(DEFAULT_CONTAINER_TYPE);
            // Create ID for shared object
            ID sharedObjectID = IDFactory.createStringID(SHARED_OBJECT_ID);
            // Create actual shared object
            ISharedObject sharedObject = new HelloSharedObject();
            // Add shared object to container
            container.getSharedObjectManager().addSharedObject(sharedObjectID,sharedObject,new HashMap());
            // Join group identified by groupID
            container.joinGroup(groupID,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return container;
    }
}

Note for this Hello World example to be able to join a shared container, an instance of org.eclipse.ecf.provider.app.ServerApplication in the org.eclipse.ecf.example.collab needs to be running on the same machine. The configuration information we are passing to the HelloClient is the following:
  1. An ID to represent the group of contained object.
    client.createAndConnect(IDFactory.createStringID("ecftcp://localhost:3282/server"));
  2. An ID to represent the particular type of shared object that is being shared.
    public static final String SHARED_OBJECT_ID = "myobject";
  3. A classname that defines in what ways the instances of our shared objects will be able to communicate.
    public static final String DEFAULT_CONTAINER_TYPE = "ecf.generic.client";
  4. For our example, we will use the ecf.generic.client class. You can see an example of this class in the org.eclipse.ecf.provider.comm.tcp.Client class in the org.eclipse.ecf.provider plugin.
With this information, a SharedContainer is created. From there, we simply create an instance of a SharedObject, in this case the HelloSharedObject class, and join it to the group. From there, all instances in the container will be able to communicate in this code block:
    public void handleEvent(Event event) {
        if (event instanceof ISharedObjectActivatedEvent) {
            System.out.println("HELLO WORLD "+getID()+".  I'm activated!");
        } else if (event instanceof ISharedObjectDeactivatedEvent) {
            System.out.println("GOODBYE from "+getID()+".  I'm deactivated!");
        } else if (event instanceof ISharedObjectContainerJoinedEvent) {
            System.out.println("Remote "+((ISharedObjectContainerJoinedEvent)event).getJoinedContainerID()+" joined!");
        } else if (event instanceof ISharedObjectContainerDepartedEvent) {
            System.out.println("Remote "+((ISharedObjectContainerDepartedEvent)event).getDepartedContainerID()+" departed!");
        } else if (event instanceof ISharedObjectMessageEvent) {
            ISharedObjectMessageEvent evt = (ISharedObjectMessageEvent) event;
            System.out.println("Got message "+evt.getData()+" from "+evt.getSenderSharedObjectID());
        }
    }

Conclusion

All the "application logic" of our Hello World example is defined in the HelloSharedObject class and this really is the core value that ECF provides. To grow our application, there are specific parts in the framework for various logical aspects of communication. For example, to add another protocol, we would only need to create a new protocol provider, and our core messaging logic would remain untouched.

The source code used in this example may be found at [].