Skip to content

Commit

Permalink
chore: Setup notification service and change Java to LTS 21 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkerKoc authored Nov 17, 2024
1 parent 27e5fdc commit d045412
Show file tree
Hide file tree
Showing 19 changed files with 802 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/application-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(22)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package de.tum.cit.aet.helios.syncing;

import io.nats.client.Connection;
import io.nats.client.JetStream;
import io.nats.client.Nats;
import io.nats.client.Options;
import io.nats.client.impl.NatsMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

@Service
public class NatsNotificationPublisherService {

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

private Connection natsConnection;
private JetStream jetStream;

@Value("${nats.enabled}")
private boolean isNatsEnabled;

@Value("${nats.server}")
private String natsServer;

@Value("${nats.auth.token}")
private String natsAuthToken;

@EventListener(ApplicationReadyEvent.class)
public void init() {
if (!isNatsEnabled) {
logger.info("NATS is disabled. Skipping initialization.");
return;
}

validateConfigurations();
Options options = buildNatsOptions();

while (true) {
try {
natsConnection = Nats.connect(options);
jetStream = natsConnection.jetStream();
logger.info("Connected to NATS at {}", natsServer);
// publishDummyNotification();
return;
} catch (IOException | InterruptedException e) {
logger.error("NATS connection error: {}", e.getMessage(), e);
}
}
}

private void validateConfigurations() {
if (natsServer == null || natsServer.trim().isEmpty()) {
throw new IllegalArgumentException("NATS server configuration is missing.");
}
}

private Options buildNatsOptions() {
return Options.builder()
.server(natsServer)
.token(natsAuthToken)
.connectionListener((conn, type) -> logger.info("Connection event - Server: {}, {}",
conn.getServerInfo().getPort(), type))
.maxReconnects(-1)
.reconnectWait(Duration.ofSeconds(2))
.build();
}

public void publishNotification(String subject, byte[] message) throws IOException, InterruptedException {
NatsMessage msg = NatsMessage.builder()
.subject("notification." + subject)
.data(message)
.build();
natsConnection.publish(msg);
logger.info("Published message to subject '{}'", "notification." + subject);
}

private void publishDummyNotification() {
String dummyMessage = "This is a dummy notification message.";
try {
publishNotification("dummy", dummyMessage.getBytes(StandardCharsets.UTF_8));
logger.info("Dummy notification message published successfully.");
} catch (IOException | InterruptedException e) {
logger.error("Failed to publish dummy notification message: {}", e.getMessage(), e);
}
}
}
5 changes: 5 additions & 0 deletions server/notification/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DATASOURCE_URL=jdbc:postgresql://127.0.0.1/helios
DATASOURCE_USERNAME=helios
DATASOURCE_PASSWORD=helios
NATS_SERVER=localhost:4222
NATS_AUTH_TOKEN=5760e8ae09adfb2756f9f8cd5cb2caa704cd3f549eaa9298be843ceb165185d815b81f90c680fa7f626b7cd63abf6ac9
3 changes: 3 additions & 0 deletions server/notification/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/gradlew text eol=lf
*.bat text eol=crlf
*.jar binary
38 changes: 38 additions & 0 deletions server/notification/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
.sdkmanrc
33 changes: 33 additions & 0 deletions server/notification/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.3.4'
id 'io.spring.dependency-management' version '1.1.6'
}

group = 'de.tum.cit.aet'
version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation 'io.nats:jnats:2.20.4'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
useJUnitPlatform()
}
Binary file not shown.
7 changes: 7 additions & 0 deletions server/notification/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit d045412

Please sign in to comment.