How to listen all MeasurementFragment in microservice

I wanted to listen all measurement in my microservices. I am able to get the values in EPL but my EPL implementation is taking too much memory from the available memory.

on all MeasurementFragment(type=“xyz”,valueFragment=“xyz”, valueSeries = “ABC”) as mf {

//

do some calculation
}

Two options to achieve that.

  1. Pulling the information periodically using the REST API and (date) filters Cumulocity IoT - OpenAPI Specification
  2. Using Notification API to subscribe on all devices with measurements and filter the relevant one: Cumulocity IoT - OpenAPI Specification

Hint: Don’t use the realTimeAPI but only Notification 2.0

Thanks a lot Stefan,

If possible for you , Can you explain me little me more with sample code . It would be great …

Hi @prakash.veer06

here is an example subscribing on measurements:

here is an example of polling data (but for the inventory, you can adapt that for the measurements):

Hello Stefan,

I am getting below error . I and running this application on my local system and connect with dev tenent.

Could you please help me in this.

023-05-25 17:08:41.087 ERROR 32832 — [subscriptions-0] .s.s.i.MicroserviceSubscriptionScheduler : Error while reacting on microservice subscription

com.cumulocity.sdk.client.SDKException: Error invoking GET http://localhost:8181/application/applicationsByName/hello-microservice-notification
at com.cumulocity.microservice.subscription.repository.impl.LegacyMicroserviceRepository.handleException(LegacyMicroserviceRepository.java:150) ~[microservice-subscription-1011.0.12.jar!/:na]
at com.cumulocity.microservice.subscription.repository.impl.LegacyMicroserviceRepository.getByName(LegacyMicroserviceRepository.java:101) ~[microservice-subscription-1011.0.12.jar!/:na]
at com.cumulocity.microservice.subscription.repository.impl.LegacyMicroserviceRepository.register(LegacyMicroserviceRepository.java:62) ~[microservice-subscription-1011.0.12.jar!/:na]
at com.cumulocity.microservice.subscription.repository.MicroserviceSubscriptionsRepository.register(MicroserviceSubscriptionsRepository.java:110) ~[microservice-subscription-1011.0.12.jar!/:na]
at com.cumulocity.microservice.subscription.service.impl.MicroserviceSubscriptionsServiceImpl.registerApplication(MicroserviceSubscriptionsServiceImpl.java:169) ~[microservice-subscription-1011.0.12.jar!/:na]
at com.cumulocity.microservice.subscription.service.impl.MicroserviceSubscriptionsServiceImpl.subscribe(MicroserviceSubscriptionsServiceImpl.java:114) ~[microservice-subscription-1011.0.12.jar!/:na]
at com.cumulocity.microservice.subscription.service.impl.MicroserviceSubscriptionScheduler.lambda$schedulePeriodicSubscription$0(MicroserviceSubscriptionScheduler.java:81) ~[microservice-subscription-1011.0.12.jar!/:na]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8181 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

Hi Prakash,
I think you don’t have done the required configuration set to run a microservice locally:
https://cumulocity.com/guides/microservice-sdk/java/#running-the-microservice-locally

You can also find good examples here:

Thanks for your reply Stefan,

I am able to deploy my microservice in Complicity and getting below logs but not getting the measurement data

80 (http) with context path ‘’
2023-05-26 08:10:22.207 INFO 13 — [main] c.e.n.helloworld.NotificationExample : Subscription added for Tenant ID:
2023-05-26 08:10:22.403 INFO 13 — [main] c.e.n.helloworld.NotificationExample : Reusing existing subscription on device <126546272>
2023-05-26 08:10:22.427 INFO 13 — [main] c.e.n.helloworld.NotificationExample : Connecting WebSocket client …
2023-05-26 08:10:22.612 INFO 13 — [WebSocketConnectReadThread-28] c.e.n.helloworld.NotificationExample : Connected to Cumulocity notification service over WebSocket ws://dev.iot.solenis.com/notification/realtime/notification2/consumer/?token=eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ0ZXN0c3Vic2NyaWJlciIsInRvcGljIjoidDU0OTA1L3JlbG5vdGlmL3Rlc3QxMjY1NDYyNzJzdWJzY3JpcHRpb24iLCJqdGkiOiJkM2YwOTVmYi1iOWE0LTRmZGYtODMyOS02OTY5OTQ2OTJkODYiLCJpYXQiOjE2ODUwODg2MjIsImV4cCI6MTY4NTE3NTAyMn0.hlPIqa8NFb2NV5cydb57X3y9JNz5cyu5iEbNMCcj6UVbM6l_22esmjMNIO2SWzqg6JsDkwqMfeq1L5fxHE_zkmB3rGHHakDm9A3VmcI55_mu-DZyWzQu9UiG6pcnQfJ5ECd3DO4rIb4e7hrQx7kdrtnidoK3TV_Blp00XOe5yGaDuQqMWakmhzYgz6m4rhd3AzuY6SHAFr6DeqPBkfiTGrXi-ud_0uOOaZE6xYFYiCpfJSYsyNxNc-8KzBiUz1z0VwuUkdbLDoaCgl08P9y2I1DqgUgi-wSMMSxTMabP4pvL_uNT7JQQ5Ke1I6efetHdWsB6lhBJhSX0GVTJj_MrtQ
2023-05-26 08:10:22.617 INFO 13 — [main] c.e.n.helloworld.HelloWorldApplication : Started HelloWorldApplication in 18.414 seconds (JVM running for 20.565)
2023-05-26 08:10:34.023 INFO 13 — [http-nio-80-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ‘dispatcherServlet’
2023-05-26 08:10:34.023 INFO 13 — [http-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet ‘dispatcherServlet’
2023-05-26 08:10:34.101 INFO 13 — [http-nio-80-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 78 ms

That’s hard to debug remotely. Most likely there is something wrong with your subscription. Please note that you have to subscribe on “mo” context using “measurements” subscription filter.

https://cumulocity.com/api/core/10.16.0/#operation/postNotificationSubscriptionResource

Hello Stefan,

I am able to deploy
SoftwareAG/cumulocity-microservice-templates/tree/main/realtime"*
with my changes and its running fine on local as well as when i deploy on VM and able to get measurements data as in listener.

I am able to deploy this microservices in complicity tenant as well and i got this response when i check the health of microservice.

/service/digitank-calculation/health"*

Response
{
“status”: “UP”,
“components”: {
“diskSpace”: {
“status”: “UP”,
“details”: {
“total”: 158395219968,
“free”: 126023880704,
“threshold”: 10485760,
“exists”: true
}
},
“heapMemory”: {
“status”: “UP”,
“details”: {
“init”: 8388608,
“used”: 34932736,
“committed”: 122683392,
“max”: 392167424,
“threshold”: 19608371
}
},
“livenessState”: {
“status”: “UP”
},
“nonHeapMemory”: {
“status”: “UP”,
“details”: {
“init”: 7667712,
“used”: 87188024,
“committed”: 91262976,
“max”: 377487360,
“threshold”: 18874368
}
},
“ping”: {
“status”: “UP”
},
“readinessState”: {
“status”: “UP”
}
},
“groups”: [
“liveness”,
“readiness”
]
}

But this microservice is not listening the measurement update for specific device or all device.
Am i missing some thing . Please suggest .

That one is using the “old” realtime API. You shoud use the notification 2.0 API instead: