Apama 10.15.0 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The Cumulocity IoT Transport Connectivity Plug-in | Invoking microservices
 
Invoking microservices
The Cumulocity IoT transport has a CumulocityRequestInterface event which allows you to invoke other microservices within Cumulocity IoT from an EPL application. This can be used from an Apama instance outside of Cumulocity IoT and within Cumulocity IoT, either as an EPL application or a custom microservice.
Before you can create an HTTP request, you need to call a static connectToCumulocity() action in order to connect (as shown in the later example). The following is the format of the action on the helper class that you call to create a request:
action createRequest(string method, string path, any payload) returns Request
Pass the following:
*The specific type of HTTP request that is to be created, such as GET or PUT.
*A specific path that you want to append to your request. For example, the path for a microservice that is running on your desired tenant: /service/myMicroService/path/under/microservice.
*The payload will be encoded as JSON. For example, a dictionary will be converted to a JSON object.
This action will return an instance of a Request from the generic HTTP API (see also Using predefined generic event definitions to invoke HTTP services with JSON and string payloads) with configuration set up on the request. You can later call execute on this request, passing in a handler to deal with any response.
The following example shows how to make use of this class, that is, how to make an HTTP request in order to retrieve information from a running microservice:
monitor CumulocityTestMonitor {
action onload() {
try{
CumulocityRequestInterface cInterface :=
CumulocityRequestInterface.connectToCumulocity();
Request req := cInterface.createRequest("GET",
"/service/myMicroService/path/under/microservice",
{"request":"data"});
req.execute(getHandler);
}
catch (com.apama.exceptions.Exception e) {
log "Error thrown trying to create a Cumulocity Request " +
e.toString() at ERROR;
}
}

action getHandler(Response resp) {
AnyExtractor d := resp.payload;
log "Response output: " + d.toString() at INFO;
log "Test Done";
}
}
The CumulocityRequestInterface will automatically detect if it is running inside or outside of Cumulocity IoT and will automatically connect. If running remotely, it will rely on properties being set, which will be the connection details provided for the transport. You can do this by creating a .properties file in your project and specifying it with the --config option when starting a correlator (see Starting the correlator).
Optionally, you can set the following if you are connecting to a Cumulocity IoT server with a self-signed or private certificate. Set this to the path to the certificate authority file by which the server's certificate was signed:
CUMULOCITY_TLS_CERT_AUTH_FILE
The helper class is included in the Utilities for Cumulocity bundle in Software AG Designer. You can also find it in the monitor/cumulocity directory of your Apama installation.