Search in sources :

Example 1 with OutgoingCloudEventMetadata

use of io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata in project smallrye-reactive-messaging by smallrye.

the class AmqpCreditBasedSender method send.

private Uni<Message<?>> send(AmqpSender sender, Message<?> msg, boolean durable, long ttl, String configuredAddress, boolean isAnonymousSender) {
    io.vertx.mutiny.amqp.AmqpMessage amqp;
    OutgoingCloudEventMetadata<?> ceMetadata = msg.getMetadata(OutgoingCloudEventMetadata.class).orElse(null);
    if (msg instanceof AmqpMessage) {
        amqp = ((AmqpMessage<?>) msg).getAmqpMessage();
    } else if (msg.getPayload() instanceof io.vertx.mutiny.amqp.AmqpMessage) {
        amqp = (io.vertx.mutiny.amqp.AmqpMessage) msg.getPayload();
    } else if (msg.getPayload() instanceof io.vertx.amqp.AmqpMessage) {
        amqp = new io.vertx.mutiny.amqp.AmqpMessage((io.vertx.amqp.AmqpMessage) msg.getPayload());
    } else if (msg.getPayload() instanceof org.apache.qpid.proton.message.Message) {
        org.apache.qpid.proton.message.Message message = (org.apache.qpid.proton.message.Message) msg.getPayload();
        AmqpMessageImpl vertxMessage = new AmqpMessageImpl(message);
        amqp = new io.vertx.mutiny.amqp.AmqpMessage(vertxMessage);
    } else {
        amqp = AmqpMessageConverter.convertToAmqpMessage(msg, durable, ttl);
    }
    if (writeCloudEvents && (ceMetadata != null || mandatoryCloudEventAttributeSet)) {
        // - or if the message does not contain this metadata, the type and source are configured on the channel
        if (writeAsBinaryCloudEvent) {
            amqp = AmqpCloudEventHelper.createBinaryCloudEventMessage(amqp, ceMetadata, this.configuration);
        } else {
            amqp = AmqpCloudEventHelper.createStructuredEventMessage(amqp, ceMetadata, this.configuration);
        }
    }
    String actualAddress = getActualAddress(msg, amqp, configuredAddress, isAnonymousSender);
    if (connector.getClients().isEmpty()) {
        log.messageNoSend(actualAddress);
        return Uni.createFrom().item(msg);
    }
    if (!actualAddress.equals(amqp.address())) {
        amqp.getDelegate().unwrap().setAddress(actualAddress);
    }
    createOutgoingTrace(msg, amqp);
    log.sendingMessageToAddress(actualAddress);
    return sender.sendWithAck(amqp).onFailure().retry().withBackOff(ofSeconds(1), ofSeconds(retryInterval)).atMost(retryAttempts).onItemOrFailure().transformToUni((success, failure) -> {
        if (failure != null) {
            return Uni.createFrom().completionStage(msg.nack(failure));
        } else {
            return Uni.createFrom().completionStage(msg.ack());
        }
    }).onItem().transform(x -> msg);
}
Also used : OutgoingCloudEventMetadata(io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata) Message(org.eclipse.microprofile.reactive.messaging.Message) AmqpMessageImpl(io.vertx.amqp.impl.AmqpMessageImpl)

Example 2 with OutgoingCloudEventMetadata

use of io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata 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);
}
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) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) ZonedDateTime(java.time.ZonedDateTime) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JsonObject(io.vertx.core.json.JsonObject) Record(io.smallrye.reactive.messaging.kafka.Record) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader)

Aggregations

OutgoingCloudEventMetadata (io.smallrye.reactive.messaging.ce.OutgoingCloudEventMetadata)2 Message (org.eclipse.microprofile.reactive.messaging.Message)2 CloudEventMetadata (io.smallrye.reactive.messaging.ce.CloudEventMetadata)1 DefaultCloudEventMetadataBuilder (io.smallrye.reactive.messaging.ce.DefaultCloudEventMetadataBuilder)1 BaseCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.BaseCloudEventMetadata)1 DefaultIncomingCloudEventMetadata (io.smallrye.reactive.messaging.ce.impl.DefaultIncomingCloudEventMetadata)1 IncomingKafkaCloudEventMetadata (io.smallrye.reactive.messaging.kafka.IncomingKafkaCloudEventMetadata)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 Record (io.smallrye.reactive.messaging.kafka.Record)1 IncomingKafkaRecordMetadata (io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata)1 OutgoingKafkaRecordMetadata (io.smallrye.reactive.messaging.kafka.api.OutgoingKafkaRecordMetadata)1 RuntimeKafkaSinkConfiguration (io.smallrye.reactive.messaging.kafka.impl.RuntimeKafkaSinkConfiguration)1 AmqpMessageImpl (io.vertx.amqp.impl.AmqpMessageImpl)1 JsonObject (io.vertx.core.json.JsonObject)1 Buffer (io.vertx.mutiny.core.buffer.Buffer)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Instant (java.time.Instant)1 ZonedDateTime (java.time.ZonedDateTime)1