.
This commit is contained in:
parent
e4fd6a8c5f
commit
d3e20606d9
10 changed files with 130 additions and 37 deletions
50
core-lib/build.gradle.kts
Normal file
50
core-lib/build.gradle.kts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
val scrowToken = providers.gradleProperty("scrowMavenToken")
|
||||
.orElse(providers.environmentVariable("SCROW_MAVEN_TOKEN")).get()
|
||||
|
||||
plugins {
|
||||
id("java")
|
||||
id("java-library")
|
||||
id("maven-publish")
|
||||
}
|
||||
|
||||
group = "de.kentoj.scrow"
|
||||
version = "0.1"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// https://mvnrepository.com/artifact/io.nats/jnats
|
||||
implementation("io.nats:jnats:2.25.2")
|
||||
api("io.nats:jnats:2.25.2")
|
||||
|
||||
// https://mvnrepository.com/artifact/org.mongodb/bson
|
||||
implementation("org.mongodb:bson:5.8.0")
|
||||
api("org.mongodb:bson:5.8.0")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("CoreLib") {
|
||||
groupId = project.group.toString()
|
||||
artifactId = project.name
|
||||
version = project.version.toString()
|
||||
from(components["java"])
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name = "scrow"
|
||||
url = uri("http://forge.kentoj.de/api/packages/scrow/maven")
|
||||
isAllowInsecureProtocol = true
|
||||
credentials(HttpHeaderCredentials::class) {
|
||||
name = "Authorization"
|
||||
value = "token $scrowToken"
|
||||
}
|
||||
authentication {
|
||||
create<HttpHeaderAuthentication>("header")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package de.kentoj.scrowlib;
|
||||
|
||||
import io.nats.client.Message;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bson.Document;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class DocumentRepository {
|
||||
|
||||
public static Document fromMessage(Message message) {
|
||||
return Document.parse(new String(message.getData()));
|
||||
}
|
||||
|
||||
public static byte[] toBytes(Document document) {
|
||||
return document.toJson().getBytes();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package de.kentoj.scrowlib;
|
||||
|
||||
import io.nats.client.Connection;
|
||||
import io.nats.client.Dispatcher;
|
||||
import io.nats.client.Message;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public class NatsRepository {
|
||||
|
||||
private final Connection nats;
|
||||
|
||||
private final Dispatcher dispatcher;
|
||||
|
||||
public NatsRepository(Connection nats) {
|
||||
this.nats = nats;
|
||||
this.dispatcher = nats.createDispatcher();
|
||||
}
|
||||
|
||||
public Mono<@NotNull Void> publish(String subject, byte[] data) {
|
||||
return Mono.fromRunnable(() -> nats.publish(subject, data));
|
||||
}
|
||||
|
||||
public Flux<@NotNull Message> subscribe(String subject) {
|
||||
return Flux.create(sink -> {
|
||||
var handler = dispatcher.subscribe(subject, msg -> {
|
||||
try {
|
||||
sink.next(msg);
|
||||
} catch (Throwable e) {
|
||||
sink.error(e);
|
||||
}
|
||||
});
|
||||
|
||||
sink.onCancel(handler::unsubscribe);
|
||||
sink.onDispose(handler::unsubscribe);
|
||||
});
|
||||
}
|
||||
|
||||
public Mono<@NotNull Message> request(String subject, byte[] data) {
|
||||
return Mono.fromFuture(nats.request(subject, data));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue