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:

Hello Stefan,

I have used the suggested sample program.
I am getting below error while running it on local.

My application.properties file entries are

application.name=hello-notification
server.port=8085
example.source.id=447
#example.websocket.url=ws://cumulocity.default.svc.cluster.local
example.websocket.url=ws://dev.iot.solenis.com
example.subscriber=testsubscriber
C8Y.baseURL=https://dev.iot.solenis.com

I am getting below error while running it on local

2023-06-14 21:52:15.779 INFO 20816 — [ main] c.e.n.helloworld.HelloWorldApplication : Started HelloWorldApplication in 16.282 seconds (JVM running for 18.043)
2023-06-14 21:52:49.566 WARN 20816 — [subscriptions-0] .s.r.MicroserviceSubscriptionsRepository : Configured application key ‘’ differs from the registered application key: ‘hello-notification-key’. Updating configuration.
2023-06-14 21:52:49.858 INFO 20816 — [subscriptions-0] c.e.n.helloworld.NotificationExample : Subscription added for Tenant ID:
2023-06-14 21:52:50.877 INFO 20816 — [subscriptions-0] c.e.n.helloworld.NotificationExample : Reusing existing subscription on device <447>
2023-06-14 21:52:51.395 INFO 20816 — [subscriptions-0] c.e.n.helloworld.NotificationExample : Connecting WebSocket client …
2023-06-14 21:52:52.003 INFO 20816 — [ctReadThread-35] c.e.n.h.w.t.TooTallNateWebSocketClient : WebSocket closed Code:1002, reason: Invalid status code received: 401 Status line: HTTP/1.1 401 Unauthorized
2023-06-14 21:52:52.004 INFO 20816 — [ctReadThread-35] c.e.n.helloworld.NotificationExample : Connection was closed.


Following entries are in application.properties file for dev platform

application.name=hello-notification
server.port=8085

example.source.id=447
example.websocket.url=ws://cumulocity.default.svc.cluster.local
#example.websocket.url=ws://dev.iot.solenis.com
example.subscriber=testsubscriber

This parameter is provided by platform for microservice deployed in cumulocity.

You can set it up to your dedicated cumulocity address. You can get one by trying free trial on www.cumulocity.com

C8Y.baseURL=http://cumulocity.default.svc.cluster.local
c8y.baseURL=https://dev.iot.solenis.com


I am getting following error on running it on dev platform

2023-06-14 16:18:17.567 INFO 13 — [main] c.e.n.helloworld.NotificationExample : Subscription added for Tenant ID:
2023-06-14 16:18:17.870 INFO 13 — [main] c.e.n.helloworld.NotificationExample : Subscription does not exist. Creating …
2023-06-14 16:18:17.982 INFO 13 — [main] c.e.n.helloworld.NotificationExample : Connecting WebSocket client …
2023-06-14 16:18:18.079 ERROR 13 — [WebSocketConnectReadThread-28] c.e.n.h.w.t.TooTallNateWebSocketClient : WebSocket error:java.net.UnknownHostException: cumulocity.default.svc.cluster.local: Name does not resolve
2023-06-14 16:18:18.079 ERROR 13 — [WebSocketConnectReadThread-28] c.e.n.helloworld.NotificationExample : We got an exception: java.net.UnknownHostException: cumulocity.default.svc.cluster.local: Name does not resolve
2023-06-14 16:18:18.086 INFO 13 — [WebSocketConnectReadThread-28] c.e.n.h.w.t.TooTallNateWebSocketClient : WebSocket closed Code:-1, reason: cumulocity.default.svc.cluster.local: Name does not resolve
2023-06-14 16:18:18.086 INFO 13 — [WebSocketConnectReadThread-28] c.e.n.helloworld.NotificationExample : Connection was closed.


Please suggest , if i need to modify any thing or am i doing some thing wrong over here.

For your first problem (401 when running it locally). Can you doublecheck that your User has the required Notification2 permission?

You can get all permissions of your User via API doing GET https://{host}/user/currentUser. The permissions are stated in the effectiveRoles fragment.

Hello Butz,

Thanks for your response.

Here is the response of <tenat_id>user/currentUser

{
“name”: “ROLE_RETENTION_RULE_READ”,
“self”: “https://t54905.iot.solenis.com/user/roles/ROLE_RETENTION_RULE_READ”,
“id”: “ROLE_RETENTION_RULE_READ”
},
{
“name”: “ROLE_AUDIT_ADMIN”,
“self”: “https://t54905.iot.solenis.com/user/roles/ROLE_AUDIT_ADMIN”,
“id”: “ROLE_AUDIT_ADMIN”
},
{
“name”: “ROLE_NOTIFICATION_2_ADMIN”,
“self”: “https://t54905.iot.solenis.com/user/roles/ROLE_NOTIFICATION_2_ADMIN”,
“id”: “ROLE_NOTIFICATION_2_ADMIN”
},


Still i am getting the same error .(Running it on local)

