use of io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.
the class KafkaTopicInvokerTest method onEvent.
@Test
public void onEvent() {
ArgumentCaptor<Message<String>> captor = ArgumentCaptor.forClass(Message.class);
Emitter<String> emitter = mock(Emitter.class);
String event = "{\"key\": \"value\"}";
String topic = "myTestTopic";
ProcessorDTO processor = createProcessor();
KafkaTopicInvoker invoker = new KafkaTopicInvoker(emitter, processor, topic);
invoker.onEvent(event);
verify(emitter).send(captor.capture());
Message<String> sent = captor.getValue();
assertThat(sent.getPayload()).isEqualTo(event);
Metadata metadata = sent.getMetadata();
OutgoingKafkaRecordMetadata recordMetadata = metadata.get(OutgoingKafkaRecordMetadata.class).get();
assertThat(recordMetadata.getTopic()).isEqualTo(topic);
}
use of io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata in project smallrye-reactive-messaging by smallrye.
the class KafkaRecordTest method testCreationOfKafkaRecordWithEverything.
@Test
public void testCreationOfKafkaRecordWithEverything() {
Instant timestamp = Instant.now();
KafkaRecord<String, String> message = KafkaRecord.of("topic", "foo", "bar", timestamp, 2);
assertThat(message.getPayload()).isEqualTo("bar");
assertThat(message.getKey()).isEqualTo("foo");
assertThat(message.getTopic()).isEqualTo("topic");
assertThat(message.getHeaders()).isEmpty();
assertThat(message.getPartition()).isEqualTo(2);
assertThat(message.getTimestamp()).isEqualTo(timestamp);
OutgoingKafkaRecordMetadata<?> metadata = message.getMetadata(OutgoingKafkaRecordMetadata.class).orElseThrow(() -> new AssertionError("Metadata expected"));
assertThat(metadata.getPartition()).isEqualTo(2);
assertThat(metadata.getKey()).isEqualTo("foo");
assertThat(metadata.getTopic()).isEqualTo("topic");
assertThat(metadata.getTimestamp()).isEqualTo(timestamp);
assertThat(metadata.getHeaders()).isEmpty();
LegacyMetadataTestUtils.tempCompareLegacyAndApiMetadata(metadata, message);
}
use of io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata in project smallrye-reactive-messaging by smallrye.
the class KafkaCloudEventHelper method createStructuredRecord.
@SuppressWarnings("rawtypes")
public static ProducerRecord<?, ?> createStructuredRecord(Message<?> message, String topic, OutgoingKafkaRecordMetadata<?> outgoingMetadata, IncomingKafkaRecordMetadata<?, ?> incomingMetadata, OutgoingCloudEventMetadata<?> ceMetadata, RuntimeKafkaSinkConfiguration configuration) {
if (ceMetadata == null) {
ceMetadata = OutgoingCloudEventMetadata.builder().build();
}
Integer partition = getPartition(outgoingMetadata, configuration);
Object key = getKey(message, outgoingMetadata, ceMetadata, configuration);
Long timestamp = getTimestamp(outgoingMetadata);
List<Header> headers = getHeaders(outgoingMetadata, incomingMetadata, configuration);
String source = getSource(ceMetadata, configuration);
String type = getType(ceMetadata, configuration);
Optional<String> subject = getSubject(ceMetadata, configuration);
Optional<String> dataContentType = getDataContentType(ceMetadata, configuration);
Optional<URI> schema = getDataSchema(ceMetadata, configuration);
// if headers does not contain a "content-type" header add one
Optional<Header> contentType = headers.stream().filter(h -> h.key().equalsIgnoreCase(KAFKA_HEADER_CONTENT_TYPE)).findFirst();
if (!contentType.isPresent()) {
headers.add(new RecordHeader(KAFKA_HEADER_CONTENT_TYPE, STRUCTURED_CONTENT_TYPE.getBytes()));
}
// We need to build the JSON Object representing the Cloud Event
JsonObject json = new JsonObject();
json.put(CE_ATTRIBUTE_SPEC_VERSION, ceMetadata.getSpecVersion()).put(CE_ATTRIBUTE_TYPE, type).put(CE_ATTRIBUTE_SOURCE, source).put(CE_ATTRIBUTE_ID, ceMetadata.getId());
ZonedDateTime time = ceMetadata.getTimeStamp().orElse(null);
if (time != null) {
json.put(CE_ATTRIBUTE_TIME, time.toInstant());
} else if (configuration.getCloudEventsInsertTimestamp()) {
json.put(CE_ATTRIBUTE_TIME, Instant.now());
}
schema.ifPresent(s -> json.put(CE_ATTRIBUTE_DATA_SCHEMA, s));
dataContentType.ifPresent(s -> json.put(CE_ATTRIBUTE_DATA_CONTENT_TYPE, s));
subject.ifPresent(s -> json.put(CE_ATTRIBUTE_SUBJECT, s));
// Extensions
ceMetadata.getExtensions().forEach(json::put);
// Encode the payload to json
Object payload = message.getPayload();
if (payload instanceof Record) {
payload = ((Record) payload).value();
}
if (payload instanceof String) {
json.put("data", payload);
} else {
json.put("data", JsonObject.mapFrom(payload));
}
return new ProducerRecord<>(topic, partition, timestamp, key, json.encode(), headers);
}
Aggregations