ITarget

To receive messages in an object, it must implement the ITarget interface and be registered to the target registry of the thread.

The ITarget interface is quite extensive. However, since the target is usually derived from CTarget, these methods are already implemented by CTarget.

Handling messages

This method handles all messages directed to the target. CTarget implements this method by passing the incoming message to a message handler registry that belongs to the target. Therefore, it is sufficient to register a separate message handler for all messages to be handled.

void handleMessage(@NotNull CMessage aMsg) throws Exception;

De-register a target

The target can be unregistered from the target registry at any time, even within message handlers.

void deregisterTarget();

Get the address

This method returns the address of the target. Only a valid address is returned as long as the target is registered. The address is unique for the target. There are no two targets with the same address in a nyssr.net. Everyone can now send messages to this target by entering this address as the receiver address in the envelope of the message. Messages sent by this target contain this address as the sender address.

CTargetAddress getAddress();

Notification after registration

The method is called after the target has been registered. The call is made in the thread that also calls registerTarget(). This method can be overridden by any target, but the base method should be called.

void notifyTargetRegistered(@NotNull CTargetAddress aAddress,
                            @NotNull IId aQID,
                            @NotNull ITargetRegistry aTargetRegistry,
                            @NotNull IMessageSender aMessageSender,
                            @NotNull IMessageLogger aMessageLogger) throws Exception;

Notification before de-registration

The method is called before the target is deregistered. Messages can still be sent. The call is made in the thread that also calls deregisterTarget(). This method can be overridden by any target, but the base method should be called.

void notifyTargetWillBeRemoved() throws Exception;

Notification after de-registration

The method is called after the target has been deregistered. The call is made in the thread that also calls deregisterTarget(). This method can be overridden by any target, but the base method should be called.

void notifyTargetDeRegistered() throws Exception;

Check if a target is registered

Returns true if the target is registered.

boolean isTargetRegistered();

Get the target registry

If the target has been registered, you can get the target registry via getter.

ITargetRegistry getTargetRegistry();

Send a message

A message is sent. The components of a message can also be passed. The wantAnswer property of the message is not changed. The address of the target is entered as the sender address.

void send(@NotNull CMessage aMsg) throws CException;
void send(@NotNull CEnvelope aEnvelope,
          @NotNull CRecord aRecord) throws CException;

Send a notification

A message is sent. The components of a message can also be passed. The wantAnswer property of the message is set to false. A response is then only expected in the event of an error. The address of the target is entered as the sender address.

void sendNotification(@NotNull CMessage aMsg) throws CException;
void sendNotification(@NotNull CEnvelope aEnvelope,
                      @NotNull CRecord aRecord) throws CException;

Send a request

A message is sent. The components of a message can also be passed. The wantAnswer property of the message is set to true. A response is expected. The address of the target is entered as the sender address.

void sendRequest(@NotNull CMessage aMsg) throws CException;
void sendRequest(@NotNull CEnvelope aEnvelope,
                 @NotNull CRecord aRecord) throws CException;

Adding a message handler

A message handler can be registered for a particular message. The handler processes messages with the specified message ID. The specified message handler interface can also be a lambda or a method, see CTarget.

void addMessageHandler(@Nullable IId aMID,
                       @NotNull IMessageHandler aHandler);

Message-Queue

Each registered target is associated with a namespace thread and thus a message queue. Most namespaces have only one thread; therefore, you rarely have to deal with them. The queue ID of a target is available via a getter:

IId getQueueId();

With the queue ID (short: QID) the message queue as well as the thread can be determined via target registry.

Debug support

The output of messages via the logging framework can be switched on or off for a target. The default value is off. In addition, the corresponding LOG level of the owner object must be set to TRACE.

void setVerbose(boolean aVerbose);
boolean isVerbose();