How to start a simple target

Target, van Gogh

Short variant

A simple target is a class derived from CTarget. This target must be registered to the target registry of a namespace. There it receives its target address and can receive messages from now on.

final INamespace namespace = mDependencies.getNamespaceRegistry()
                                          .getNamespace(CWellKnownNID.SYSTEM);
if (namespace != null)
{
    namespace.getTargetRegistry()
             .registerTarget(this);
}

In order to use the mDependencies interface, as always, a few preparations must be made. For this we refer to the Tutorial on initializing a Java package.

Detailed variant

A detailed target looks like this:

class CMyTarget extends CTarget implements IService
{
    private final IDependencies mDependencies;

    CMyTarget(@NotNull final IDependencies aDependencies)
    {
        mDependencies = aDependencies;

        addMessageHandler(CRecordStartTarget.ID,
                          this::asyncStartTarget);
    }

    private boolean asyncStartTarget(@NotNull final CEnvelope aEnvelope,
                                     @NotNull final CRecord aRecord)
    {
        // In the SYSTEM namespace thread
        // do something...

        aEnvelope.setResultSuccess();
        return true;
    }

    @Override
    public void activate(@NotNull final IServiceRegistry aServiceRegistry) throws Exception
    {
        final INamespace namespace = mDependencies.getNamespaceRegistry()
                                                  .getNamespace(CWellKnownNID.SYSTEM);
        assert namespace != null;
        namespace.getTargetRegistry()
                 .registerTarget(this);
    }

    @Override
    public void deactivate(@NotNull final IServiceRegistry aServiceRegistry) throws Exception
    {
        deregisterTarget();
    }
}

The activate() and deactivate() methods are called by the ServiceStarter. In these methods the target is registered or de-registered. The method asyncStartTarget() is called by the system when the target is registered. It is the first message that the target receives. The mapping of the message ID CRecordStartTarget.ID to the method asyncStartTarget() is done in the constructor by adding a message handler.

A minimal target

A minimal target is also derived from CTarget, but does not implement the IService interface. This will eliminate the activate() and deactivate() methods. Registration is simply done by the caller. The handling of the CRecordStartTarget message is also optional. Just register the message handlers you need.

class CMyTarget extends CTarget
{
    CMyTarget()
    {
        addMessageHandler(CRecordMyMessage.ID,
                          this::asyncMyMessage);
    }

    private boolean asyncMyMessage(@NotNull final CEnvelope aEnvelope,
                                   @NotNull final CRecord aRecord)
    {
        // do something...

        aEnvelope.setResultSuccess();
        return true;
    }
}

Creation and registration by another target:

final ITarget tgt = new CMyTarget();
getTargetRegistry().register(tgt);