The target registry is part of each namespaces. Here targets are registered. It can be easily fetched from a namespace via a getter:
final ITargetRegistry tr = namespace.getTargetRegistry();
Another method is to go via the service registry. A filter then needs to be specified to specify the namespace:
final ITargetRegistry tr = CServiceRegistry.getInstance() .getService(ITargetRegistry.class, "nid=SYSTEM");
Inside an already registered target there is also a simple getter:
final ITargetRegistry tr = getTargetRegistry();
Register a target
When a target is registered, it receives an address that is unique in the nyssr.net. Then it can receive messages.
To register target there is a method, which binds the target to the first (and usually only) thread in the namespace. It receives a sequential numeric ID, the target ID (TID for short).
void registerTarget(@NotNull ITarget aTarget) throws CException;
It is also possible to specify any target ID:
void registerTarget(@NotNull ITarget aTarget, @NotNull IId aTID) throws CException;
If multiple threads exist in the namespace, the queue ID of the thread can also be supplied:
void registerTarget(@NotNull ITarget aTarget, @NotNull IId aTID, @NotNull IId aQID) throws CException;
During the registration the following actions are performed:
Call the target method
notifyTargetRegistered. This method is used by CTarget to store the various parameters.
- The target monitor is triggered. Interested targets can register Observer for this, to receive information about newly registered targets.
The nano service CRecordNotifyTargetRegistered is triggered in the namespace
SYSTEM. Interested targets can register Observer for this, to receive information about newly registered targets.
- The message CRecordStartTarget is sent to the target (asynchronously). It is the first message that the target gets. The delivery of the message is always in the thread of the namespace that was specified during registration.
- A LOG debug message is printed with the logger of the TargetRegistry.
Deregistration of a target
Targets can be deregistered at any time, even while a message is being processed.
void deregisterTarget(@NotNull IId aTID); void deregisterTarget(@NotNull ITarget aTarget); void deregisterTarget(@NotNull CTargetAddress aAddress);
Information on targets
Fetching a target can be done via the ID.
@Nullable ITarget getTarget(@NotNull IId aTID);
Checking whether a target is registered is done with another method:
boolean isTargetRegistered(@NotNull IId aTID);
Even more information about registered targets can be determined. This can be useful if, for example, a list of registered targets is to be displayed in a dialog. The records are of type CRecordDataTargetInfo.
CRecord @NotNull  getTargetInfo(boolean all, @NotNull Collection<IId> requestList) throws CException;
The number of targets registered in the registry can be determined via
Message queues (Threads)
The queue ID to which a target is assigned can easily be found out by using the target ID:
IId getQueueId(@NotNull IId aTID);
Likewise, the message queue can be fetched:
IMessageQueue getQueue(@NotNull IId aTID);
The following method returns a list of all target IDs assigned to a given queue:
IId @NotNull  getTIDs(@NotNull IId aQID);