Search in sources :

Example 6 with IncomingKafkaCloudEventMetadata

use of io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithCloudEventsTest method testReceivingBinaryCloudEventsWithSupportDisabled.

@SuppressWarnings("unchecked")
@Test
public void testReceivingBinaryCloudEventsWithSupportDisabled() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.deserializer", StringDeserializer.class.getName());
    config.put("channel-name", topic);
    config.put("cloud-events", false);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = new ArrayList<>();
    source.getStream().subscribe().with(messages::add);
    List<Header> headers = new ArrayList<>();
    headers.add(new RecordHeader("ce_specversion", CloudEventMetadata.CE_VERSION_1_0.getBytes()));
    headers.add(new RecordHeader("ce_type", "type".getBytes()));
    headers.add(new RecordHeader("ce_source", "test://test".getBytes()));
    headers.add(new RecordHeader("ce_id", "id".getBytes()));
    headers.add(new RecordHeader("ce_time", "2020-07-23T07:59:04Z".getBytes()));
    headers.add(new RecordHeader("content-type", "text/plain".getBytes()));
    headers.add(new RecordHeader("ce_subject", "foo".getBytes()));
    headers.add(new RecordHeader("ce_dataschema", "http://schema.io".getBytes()));
    headers.add(new RecordHeader("ce_ext", "bar".getBytes()));
    headers.add(new RecordHeader("some-header", "baz".getBytes()));
    companion.produceStrings().fromRecords(new ProducerRecord<>(topic, null, null, "key", "Hello World", headers));
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1);
    Message<?> message = messages.get(0);
    IncomingKafkaCloudEventMetadata<String, String> metadata = message.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNull();
    assertThat(message.getPayload()).isInstanceOf(String.class).isEqualTo("Hello World");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 7 with IncomingKafkaCloudEventMetadata

use of io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithCloudEventsTest method testReceivingStructuredCloudEventsWithJsonObjectDeserializer.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithJsonObjectDeserializer() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.deserializer", JsonObjectDeserializer.class.getName());
    config.put("channel-name", topic);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = new ArrayList<>();
    source.getStream().subscribe().with(messages::add);
    companion.produce(String.class, JsonObject.class).fromRecords(new ProducerRecord<>(topic, null, null, "key", new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("subject", "foo").put("data", new JsonObject().put("name", "neo")), Collections.singletonList(new RecordHeader("content-type", "application/cloudevents+json; charset=utf-8".getBytes()))));
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1);
    Message<?> message = messages.get(0);
    IncomingKafkaCloudEventMetadata<String, JsonObject> metadata = message.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getSubject()).hasValue("foo");
    assertThat(metadata.getTimeStamp()).isEmpty();
    assertThat(metadata.getDataContentType()).isEmpty();
    assertThat(metadata.getDataSchema()).isEmpty();
    assertThat(metadata.getData().getString("name")).isEqualTo("neo");
    // Extensions
    assertThat(metadata.getKey()).isEqualTo("key");
    // Rule 3.1 - partitionkey attribute
    assertThat(metadata.<String>getExtension("partitionkey")).hasValue("key");
    assertThat(metadata.getTopic()).isEqualTo(topic);
    assertThat(message.getPayload()).isInstanceOf(JsonObject.class);
    assertThat(((JsonObject) message.getPayload()).getString("name")).isEqualTo("neo");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JsonObject(io.vertx.core.json.JsonObject) JsonObjectDeserializer(io.vertx.kafka.client.serialization.JsonObjectDeserializer) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 8 with IncomingKafkaCloudEventMetadata

use of io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithCloudEventsTest method testReceivingBinaryCloudEvents.

