Skip to content
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

chore: Setup notification service and change Java to LTS 21 #29

Merged
merged 7 commits into from
Nov 17, 2024
Merged
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
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.
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