-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4739 from geoand/kafka-conditional-jsonb
Tighten integration between Kafka client and jsonb - Add Jackson support
- Loading branch information
Showing
18 changed files
with
285 additions
and
21 deletions.
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
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
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
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
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
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
24 changes: 24 additions & 0 deletions
24
...fka-client/runtime/src/main/java/io/quarkus/kafka/client/serialization/JsonbProducer.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,24 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
|
||
final class JsonbProducer { | ||
|
||
private JsonbProducer() { | ||
} | ||
|
||
// Try to get JSON-B from Arc but fallback to regular JSON-B creation | ||
// The fallback could be used for example in unit tests where Arc has not been initialized | ||
static Jsonb get() { | ||
Jsonb jsonb = null; | ||
ArcContainer container = Arc.container(); | ||
if (container != null) { | ||
jsonb = container.instance(Jsonb.class).get(); | ||
} | ||
return jsonb != null ? jsonb : JsonbBuilder.create(); | ||
} | ||
} |
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
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
46 changes: 46 additions & 0 deletions
46
...runtime/src/main/java/io/quarkus/kafka/client/serialization/ObjectMapperDeserializer.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,46 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.serialization.Deserializer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class ObjectMapperDeserializer<T> implements Deserializer<T> { | ||
|
||
private final Class<T> type; | ||
private final ObjectMapper objectMapper; | ||
|
||
public ObjectMapperDeserializer(Class<T> type) { | ||
this(type, ObjectMapperProducer.get()); | ||
} | ||
|
||
public ObjectMapperDeserializer(Class<T> type, ObjectMapper objectMapper) { | ||
this.type = type; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs, boolean isKey) { | ||
} | ||
|
||
@Override | ||
public T deserialize(String topic, byte[] data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
|
||
try (InputStream is = new ByteArrayInputStream(data)) { | ||
return objectMapper.readValue(is, type); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ent/runtime/src/main/java/io/quarkus/kafka/client/serialization/ObjectMapperProducer.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,23 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.arc.ArcContainer; | ||
|
||
final class ObjectMapperProducer { | ||
|
||
private ObjectMapperProducer() { | ||
} | ||
|
||
// Try to get JSON-B from Arc but fallback to regular ObjectMapper creation | ||
// The fallback could be used for example in unit tests where Arc has not been initialized | ||
static ObjectMapper get() { | ||
ObjectMapper objectMapper = null; | ||
ArcContainer container = Arc.container(); | ||
if (container != null) { | ||
objectMapper = container.instance(ObjectMapper.class).get(); | ||
} | ||
return objectMapper != null ? objectMapper : new ObjectMapper(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...client/runtime/src/main/java/io/quarkus/kafka/client/serialization/ObjectMapperSerde.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,48 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.apache.kafka.common.serialization.Serde; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* A {@link Serde} that (de-)serializes JSON using Jackson's ObjectMapper. | ||
*/ | ||
public class ObjectMapperSerde<T> implements Serde<T> { | ||
|
||
private final ObjectMapperSerializer<T> serializer; | ||
private final ObjectMapperDeserializer<T> deserializer; | ||
|
||
public ObjectMapperSerde(Class<T> type) { | ||
this(type, ObjectMapperProducer.get()); | ||
} | ||
|
||
public ObjectMapperSerde(Class<T> type, ObjectMapper objectMapper) { | ||
this.serializer = new ObjectMapperSerializer<T>(objectMapper); | ||
this.deserializer = new ObjectMapperDeserializer<T>(type, objectMapper); | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs, boolean isKey) { | ||
|
||
} | ||
|
||
@Override | ||
public void close() { | ||
serializer.close(); | ||
deserializer.close(); | ||
} | ||
|
||
@Override | ||
public Serializer<T> serializer() { | ||
return serializer; | ||
} | ||
|
||
@Override | ||
public Deserializer<T> deserializer() { | ||
return deserializer; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...t/runtime/src/main/java/io/quarkus/kafka/client/serialization/ObjectMapperSerializer.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,44 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.kafka.common.serialization.Deserializer; | ||
import org.apache.kafka.common.serialization.Serializer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* A {@link Deserializer} that deserializes JSON using Jackson's ObjectMapper. | ||
*/ | ||
public class ObjectMapperSerializer<T> implements Serializer<T> { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public ObjectMapperSerializer() { | ||
this(ObjectMapperProducer.get()); | ||
} | ||
|
||
public ObjectMapperSerializer(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs, boolean isKey) { | ||
} | ||
|
||
@Override | ||
public byte[] serialize(String topic, T data) { | ||
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { | ||
objectMapper.writeValue(output, data); | ||
return output.toByteArray(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...fka-client/runtime/src/test/java/io/quarkus/kafka/client/serde/ObjectMapperSerdeTest.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,48 @@ | ||
package io.quarkus.kafka.client.serde; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.kafka.client.serialization.ObjectMapperSerde; | ||
|
||
public class ObjectMapperSerdeTest { | ||
|
||
@Test | ||
public void shouldSerializeAndDeserializeEntity() { | ||
MyEntity entity = new MyEntity(); | ||
entity.id = 42L; | ||
entity.name = "Bob"; | ||
|
||
try (ObjectMapperSerde<MyEntity> serde = new ObjectMapperSerde<>(MyEntity.class)) { | ||
byte[] serialized = serde.serializer().serialize("my-topic", entity); | ||
MyEntity deserialized = serde.deserializer().deserialize("my-topic", serialized); | ||
|
||
assertThat(deserialized.id).isEqualTo(42L); | ||
assertThat(deserialized.name).isEqualTo("Bob"); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldSerializeAndDeserializeEntityWithGivenObjectMapper() throws Exception { | ||
MyEntity entity = new MyEntity(); | ||
entity.id = 42L; | ||
entity.name = "Bob"; | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try (ObjectMapperSerde<MyEntity> serde = new ObjectMapperSerde<>(MyEntity.class, objectMapper)) { | ||
byte[] serialized = serde.serializer().serialize("my-topic", entity); | ||
MyEntity deserialized = serde.deserializer().deserialize("my-topic", serialized); | ||
|
||
assertThat(deserialized.id).isEqualTo(42L); | ||
assertThat(deserialized.name).isEqualTo("Bob"); | ||
} | ||
} | ||
|
||
public static class MyEntity { | ||
public long id; | ||
public String name; | ||
} | ||
} |
Oops, something went wrong.