Skip to content

[DM-4600] - Notifications2Api usage example #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions notification2-api-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To use Notifications2Api you need to:
1. Implement your own NotificationListener (one or more) that will handle messages.
2. Autowire Notifications2Api in your Microservice
3. Subscribe and enjoy!

You also need to set **C8Y.notifications2.websocketUrl** property to point to our pulsar websocket proxy.

In this small service you can dynamically subscribe/unsubscribe to different topics using REST API.
The listener will only output received messages to logs.

See Notifications2Api javadocs for more details.
80 changes: 80 additions & 0 deletions notification2-api-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>c8y.example</groupId>
<artifactId>c8y-examples</artifactId>
<version>2025.29.0-SNAPSHOT</version>
</parent>

<groupId>c8y.agents</groupId>
<artifactId>notification2-api-example</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>microservice-autoconfigure</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>java-client</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.38</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.9</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.cumulocity.examples.notifications2.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>microservice-package-maven-plugin</artifactId>
<version>2025.25.0</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>package</goal>
</goals>
<configuration>
<name>n2test</name>
<image>n2test</image>
<encoding>UTF-8</encoding>
<rpmSkip>true</rpmSkip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
33 changes: 33 additions & 0 deletions notification2-api-example/src/main/configuration/cumulocity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"apiVersion": "2",
"version": "1.0",
"provider": {
"name": "Cumulocity GmbH"
},
"isolation": "MULTI_TENANT",
"requiredRoles": [
"ROLE_INVENTORY_READ",
"ROLE_NOTIFICATION_2_ADMIN"
],
"roles": [
],
"resources": {
"memory": "1G"
},
"livenessProbe": {
"httpGet": {
"path": "/health",
"port": 80
},
"initialDelaySeconds": 30,
"periodSeconds": 10
},
"readinessProbe": {
"httpGet": {
"path": "/health",
"port": 80
},
"initialDelaySeconds": 30,
"periodSeconds": 10
}
}
11 changes: 11 additions & 0 deletions notification2-api-example/src/main/java/notifications2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package notifications2;

import com.cumulocity.microservice.autoconfigure.MicroserviceApplication;
import org.springframework.boot.SpringApplication;

@MicroserviceApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package notifications2.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources(value = {
@PropertySource(value = "classpath:notifications2.properties", ignoreResourceNotFound = true),
@PropertySource(value = "file:/etc/notifications2/notifications2.properties", ignoreResourceNotFound = true)
})
public class ApplicationPropertiesConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package notifications2.model;

import com.cumulocity.sdk.client.notification2.model.DeviceNotificationTopic;
import com.cumulocity.sdk.client.notification2.model.TenantNotificationTopic;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
* This is a model class that is accepted by test REST endpoints - it's not required by any means to have this to
* use Notifications2Api
*/
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Subscription implements Serializable {
private String deviceId;
private TenantNotificationTopic tenantTopic;
private DeviceNotificationTopic deviceTopic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package notifications2.rest;

import lombok.extern.slf4j.Slf4j;
import notifications2.model.Subscription;
import notifications2.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
* This resource allows to dynamically subscribe/unsubscribe notification topics.
* It's not required to have this to use Notifications2Api - it's just a part of test microservice logic.
*/
@Slf4j
@RestController
public class SubscriptionResource {
@Autowired
private SubscriptionService subscriptionService;

@RequestMapping(
path = "/subscribe",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseBody
public ResponseEntity<Void> subscribe(@RequestBody Subscription subscription) {
subscriptionService.subscribe(subscription);
return ResponseEntity.status(HttpStatus.OK).build();
}


@RequestMapping(
path = "/unsubscribe",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@ResponseBody
public ResponseEntity<Void> unsubscribe(@RequestBody Subscription subscription) {
subscriptionService.unsubscribe(subscription);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package notifications2.service;

import com.cumulocity.sdk.client.notification2.api.NotificationListener;
import com.cumulocity.sdk.client.notification2.model.Notification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* This is the example implementation of the listener. All messages received in this service will end up here.
* Implementing your own listener IS REQUIRED to use Notifications2Api
*/
@Component
@Slf4j
public class MyNotificationListener implements NotificationListener {

@Override
public void onMessage(Notification message, String subscriptionName) {
log.info("Received message from subscription {}\n{}", subscriptionName, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package notifications2.service;


import com.cumulocity.sdk.client.notification2.api.Notifications2Api;
import notifications2.model.Subscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SubscriptionService {
/**
* Consumer name must be unique, so if your microservice is clustered, you need to make sure that every instance has a unique identifier here.
*/
private static final String CONSUMER = "notifications2Tester";

@Autowired
private Notifications2Api notifications2Api;

@Autowired
private MyNotificationListener listener;

public void subscribe(Subscription cmd) {
if (cmd.getDeviceTopic() != null && cmd.getDeviceId() != null) {
notifications2Api.subscribeToDeviceTopic(CONSUMER, cmd.getDeviceTopic(), cmd.getDeviceId(), listener);
}
if (cmd.getTenantTopic() != null) {
notifications2Api.subscribeToTenantTopic(CONSUMER, cmd.getTenantTopic(), listener);
}
}

public void unsubscribe(Subscription cmd) {
if (cmd.getDeviceTopic() != null && cmd.getDeviceId() != null) {
notifications2Api.unsubscribeFromDeviceTopic(CONSUMER, cmd.getDeviceTopic(), cmd.getDeviceId());
}
if (cmd.getTenantTopic() != null) {
notifications2Api.unsubscribeFromTenantTopic(CONSUMER, cmd.getTenantTopic());
}
}
}
15 changes: 15 additions & 0 deletions notification2-api-example/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<include resource="org/springframework/boot/logging/logback/base.xml"/>

<logger name="org.springframework" level="info"/>
<logger name="com.cumulocity.sdk.client.notification2" level="trace"/>
<logger name="com.cumulocity.examples.notifications2" level="trace"/>

<root level="warn">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C8Y.baseWebsocketUrl=ws://pulsar-websocketproxy.c8y-messaging-service.svc.cluster.local:80
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<module>snmp</module>
<module>mqtt-client</module>
<module>microservices</module>
<module>notification2-api-example</module>
</modules>

<licenses>
Expand Down