@SuppressWarnings("unchecked")
@Test
public void testReceivingBinaryCloudEvents() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.deserializer", StringDeserializer.class.getName());
    config.put("channel-name", topic);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = new ArrayList<>();
    source.getStream().subscribe().with(messages::add);
    List<Header> headers = new ArrayList<>();
    headers.add(new RecordHeader("ce_specversion", CloudEventMetadata.CE_VERSION_1_0.getBytes()));
    headers.add(new RecordHeader("ce_type", "type".getBytes()));
    headers.add(new RecordHeader("ce_source", "test://test".getBytes()));
    headers.add(new RecordHeader("ce_id", "id".getBytes()));
    headers.add(new RecordHeader("ce_time", "2020-07-23T07:59:04Z".getBytes()));
    headers.add(new RecordHeader("content-type", "text/plain".getBytes()));
    headers.add(new RecordHeader("ce_subject", "foo".getBytes()));
    headers.add(new RecordHeader("ce_dataschema", "http://schema.io".getBytes()));
    headers.add(new RecordHeader("ce_ext", "bar".getBytes()));
    headers.add(new RecordHeader("some-header", "baz".getBytes()));
    companion.produceStrings().fromRecords(new ProducerRecord<>(topic, null, null, "key", "Hello World", headers));
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1);
    Message<?> message = messages.get(0);
    IncomingKafkaCloudEventMetadata<String, String> metadata = message.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNotNull();
    assertThat(metadata.getSpecVersion()).isEqualTo(CloudEventMetadata.CE_VERSION_1_0);
    assertThat(metadata.getType()).isEqualTo("type");
    assertThat(metadata.getId()).isEqualTo("id");
    assertThat(metadata.getSource()).isEqualTo(URI.create("test://test"));
    assertThat(metadata.getSubject()).hasValue("foo");
    assertThat(metadata.getDataSchema()).hasValue(URI.create("http://schema.io"));
    assertThat(metadata.getTimeStamp()).isNotEmpty();
    assertThat(metadata.getData()).isEqualTo("Hello World");
    // Rule 3.2.1 - the content-type must be mapped to the datacontenttype attribute
    assertThat(metadata.getDataContentType()).hasValue("text/plain");
    // Rule 3.2.3
    assertThat(metadata.getExtension("ext")).hasValue("bar");
    assertThat(metadata.getExtension("some-header")).isEmpty();
    // Extensions
    assertThat(metadata.getKey()).isEqualTo("key");
    // Rule 3.1 - partitionkey attribute
    assertThat(metadata.<String>getExtension("partitionkey")).hasValue("key");
    assertThat(metadata.getTopic()).isEqualTo(topic);
    assertThat(message.getPayload()).isInstanceOf(String.class).isEqualTo("Hello World");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 9 with IncomingKafkaCloudEventMetadata

use of io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaSourceWithCloudEventsTest method testReceivingStructuredCloudEventsWithoutMatchingContentTypeIsNotReadACloudEvent.

@SuppressWarnings("unchecked")
@Test
public void testReceivingStructuredCloudEventsWithoutMatchingContentTypeIsNotReadACloudEvent() {
    KafkaMapBasedConfig config = newCommonConfig();
    config.put("topic", topic);
    config.put("value.deserializer", StringDeserializer.class.getName());
    config.put("channel-name", topic);
    KafkaConnectorIncomingConfiguration ic = new KafkaConnectorIncomingConfiguration(config);
    source = new KafkaSource<>(vertx, UUID.randomUUID().toString(), ic, UnsatisfiedInstance.instance(), CountKafkaCdiEvents.noCdiEvents, UnsatisfiedInstance.instance(), -1);
    List<Message<?>> messages = new ArrayList<>();
    source.getStream().subscribe().with(messages::add);
    companion.produceStrings().fromRecords(new ProducerRecord<>(topic, null, null, "key", new JsonObject().put("specversion", CloudEventMetadata.CE_VERSION_1_0).put("type", "type").put("id", "id").put("source", "test://test").put("data", new JsonObject().put("name", "neo")).encode(), Collections.singletonList(new RecordHeader("content-type", "application/json; charset=utf-8".getBytes()))));
    await().atMost(2, TimeUnit.MINUTES).until(() -> messages.size() >= 1);
    Message<?> message = messages.get(0);
    IncomingKafkaCloudEventMetadata<String, JsonObject> metadata = message.getMetadata(IncomingKafkaCloudEventMetadata.class).orElse(null);
    assertThat(metadata).isNull();
    CloudEventMetadata<JsonObject> metadata2 = message.getMetadata(CloudEventMetadata.class).orElse(null);
    assertThat(metadata2).isNull();
    assertThat(message.getPayload()).isInstanceOf(String.class);
    JsonObject json = new JsonObject(message.getPayload().toString());
    assertThat(json.getString("id")).isEqualTo("id");
}
Also used : Message(org.eclipse.microprofile.reactive.messaging.Message) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) JsonObject(io.vertx.core.json.JsonObject) CloudEventMetadata(io.smallrye.reactive.messaging.ce.CloudEventMetadata) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) KafkaConnectorIncomingConfiguration(io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) KafkaMapBasedConfig(io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig) Test(org.junit.jupiter.api.Test)

Example 10 with IncomingKafkaCloudEventMetadata

use of io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata in project smallrye-reactive-messaging by smallrye.

the class KafkaCloudEventHelper method createFromBinaryCloudEvent.

