How to offer a nano service

Nano services are needed for three purposes:
-
Offering notifications.
For a nano service, interested targets can register an observer. The notification trigger sends messages to the nano service, of which each observer receives a copy.
-
Operation of a tiny services
The owner registers a nano service and registers himself as an observer. Clients of the service send a message to the nano service, which is forwarded to the owner. Advantage: The client does not need the address of the providing service.
-
Observation
A silent observer can register an observer for a nano service and get copies of the messages to the service.
Prerequisite for the tutorial
You should have read these tutorials beforehand:
Package initialization
How to start a simple target
How to register a nano service
How to add an observer to a nano service
Preliminary work
As described in the tutorials, first a nano service is registered and then an observer with its own address is added.
Nano service message processing
Another message handler is added in the constructor.
addMessageHandler(CRecordAppRunApplication.ID,
this::asyncStartApp);
The method of the message handler is implemented:
- The data is fetched from the message if necessary.
- The data will be verified.
- Something will be done with it.
- New data is placed in the message (the same message is returned as a response).
- A result code is set.
- After leaving the handler, the message is automatically sent back to the sender by the system (if the sender wishes a reply).
private boolean asyncStartApp(@NotNull final CEnvelope aEnvelope,
@NotNull final CRecord aRecord) throws CException
{
// get preset name
final String presetName = CRecordAppRunApplication.getPresetName(aRecord,
null);
CUtilCheck.checkEmptyString(presetName,
"Missing Preset Name.");
// get remote skin client
final CNodeAddress remoteSkinClient = CRecordAppRunApplication.getRemoteSkinClient(aRecord,
null);
// get session token
final UUID sessionToken = CRecordAppRunApplication.getSessionToken(aRecord,
null);
CUtilCheck.checkNotNull(sessionToken,
"Missing Session Token");
// ...
// Do something
aEnvelope.setResult(CResultCode.SUCCESS,
"");
return true;
}
Related Pages
How to send a message
Learn how to send messages in nyssr.net to various targets: known receivers, local/remote nano services, microservices, and broadcast messages.
How to register a nano service
earn how to register Nano Services in nyssr.net using the Nano Service Registry, ServiceStarter, and filtering for the SYSTEM Namespace.
How to add an observer to a nano service
Learn how to add an Observer to a Nano Service in nyssr.net using the Record Helper or directly via the Nano Service Registry and IServiceStarter dependencies.
How to catch a message
Learn how to catch and process messages in nyssr.net Targets using Message Handlers. Covers request/response logic and setting message result codes.
How to build a message
Learn how to build a message (Envelope + Record) in nyssr.net. Use the JSON-based Record Generator for simple, type-safe construction and data access.
How to use the job engine for bulk tasks
Learn how to use the Job Engine in nyssr.net to process bulk tasks (like file hashing) in parallel using multiple working threads and an owner target.