Skip to content

Commit

Permalink
AER-2445 refactored out GSON library (#71)
Browse files Browse the repository at this point in the history
Also let taskmanager-parent extend from aerius root pom.
  • Loading branch information
Hilbrand authored Jan 8, 2024
1 parent 6f619fc commit 8776058
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 179 deletions.
87 changes: 9 additions & 78 deletions source/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
<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>

<groupId>nl.aerius</groupId>
<parent>
<groupId>nl.aerius</groupId>
<artifactId>aerius-root-pom</artifactId>
<version>1.0.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<artifactId>taskmanager-parent</artifactId>
<version>1.10.0-SNAPSHOT</version>
<packaging>pom</packaging>
Expand All @@ -45,34 +51,16 @@
<tag>HEAD</tag>
</scm>

<distributionManagement>
<repository>
<id>aerius-nexus</id>
<url>https://nexus.aerius.nl/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>aerius-nexus</id>
<url>https://nexus.aerius.nl/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aerius-tools.version>1.2.0</aerius-tools.version>
<spotless.version>2.28.0</spotless.version>
<dropwizard.metrics.version>4.2.13</dropwizard.metrics.version>
<slf4j.version>2.0.5</slf4j.version>
<junit.version>5.9.1</junit.version>
<mockito.version>4.9.0</mockito.version>

<sonar.projectKey>aerius_taskmanager</sonar.projectKey>
<sonar.moduleKey>${project.artifactId}</sonar.moduleKey>
<sonar.organization>aerius</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>

<aggregate.report.dir>taskmanager-report/target/site/jacoco-aggregate/jacoco.xml</aggregate.report.dir>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
Expand All @@ -87,13 +75,6 @@
<version>5.16.0</version>
</dependency>

<!-- json -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>

<!-- CLI -->
<dependency>
<groupId>commons-cli</groupId>
Expand Down Expand Up @@ -124,7 +105,7 @@
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- OpenTelemetry -->
<dependency>
<groupId>io.opentelemetry</groupId>
Expand Down Expand Up @@ -365,26 +346,6 @@
</dependencies>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.6.3,)</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -413,35 +374,5 @@
<module>taskmanager-report</module>
</modules>
</profile>

<profile>
<id>dependency-check</id>
<build>
<plugins>
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- If we ever want to fail on high CVSS scores, this is an example how -->
<!-- <failBuildOnCVSS>8</failBuildOnCVSS> -->
<!-- Generate all report formats -->
<format>ALL</format>
<!-- Don't use Nexus Analyzer -->
<centralAnalyzerEnabled>false</centralAnalyzerEnabled>
<!-- Am I the latest version? -->
<versionCheckEnabled>true</versionCheckEnabled>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
6 changes: 0 additions & 6 deletions source/taskmanager-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
<artifactId>amqp-client</artifactId>
</dependency>

<!-- json -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

<!-- Commons -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
6 changes: 6 additions & 0 deletions source/taskmanager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<artifactId>commons-cli</artifactId>
</dependency>

<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<!-- OpenTelemetry -->
<dependency>
<groupId>io.opentelemetry</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import nl.aerius.taskmanager.domain.PriorityTaskQueue;
import nl.aerius.taskmanager.domain.PriorityTaskSchedule;
Expand All @@ -40,10 +36,9 @@ class PriorityTaskSchedulerFileHandler implements SchedulerFileConfigurationHand

private static final Logger LOG = LoggerFactory.getLogger(PriorityTaskSchedulerFileHandler.class);

private static final String FILE_PREFIX = "priority-task-scheduler.";
private static final String ENV_PREFIX = "AERIUS_PRIORITY_TASK_SCHEDULER_";

