Skip to content

Commit

Permalink
make it compatible for jackson. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek authored Jun 9, 2024
1 parent 337ac3d commit c33c5e7
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ private Collection<String> channelsForMessage(final Object message, final Collec
}

private String messageTypeId(final Class<?> messageType) {
return messageType.toString();
return messageType.getTypeName();
}
}
10 changes: 6 additions & 4 deletions common/src/main/java/net/infumia/pubsub/Envelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.util.UUID;

final class Envelope {
final UUID brokerId;
final UUID messageId;
UUID brokerId;
UUID messageId;
/**
* Can be {@code null}.
*/
final UUID respondsTo;
final byte[] messagePayload;
UUID respondsTo;
byte[] messagePayload;

Envelope() {}

Envelope(
final UUID brokerId,
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ redis = { module = "io.lettuce:lettuce-core", version = "6.3.2.RELEASE" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinserialization" }
kotlinx-serialization-protobuf = { module = "org.jetbrains.kotlinx:kotlinx-serialization-protobuf", version.ref = "kotlinserialization" }
jackson = { module = "com.fasterxml.jackson.core:jackson-databind", version = "2.17.1" }

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
5 changes: 5 additions & 0 deletions jackson/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
compileOnly(project(":common"))

compileOnly(libs.jackson)
}
1 change: 1 addition & 0 deletions jackson/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
artifact-id=pubsub-jackson
34 changes: 34 additions & 0 deletions jackson/src/main/java/net/infumia/pubsub/CodecJackson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.infumia.pubsub;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

final class CodecJackson<T> implements Codec<T> {
private final ObjectMapper mapper;
private final Class<T> type;

CodecJackson(final ObjectMapper mapper, final Class<T> type) {
this.mapper = mapper;
this.type = type;
}

@Override
public byte[] encode(final T t) {
try {
return this.mapper.writerFor(this.type).writeValueAsBytes(t);
} catch (final JsonProcessingException e) {
throw new RuntimeException("Failed to serialize " + this.type, e);
}
}

@Override
public T decode(final byte[] bytes) {
try {
return this.mapper.readerFor(this.type).readValue(bytes);
} catch (final IOException e) {
throw new RuntimeException("Failed to deserialize " + this.type, e);
}
}
}
24 changes: 24 additions & 0 deletions jackson/src/main/java/net/infumia/pubsub/CodecProviderJackson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.infumia.pubsub;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A {@link CodecProvider} implementation that uses Jackson for JSON serialization and deserialization.
*/
public final class CodecProviderJackson implements CodecProvider {
private final JacksonProvider provider;

/**
* Ctor.
*
* @param provider the {@link JacksonProvider} used to provide {@link ObjectMapper} instances.
*/
public CodecProviderJackson(final JacksonProvider provider) {
this.provider = provider;
}

@Override
public <T> Codec<T> provide(final Class<T> type) {
return new CodecJackson<>(this.provider.provide(), type);
}
}
15 changes: 15 additions & 0 deletions jackson/src/main/java/net/infumia/pubsub/JacksonProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.infumia.pubsub;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A provider interface for supplying {@link ObjectMapper} instances.
*/
public interface JacksonProvider {
/**
* Provides an {@link ObjectMapper} instance.
*
* @return an {@link ObjectMapper} instance.
*/
ObjectMapper provide();
}
5 changes: 4 additions & 1 deletion redis/src/main/java/net/infumia/pubsub/BrokerRedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public void message(final String pattern, final String channel, final String mes
BrokerRedis.this.callHandlers(channel, message);
}
});
this.subscribeConnection.sync().psubscribe(this.channelPrefixes.get().toArray(new String[0]));
final String[] channels = this.channelPrefixes.get().stream()
.map(s -> s + "*")
.toArray(String[]::new);
this.subscribeConnection.sync().psubscribe(channels);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {

rootProject.name = "pubsub"

include("common", "redis", "kotlin-extensions", "kotlin-protobuf")
include("common", "redis", "jackson", "kotlin-extensions", "kotlin-protobuf")

project(":kotlin-extensions").projectDir = file("kotlin/extensions")
project(":kotlin-protobuf").projectDir = file("kotlin/protobuf")

0 comments on commit c33c5e7

Please sign in to comment.