2023-06-16 15:02:10.770 INFO 22300 — [ main] c.e.n.helloworld.HelloWorldApplication : Started HelloWorldApplication in 17.606 seconds (JVM running for 20.087)
2023-06-16 15:02:45.814 WARN 22300 — [subscriptions-0] .s.r.MicroserviceSubscriptionsRepository : Configured application key ‘’ differs from the registered application key: ‘hello-notification-key’. Updating configuration.
2023-06-16 15:02:46.093 INFO 22300 — [subscriptions-0] c.e.n.helloworld.NotificationExample : Subscription added for Tenant ID:
2023-06-16 15:02:47.568 INFO 22300 — [subscriptions-0] c.e.n.helloworld.NotificationExample : Reusing existing subscription on device <447>
2023-06-16 15:02:48.331 INFO 22300 — [subscriptions-0] c.e.n.helloworld.NotificationExample : Connecting WebSocket client …
2023-06-16 15:02:49.266 INFO 22300 — [ctReadThread-35] c.e.n.h.w.t.TooTallNateWebSocketClient : WebSocket closed Code:1002, reason: Invalid status code received: 401 Status line: HTTP/1.1 401 Unauthorized
2023-06-16 15:02:49.267 INFO 22300 — [ctReadThread-35] c.e.n.helloworld.NotificationExample : Connection was closed.

Okay in that case it might be worthwhile to check if this is about the Notifications Endpoint or caused by your local setup. Can you read measurements via API in your local Service? (You can use Measurement API Class )

I’ve just tested the service on my localhost myself - works for me.

I am using these application.properties:

application.name=hello-notification
server.port=8085
example.source.id=45168311624
example.websocket.url=wss://my-tenant.eu-latest.cumulocity.com
example.subscriber=testsubscriber
C8Y.baseURL=https://my-tenant.eu-latest.cumulocity.com

And set these Environment variables locally:

C8Y_BOOTSTRAP_TENANT={tenant id, e.g. t948822};
C8Y_BOOTSTRAP_USER=servicebootstrap_hello-notification;
C8Y_BOOTSTRAP_PASSWORD={bootstrap user password};
C8Y_MICROSERVICE_ISOLATION=PER_TENANT;
C8Y_TENANT={tenant id, e.g. t948822};
C8Y_USER={my user name};
C8Y_PASSWORD={my user password};

The C8Y_USER needs to have ROLE_NOTIFICATION_2_ADMIN permission. If it does not have the permission, server responds with 403, not 401. So I suspect your problem has to do with your local setup (make sure the required environment variables are set).

To get the Bootstrap user you can use this API endpoint. Use the ID of the hello-notification service here.

// Also, when running from your localhost, make sure you are using wss:// rather than ws:// in your websocket URL.

Hello Butz,

I appreciate all your help and prompt response.

Now i am getting 403 – Access error

com.cumulocity.sdk.client.SDKException: Http status code: 403
{error=“security/Forbidden”,message=“Access is denied”,info=“https://cumulocity.com/guides/reference/openapi/”}
at com.cumulocity.sdk.client.ResponseParser.checkStatus(ResponseParser.java:75) ~[java-client-1015.0.386.jar:na]
at com.cumulocity.sdk.client.ResponseParser.parse(ResponseParser.java:57) ~[java-client-1015.0.386.jar:na]

My Properties file entries

application.name=hello-notification
server.port=8085

example.source.id=447
#example.websocket.url=ws://cumulocity.default.svc.cluster.local
example.websocket.url=wss://dev.iot.solenis.com
example.subscriber=testsubscriber

This parameter is provided by platform for microservice deployed in cumulocity.

You can set it up to your dedicated cumulocity address. You can get one by trying free trial on www.cumulocity.com

c8y.baseURL=http://cumulocity.default.svc.cluster.local
C8Y.baseURL=https://dev.iot.solenis.com


I have made the same entries as per suggestion in evn variables.


Current user is having the admin access

{
“name”: “ROLE_AUDIT_ADMIN”,
“self”: “https://t54905.iot.solenis.com/user/roles/ROLE_AUDIT_ADMIN”,
“id”: “ROLE_AUDIT_ADMIN”
},
{
“name”: “ROLE_NOTIFICATION_2_ADMIN”,
“self”: “https://t54905.iot.solenis.com/user/roles/ROLE_NOTIFICATION_2_ADMIN”,
“id”: “ROLE_NOTIFICATION_2_ADMIN”
},
{
“name”: “ROLE_ACCOUNT_ADMIN”,
“self”: “https://t54905.iot.solenis.com/user/roles/ROLE_ACCOUNT_ADMIN”,
“id”: “ROLE_ACCOUNT_ADMIN”
},

Let me know, what i am missing . Thank you so much for all help.

Okay, in that case I would need access to your tenant to verify the setup. I’ll send a PN.

Hello Butz,

Thanks for your prompt response and help. Let me know, what details i need to provide you .

Regards/-
Prakash

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.