public static <T, K> IncomingKafkaCloudEventMetadata<K, T> createFromBinaryCloudEvent(ConsumerRecord<?, T> record) {
    DefaultCloudEventMetadataBuilder<T> builder = new DefaultCloudEventMetadataBuilder<>();
    // Build a map containing all the headers
    // We remove the entry at each access to filter out extension attribute.
    Map<String, String> headers = new HashMap<>();
    record.headers().forEach(kh -> {
        String key = kh.key();
        // Rules 3.2.3 - Force UTF-8
        String value = new String(kh.value(), StandardCharsets.UTF_8);
        headers.put(key, value);
    });
    // Required
    builder.withSpecVersion(headers.remove(KAFKA_HEADER_FOR_SPEC_VERSION));
    builder.withId(headers.remove(KAFKA_HEADER_FOR_ID));
    String source = headers.remove(KAFKA_HEADER_FOR_SOURCE);
    if (source == null) {
        throw new IllegalArgumentException("The Kafka record must contain the " + KAFKA_HEADER_FOR_SOURCE + " header");
    }
    builder.withSource(URI.create(source));
    builder.withType(headers.remove(KAFKA_HEADER_FOR_TYPE));
    // Optional
    // Rules 3.2.1 - Set datacontenttype to the record's content type header
    String ct = headers.remove(KAFKA_HEADER_CONTENT_TYPE);
    if (ct != null) {
        builder.withDataContentType(ct);
    }
    String schema = headers.remove(KAFKA_HEADER_FOR_SCHEMA);
    if (schema != null) {
        builder.withDataSchema(URI.create(schema));
    }
    String subject = headers.remove(KAFKA_HEADER_FOR_SUBJECT);
    if (subject != null) {
        builder.withSubject(subject);
    }
    String time = headers.remove(KAFKA_HEADER_FOR_TIME);
    if (time != null) {
        ZonedDateTime parse = ZonedDateTime.parse(time, RFC3339_DATE_FORMAT);
        builder.withTimestamp(parse);
    }
    // Extensions
    if (record.key() != null) {
        builder.withExtension(CE_KAFKA_KEY, record.key());
    }
    builder.withExtension(CE_KAFKA_TOPIC, record.topic());
    headers.entrySet().stream().filter(entry -> entry.getKey().startsWith(CE_HEADER_PREFIX)).forEach(entry -> {
        String key = entry.getKey().substring(CE_HEADER_PREFIX.length());
        // Implementation choice: Extension attributes are stored as String.
        builder.withExtension(key, entry.getValue());
    });
    // Data
    builder.withData(record.value());
    BaseCloudEventMetadata<T> cloudEventMetadata = builder.build();
    return new DefaultIncomingKafkaCloudEventMetadata<>(new DefaultIncomingCloudEventMetadata<>(cloudEventMetadata));
}
Also used : DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) java.util(java.util) Record(io.smallrye.reactive.messaging.kafka.Record) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ZonedDateTime(java.time.ZonedDateTime) Headers(org.apache.kafka.common.header.Headers) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) CE_KAFKA_TOPIC(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_TOPIC) IncomingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata) RuntimeKafkaSinkConfiguration(io.smallrye.reactive.messaging.kafka.impl.RuntimeKafkaSinkConfiguration) CloudEventMetadata(io.smallrye.reactive.messaging.ce.CloudEventMetadata) DefaultIncomingCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata) BaseCloudEventMetadata(io.smallrye.reactive.messaging.ce.impl.BaseCloudEventMetadata) CE_KAFKA_KEY(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_KEY) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) IncomingKafkaCloudEventMetadata(io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata) Instant(java.time.Instant) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Message(org.eclipse.microprofile.reactive.messaging.Message) OutgoingCloudEventMetadata(io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata) OutgoingKafkaRecordMetadata(io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata) Buffer(io.vertx.mutiny.core.buffer.Buffer) Header(org.apache.kafka.common.header.Header) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) DateTimeFormatter(java.time.format.DateTimeFormatter) ZonedDateTime(java.time.ZonedDateTime) DefaultCloudEventMetadataBuilder(io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder)

Aggregations

IncomingKafkaCloudEventMetadata (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata)17 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)17 ArrayList (java.util.ArrayList)16 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)16 Test (org.junit.jupiter.api.Test)16 Message (org.eclipse.microprofile.reactive.messaging.Message)15 KafkaConnectorIncomingConfiguration (io.smallrye.reactive.messaging.kafka.KafkaConnectorIncomingConfiguration)14 KafkaMapBasedConfig (io.smallrye.reactive.messaging.kafka.base.KafkaMapBasedConfig)14 JsonObject (io.vertx.core.json.JsonObject)12 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)9 Header (org.apache.kafka.common.header.Header)8 JsonObjectDeserializer (io.vertx.kafka.client.serialization.JsonObjectDeserializer)4 CloudEventMetadata (io.smallrye.reactive.messaging.ce.CloudEventMetadata)2 KafkaRecord (io.smallrye.reactive.messaging.kafka.KafkaRecord)2 DefaultCloudEventMetadataBuilder (io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder)1 OutgoingCloudEventMetadata (io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata)1 BaseCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.BaseCloudEventMetadata)1 DefaultIncomingCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata)1 CE_KAFKA_KEY (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_KEY)1 CE_KAFKA_TOPIC (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata.CE_KAFKA_TOPIC)1