private final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public PriorityTaskSchedule read(final File file) throws IOException {
Expand All @@ -53,26 +48,16 @@ public PriorityTaskSchedule read(final File file) throws IOException {
}

private PriorityTaskSchedule readFromFile(final File file) throws IOException {
try (final Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
return gson.fromJson(reader, PriorityTaskSchedule.class);
}
return objectMapper.readValue(file, PriorityTaskSchedule.class);
}

private PriorityTaskSchedule readFromEnvironment(final String workerQueueName) {
private PriorityTaskSchedule readFromEnvironment(final String workerQueueName) throws JsonProcessingException {
final String environmentKey = ENV_PREFIX + workerQueueName.toUpperCase(Locale.ROOT);
final String environmentValue = System.getenv(environmentKey);
if (environmentValue != null) {
LOG.info("Using configuration for worker queue {} from environment", workerQueueName);
return gson.fromJson(environmentValue, PriorityTaskSchedule.class);
return objectMapper.readValue(environmentValue, PriorityTaskSchedule.class);
}
return null;
}

@Override
public void write(final File path, final PriorityTaskSchedule priorityTaskSchedule) throws IOException {
final File targetFile = new File(path, FILE_PREFIX + priorityTaskSchedule.getWorkerQueueName() + ".json");
try (final Writer writer = Files.newBufferedWriter(targetFile.toPath(), StandardCharsets.UTF_8)) {
writer.write(gson.toJson(priorityTaskSchedule));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,4 @@ interface SchedulerFileConfigurationHandler<T extends TaskQueue, S extends TaskS
* @throws IOException
*/
S read(File file) throws IOException;

/**
* Writes the schedule to a file.
*
* @param path path to store the file
* @param schedule schedule to store
* @throws IOException
*/
void write(File path, S schedule) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public boolean updateTaskScheduler(final TaskSchedule<T> schedule) throws IOExce
}
final TaskScheduleBucket taskScheduleBucket = buckets.get(workerQueueName);

taskScheduleBucket.updateQueues(schedule.getTaskQueues(), schedule.isDurable());
taskScheduleBucket.updateQueues(schedule.getQueues(), schedule.isDurable());
return taskScheduleBucket.isRunning();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private S rewriteQueueNames(final File file) throws IOException {
final WorkerQueueType workerQueueType = new WorkerQueueType(schedule.getWorkerQueueName());

schedule.setWorkerQueueName(workerQueueType.getWorkerQueueName());
schedule.getTaskQueues().forEach(s -> s.setQueueName(workerQueueType.getTaskQueueName(s.getQueueName())));
schedule.getQueues().forEach(s -> s.setQueueName(workerQueueType.getTaskQueueName(s.getQueueName())));
return schedule;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,13 @@
*/
package nl.aerius.taskmanager.domain;

import java.io.Serializable;

import com.google.gson.annotations.Expose;

/**
* The configuration of a task, which is used by Task Consumer to retrieve the queue name
* and used by the scheduler to determine if a message should be handled or not.
*
*/
public class PriorityTaskQueue extends TaskQueue implements Serializable {

private static final long serialVersionUID = 7719329377305394882L;
public class PriorityTaskQueue extends TaskQueue {

@Expose
private int priority;
@Expose
private double maxCapacityUse;

/**
Expand All @@ -43,11 +34,10 @@ public PriorityTaskQueue() {

/**
* @param queueName The name of the queue the task corresponds to.
* @param description The description of this task queue.
* @param priority The priority this task should have.
* @param maxCapacityUse The maximum capacity this task can use of the total workers assigned. Should be a fraction.
*/
public PriorityTaskQueue(final String queueName, final String description, final int priority, final double maxCapacityUse) {
public PriorityTaskQueue(final String queueName, final int priority, final double maxCapacityUse) {
super(queueName);
this.priority = priority;
this.maxCapacityUse = maxCapacityUse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@
* The configuration of a TaskScheduler.
*/
public class PriorityTaskSchedule extends TaskSchedule<PriorityTaskQueue> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package nl.aerius.taskmanager.domain;

import com.google.gson.annotations.Expose;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

/**
* Base class for a single task queue configuration.
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
@JsonSubTypes({ @JsonSubTypes.Type(PriorityTaskQueue.class) })
public class TaskQueue {
@Expose
private String queueName;

protected TaskQueue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@
import java.util.ArrayList;
import java.util.List;

import com.google.gson.annotations.Expose;

/**
* Base class for a single schedule configuration.
* @param <T> specific task queue configuration class
*/
public class TaskSchedule<T extends TaskQueue> {

@Expose
private String workerQueueName;

@Expose
private Boolean durable;

@Expose
private List<T> queues = new ArrayList<>();

public String getWorkerQueueName() {
Expand All @@ -44,12 +39,12 @@ public void setWorkerQueueName(final String workerQueueName) {
this.workerQueueName = workerQueueName;
}

public List<T> getTaskQueues() {
public List<T> getQueues() {
return queues;
}

public void setTaskConfigurations(final List<T> taskConfigurations) {
this.queues = taskConfigurations;
public void setQueues(final List<T> queues) {
this.queues = queues;
}

public void setDurable(final boolean durable) {
Expand Down
Loading

0 comments on commit 8776058

Please sign in to comment.