Skip to content

Commit

Permalink
add async example
Browse files Browse the repository at this point in the history
  • Loading branch information
PestusAtSAG committed Jul 30, 2024
1 parent 6a5fed0 commit 033bcf0
Show file tree
Hide file tree
Showing 18 changed files with 625 additions and 0 deletions.
101 changes: 101 additions & 0 deletions async/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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>cumulocity.microservice.templates</groupId>
<artifactId>cumulocity-microservice-templates</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>cumulocity-microservice-templates-async</artifactId>
<description>Cumulocity Microservice Async template </description>

<dependencies>
<dependency>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>microservice-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.nsn.cumulocity.model</groupId>
<artifactId>device-capability-model</artifactId>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.properties</include>
<include>**/application.properties</include>
<include>**/banner.txt</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<mainClass>com.c8y.ms.templates.basic.App</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>microservice-package-maven-plugin</artifactId>
<version>${c8y.version}</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>package</goal>
</goals>
<configuration>
<name>templates-async</name>
<image>templates-async</image>
<encoding>UTF-8</encoding>
<skip>${c8y.docker.skip}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
35 changes: 35 additions & 0 deletions async/src/main/configuration/cumulocity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"apiVersion": "2",
"version": "@project.version@",
"provider": {
"name": "Cumulocity GmbH"
},
"isolation": "MULTI_TENANT",
"resources": {
"cpu": "1",
"memory": "1024M"
},
"requiredRoles": [
"ROLE_INVENTORY_READ",
"ROLE_INVENTORY_CREATE",
"ROLE_INVENTORY_ADMIN"
],
"roles": [
],
"livenessProbe": {
"httpGet": {
"path": "/health",
"port": 80
},
"initialDelaySeconds": 30,
"periodSeconds": 10
},
"readinessProbe": {
"httpGet": {
"path": "/health",
"port": 80
},
"initialDelaySeconds": 10,
"periodSeconds": 10
}
}
39 changes: 39 additions & 0 deletions async/src/main/configuration/templates-async-logging.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>

<logger name="com.cumulocity.microservice.security.filter.provider.SpringSecurityContextProvider" level="ERROR" />

<springProfile name="dev">
<logger name="org.springframework.web" level="INFO" />
<logger name="org.apache.commons.httpclient" level="INFO" />
<logger name="httpclient.wire" level="DEBUG" />
<logger name="com.c8y.test" level="DEBUG" />
<logger name="com.cumulocity" level="DEBUG" />
</springProfile>

<springProfile name="test">
<logger name="org.springframework.web" level="INFO" />
<logger name="org.apache.commons.httpclient" level="INFO" />
<logger name="httpclient.wire" level="INFO" />
<logger name="com.c8y.test" level="DEBUG" />
<logger name="com.cumulocity" level="DEBUG" />
</springProfile>

<springProfile name="prod">
<logger name="com.cumulocity" level="INFO" />
<logger name="com.c8y.test" level="INFO" />
</springProfile>


<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
17 changes: 17 additions & 0 deletions async/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM alpine:3 as base
RUN apk add --no-cache [email protected]@
RUN jlink \
--module-path /opt/java/jmods \
--compress=2 \
--add-modules java.se,jdk.unsupported,jdk.crypto.ec \
--no-header-files \
--no-man-pages \
--output /opt/jdk-mini
FROM alpine:3
COPY --from=base /opt/jdk-mini /opt/jdk-mini
RUN apk add --no-cache coreutils
ENV JAVA_HOME=/opt/jdk-mini
ENV PATH="$PATH:$JAVA_HOME/bin"
COPY etc/ /etc/service-request-mgmt/
ADD resources/* /data/
ENTRYPOINT ["java", "-jar", "/data/@[email protected]"]
13 changes: 13 additions & 0 deletions async/src/main/java/com/c8y/ms/templates/async/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.c8y.ms.templates.basic;

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

@MicroserviceApplication
@EnableScheduling
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.c8y.ms.templates.async.controller;

import com.c8y.ms.templates.async.model.Device;
import com.c8y.ms.templates.async.service.DeviceService;
import com.cumulocity.rest.representation.inventory.ManagedObjectRepresentation;
import com.google.common.base.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* This is an example controller. This should be removed for your real project!
*
* @author APES
*/
@RestController
@RequestMapping("/devices")
public class DeviceController {

private DeviceService deviceService;

public DeviceController(DeviceService deviceService) {
this.deviceService = deviceService;
}

@GetMapping(path = "/names", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<String>> getAllDeviceNames() {
List<String> response = deviceService.getAllDeviceNames();
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping(path = "/{deviceId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getDeviceRepresentationById(final @PathVariable String deviceId) {
final Optional<ManagedObjectRepresentation> deviceRepresentationOptional = deviceService.getDeviceRepresentation(deviceId);

if (!deviceRepresentationOptional.isPresent()) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}

return new ResponseEntity<>(deviceRepresentationOptional.get().toJSON(), HttpStatus.OK);
}

@PostMapping(path = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createNewDevice(final @RequestBody @Valid Device device) {
final Optional<ManagedObjectRepresentation> deviceCreated = deviceService.createDevice(device);

if (!deviceCreated.isPresent()) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<>(deviceCreated.get().toJSON(), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.c8y.ms.templates.async.controller;

import com.c8y.ms.templates.async.service.MeasurementService;
import com.cumulocity.rest.representation.measurement.MeasurementRepresentation;
import com.google.common.base.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/measurements")
public class MeasurementController {

private final MeasurementService measurementService;

public MeasurementController(MeasurementService measurementService) {
this.measurementService = measurementService;
}

@GetMapping(path = "/latest/{deviceId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getLatestMeasurementForDevice(final @PathVariable String deviceId) {
final Optional<MeasurementRepresentation> measurementRepresentationOptional = measurementService.getLatestMeasurement(deviceId);

if (!measurementRepresentationOptional.isPresent()) {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}

return new ResponseEntity<>(measurementRepresentationOptional.get().toJSON(), HttpStatus.OK);
}
}
51 changes: 51 additions & 0 deletions async/src/main/java/com/c8y/ms/templates/async/model/Device.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.c8y.ms.templates.async.model;

import javax.validation.constraints.NotEmpty;
import java.util.Objects;

public class Device {

@NotEmpty
private String name;

@NotEmpty
private String type;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Device device = (Device) o;
return Objects.equals(name, device.name) &&
Objects.equals(type, device.type);
}

@Override
public int hashCode() {
return Objects.hash(name, type);
}

@Override
public String toString() {
return "Device{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
'}';
}
}
Loading

0 comments on commit 033bcf0

Please sign in to comment.