Cumulocity Microservice Tutorial Part 3 (Microservice/User-Context, external ID & measurement creation)


This microservice will create (by using the microservice context) every x seconds a new temperature measurement for a new created managed object.


The tutorial is based on Microservice Tutorial Part 1a, Part 1b & Part 2.

Source code

Microservice context

After subscription of a new microservice C8Y will create a new service user. This service user will be assigned to the microservice. The assigned user has access to the tenant and is for this reason able to send internal REST calls to C8Y core. If a microservice would like to e.g. generate new measurements, the measurements will be sent via this “service user” to C8Y core.


In some cases, you would like to restrict the number of calls for a specific user or you don’t want to use the service user for any other reason. In this case, you need to use the user context. 

A microservice is in general running within the microservice context.

Microservice preparation

  • Create a new microservice by using postman
  • Get the bootstrap credentials by using postman
  • Configure your

Measurement creation

Import the measurement API:

MeasurementApi measurementApi;

Create a new temperature measurement:

TemperatureMeasurement temperatureMeasurement = new TemperatureMeasurement();

Set the temperature value and unit:

temperatureMeasurement.setT(new MeasurementValue(BigDecimal.valueOf(RandomUtils.nextInt(100)), "C"));

Create a new measurement representation:

MeasurementRepresentation measurementRepresentation = new MeasurementRepresentation();

Set the type of the planned measurement e.g. temperature


Set the generation time of the measurement:

measurementRepresentation.setDateTime(new DateTime());

Define the managed object where you would like to send the measurements:


Set the temperature measurement you defined before:


Create the measurement:


External id creation

Create a new external id representation:

ExternalIDRepresentation externalIDRepresentation = new ExternalIDRepresentation();

Definition of the external id:


Assign the external id to an existing managed object:


Definition of the serial:


Creation of the external id:


Context definition and scheduling

Set the context e.g. for all tenants. In this case, many tenants e.g. by uploading the microservice to the management tenant are able to subscribe to this microservice: 


Scheduling to generate every x seconds a new measurement:

@Scheduled(initialDelay=10000, fixedDelay=10000)

public void startThread() {

        subscriptionService.runForEachTenant(new Runnable() {


        public void run() {

                // TODO



You can get the microservice and user context information by using the context service.

Microservice credentials

private final ContextService<MicroserviceCredentials> contextServiceMicroserviceCredentials;
MicroserviceCredentials microserviceCredentials = contextServiceMicroserviceCredentials.getContext();


User credentials

private final ContextService<UserCredentials> contextServiceUserCredentials;
UserCredentials userCredentials = contextServiceUserCredentials.getContext();


List of all tutorials

Cumulocity Microservice Tutorial Part 1a (Hello World)
Cumulocity Microservice Tutorial Part 1b (local testing)
Cumulocity Microservice Tutorial Part 2 (Managed Object CRUD)
Cumulocity Microservice Tutorial Part 3 (Microservice/User-Context, external ID & measurement creation
Cumulocity Microservice Tutorial Part 4 (Read and display data from an external weather API & microservice settings/configuration)
Cumulocity Microservice Tutorial Part 5 (Creation of custom measurements)
Cumulocity Microservice Tutorial Part 6 (send a REST request via SDK to get the latest measurement)
Cumulocity Microservice Tutorial Part 7 (Alarm handling)
Cumulocity Microservice Tutorial Part 8 (Event handling)
Cumulocity Microservice Tutorial Part 9 (Operation handling)