Search in sources :

Example 1 with RabbitMQMessage

use of io.vertx.mutiny.rabbitmq.RabbitMQMessage in project smallrye-reactive-messaging by smallrye.

the class RabbitMQMessageConverter method convert.

/**
 * Converts the supplied {@link Message} to an {@link OutgoingRabbitMQMessage}.
 *
 * @param message the source message
 * @param exchange the destination exchange
 * @param defaultRoutingKey the fallback routing key to use
 * @param isTracingEnabled whether tracing is enabled
 * @param attributeHeaders a list (possibly empty) of message header names whose values should be
 *        included as span attributes
 * @return an {@link OutgoingRabbitMQMessage}
 */
public static OutgoingRabbitMQMessage convert(final Message<?> message, final String exchange, final String defaultRoutingKey, final Optional<Long> defaultTtl, final boolean isTracingEnabled, final List<String> attributeHeaders) {
    final Optional<io.vertx.mutiny.rabbitmq.RabbitMQMessage> rabbitMQMessage = getRabbitMQMessage(message);
    final String routingKey = getRoutingKey(message).orElse(defaultRoutingKey);
    // Figure out the body and properties
    Buffer body;
    BasicProperties properties;
    if (rabbitMQMessage.isPresent()) {
        // If we already have a RabbitMQMessage present, use it as the basis for the outgoing one
        body = rabbitMQMessage.get().body();
        final BasicProperties sourceProperties = rabbitMQMessage.get().properties();
        // Make a copy of the source headers as the original is probably immutable
        final Map<String, Object> sourceHeaders = new HashMap<>(sourceProperties.getHeaders());
        if (isTracingEnabled) {
            // Create a new span for the outbound message and record updated tracing information in
            // the headers; this has to be done before we build the properties below
            TracingUtils.createOutgoingTrace(message, sourceHeaders, exchange, routingKey, attributeHeaders);
        }
        // Reconstruct the properties from the source, except with the (possibly) modified headers;
        // only override the existing expiration if not already set and a non-negative default TTL
        // has been specified.
        final String expiration = (null != sourceProperties.getExpiration()) ? sourceProperties.getExpiration() : defaultTtl.map(String::valueOf).orElse(null);
        // If not already specified, figure out the content type from the message payload
        final String contentType = (sourceProperties.getContentType() != null) ? sourceProperties.getContentType() : getDefaultContentTypeForPayload(message.getPayload());
        properties = new AMQP.BasicProperties.Builder().contentType(contentType).contentEncoding(sourceProperties.getContentEncoding()).headers(sourceHeaders).deliveryMode(sourceProperties.getDeliveryMode()).priority(sourceProperties.getPriority()).correlationId(sourceProperties.getCorrelationId()).replyTo(sourceProperties.getReplyTo()).expiration(expiration).messageId(sourceProperties.getMessageId()).timestamp(sourceProperties.getTimestamp()).type(sourceProperties.getType()).userId(sourceProperties.getUserId()).appId(sourceProperties.getAppId()).build();
    } else {
        // Getting here means we have to work a little harder
        final String defaultContentType = getDefaultContentTypeForPayload(message.getPayload());
        body = getBodyFromPayload(message.getPayload());
        final OutgoingRabbitMQMetadata metadata = message.getMetadata(OutgoingRabbitMQMetadata.class).orElse(new OutgoingRabbitMQMetadata.Builder().withContentType(defaultContentType).withExpiration(defaultTtl.map(String::valueOf).orElse(null)).build());
        if (isTracingEnabled) {
            // Create a new span for the outbound message and record updated tracing information in
            // the message headers; this has to be done before we build the properties below
            TracingUtils.createOutgoingTrace(message, metadata.getHeaders(), exchange, routingKey, attributeHeaders);
        }
        final Date timestamp = (metadata.getTimestamp() != null) ? Date.from(metadata.getTimestamp().toInstant()) : null;
        // If not already specified, use the default content type for the message payload
        final String contentType = (metadata.getContentType() != null) ? metadata.getContentType() : defaultContentType;
        properties = new AMQP.BasicProperties.Builder().contentType(contentType).contentEncoding(metadata.getContentEncoding()).headers(metadata.getHeaders()).deliveryMode(metadata.getDeliveryMode()).priority(metadata.getPriority()).correlationId(metadata.getCorrelationId()).replyTo(metadata.getReplyTo()).expiration(metadata.getExpiration()).messageId(metadata.getMessageId()).timestamp(timestamp).type(metadata.getType()).userId(metadata.getUserId()).appId(metadata.getAppId()).clusterId(metadata.getClusterId()).build();
    }
    return new OutgoingRabbitMQMessage(routingKey, body, properties);
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) BasicProperties(com.rabbitmq.client.BasicProperties) HashMap(java.util.HashMap) RabbitMQMessage(io.vertx.mutiny.rabbitmq.RabbitMQMessage) Date(java.util.Date) AMQP(com.rabbitmq.client.AMQP) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

AMQP (com.rabbitmq.client.AMQP)1 BasicProperties (com.rabbitmq.client.BasicProperties)1 JsonObject (io.vertx.core.json.JsonObject)1 Buffer (io.vertx.mutiny.core.buffer.Buffer)1 RabbitMQMessage (io.vertx.mutiny.rabbitmq.RabbitMQMessage)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1