Skip to content

Commit

Permalink
Remove Spring Dependency from BaSyxClient Example (#494)
Browse files Browse the repository at this point in the history
* wip: Remove spring dependency from ClientExample

* Refactor container infra to support the changes in POM

* Extract DirectoryWatcher build process to own factory

* docs: Update examples README
  • Loading branch information
mateusmolina-iese authored Oct 17, 2024
1 parent 1d82ac6 commit 25b9333
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 111 deletions.
4 changes: 1 addition & 3 deletions examples/BaSyxClient/basyx-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
FROM maven:3.8.1-openjdk-17 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src src
RUN mvn clean package -DskipTests

FROM openjdk:17
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 8080
COPY --from=build /app/target/*-jar-with-dependencies.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
62 changes: 24 additions & 38 deletions examples/BaSyxClient/basyx-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,16 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>example-basyx-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>examples.basyxclient</name>
<description>BaSyx Client Example</description>
<properties>
<java.version>17</java.version>
<aas4j-version>1.0.2</aas4j-version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>aas4j-model</artifactId>
<version>${aas4j-version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
Expand All @@ -62,10 +30,28 @@
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>
org.eclipse.digitaltwin.basyx.examples.basyxclient.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@

package org.eclipse.digitaltwin.basyx.examples.basyxclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

@SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
import java.nio.file.Path;
public class Application {

static final Path DIR_TO_WATCH = Path.of("/ingest");
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
new Thread(DirectoryWatcherFactory.build(DIR_TO_WATCH)).start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.eclipse.digitaltwin.basyx.examples.basyxclient;

import java.nio.file.Path;
import java.util.List;

import org.eclipse.digitaltwin.basyx.aasenvironment.client.ConnectedAasManager;
import org.eclipse.digitaltwin.basyx.examples.basyxclient.configuration.BasyxSettings;
import org.eclipse.digitaltwin.basyx.examples.basyxclient.processing.BaSyxSyncService;
import org.eclipse.digitaltwin.basyx.examples.basyxclient.processing.BaSyxWarehouseService;
import org.eclipse.digitaltwin.basyx.examples.basyxclient.processing.DirectoryWatcher;
import org.eclipse.digitaltwin.basyx.examples.basyxclient.processing.EntryProcessor;

public final class DirectoryWatcherFactory {

private DirectoryWatcherFactory() {
}

public static DirectoryWatcher build(Path pathToWatch) {
BasyxSettings settings = buildBasyxSettingsFromEnv();
ConnectedAasManager connectedAasManager = buildConnectedAasManager(settings);
List<EntryProcessor> entryProcessors = buildEntryProcessors(connectedAasManager);

return new DirectoryWatcher(entryProcessors, pathToWatch);
}

static BasyxSettings buildBasyxSettingsFromEnv() {
String aasRegistryBaseUrl = System.getenv("AAS_REGISTRY_BASE_URL");
String aasRepositoryBaseUrl = System.getenv("AAS_REPOSITORY_BASE_URL");
String submodelRegistryBaseUrl = System.getenv("SUBMODEL_REGISTRY_BASE_URL");
String submodelRepositoryBaseUrl = System.getenv("SUBMODEL_REPOSITORY_BASE_URL");

return new BasyxSettings(aasRepositoryBaseUrl, submodelRepositoryBaseUrl, aasRegistryBaseUrl, submodelRegistryBaseUrl);
}

static ConnectedAasManager buildConnectedAasManager(BasyxSettings settings) {
return new ConnectedAasManager(settings.aasRegistryBaseUrl(), settings.aasRepositoryBaseUrl(), settings.submodelRegistryBaseUrl(), settings.submodelRepositoryBaseUrl());
}

static List<EntryProcessor> buildEntryProcessors(ConnectedAasManager connectedAasManager) {
BaSyxSyncService syncService = new BaSyxSyncService(connectedAasManager);
BaSyxWarehouseService warehouseService = new BaSyxWarehouseService(connectedAasManager);

return List.of(syncService, warehouseService);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@

package org.eclipse.digitaltwin.basyx.examples.basyxclient.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "basyx")
public record BasyxSettings(String aasRepositoryBaseUrl, String submodelRepositoryBaseUrl, String aasRegistryBaseUrl, String submodelRegistryBaseUrl) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
import org.eclipse.digitaltwin.basyx.submodelservice.client.ConnectedSubmodelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class BaSyxSyncService implements EntryProcessor {

private final Logger logger = LoggerFactory.getLogger(BaSyxSyncService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
import org.eclipse.digitaltwin.basyx.submodelservice.client.ConnectedSubmodelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class BaSyxWarehouseService implements EntryProcessor {

private final Logger logger = LoggerFactory.getLogger(BaSyxWarehouseService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,30 @@
import org.eclipse.digitaltwin.basyx.examples.basyxclient.model.MotorEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.opencsv.bean.CsvToBeanBuilder;

@Component
public class DirectoryWatcher implements CommandLineRunner {
public class DirectoryWatcher implements Runnable {

private final Logger logger = LoggerFactory.getLogger(DirectoryWatcher.class);

private final List<EntryProcessor> entryProcessors;

private final Path dirToWatch;

public DirectoryWatcher(List<EntryProcessor> entryProcessors, @Value("${erp.internal.watchpath:/ingest}") Path dirToWatch) {
public DirectoryWatcher(List<EntryProcessor> entryProcessors, Path dirToWatch) {
this.entryProcessors = entryProcessors;
this.dirToWatch = dirToWatch;
}

@Override
public void run(String... args) throws Exception {
watchDirectory(dirToWatch);
public void run() {
try {
watchDirectory(dirToWatch);
} catch (IOException | InterruptedException e) {
logger.error("Error watching directory", e);
Thread.currentThread().interrupt();
}
}

public void watchDirectory(Path dirToWatch) throws IOException, InterruptedException {
Expand Down Expand Up @@ -113,4 +114,5 @@ private void processAllEntries(List<MotorEntry> entries) {
}
}


}
4 changes: 4 additions & 0 deletions examples/BaSyxClient/config/basyx-client.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AAS_REGISTRY_BASE_URL=http://localhost:8082
SUBMODEL_REGISTRY_BASE_URL=http://localhost:8083
AAS_REPOSITORY_BASE_URL=http://localhost:8081
SUBMODEL_REPOSITORY_BASE_URL=http://localhost:8081
4 changes: 0 additions & 4 deletions examples/BaSyxClient/config/basyx-client.properties

This file was deleted.

4 changes: 1 addition & 3 deletions examples/BaSyxClient/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ services:
context: ./basyx-client
dockerfile: Dockerfile
container_name: basyx-client
env_file: ./config/basyx-client.env
volumes:
- ./config/basyx-client.properties:/app/application.properties
- ./ingest:/ingest
ports:
- '8087:8087'
network_mode: host
depends_on:
aas-env:
Expand Down
5 changes: 4 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ See the separate [Time Series example](https://github.com/eclipse-basyx/basyx-ap
See the separate [Operation Delegation Example](BaSyxOperationDelegation) for a comprehensive setup leveraging Operation Delegation.

## BaSyx Secure Setup Example
See the separate [Secure Setup Example](BaSyxSecured) for a comprehensive setup leveraging Keycloak.
See the separate [Secure Setup Example](BaSyxSecured) for a comprehensive setup leveraging Keycloak.

## BaSyx Client Example
See the separate [Client Example](BaSyxClient) for a comprehensive setup leveraging the BaSyx Java Client library.

0 comments on commit 25b9333

Please sign in to comment.