-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Setup notification service and change Java to LTS 21 (#29)
- Loading branch information
Showing
19 changed files
with
802 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...-server/src/main/java/de/tum/cit/aet/helios/syncing/NatsNotificationPublisherService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/gradlew text eol=lf | ||
*.bat text eol=crlf | ||
*.jar binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.