All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.expediagroup.rhapsody.rabbitmq.message.DefaultRabbitMessageCreator Maven / Gradle / Ivy

There is a newer version: 0.5.16
Show newest version
/**
 * Copyright 2019 Expedia, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.expediagroup.rhapsody.rabbitmq.message;

import java.util.Objects;
import java.util.function.Function;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.MessageProperties;

public class DefaultRabbitMessageCreator extends AbstractRabbitMessageCreator {

    private final Function exchangeExtractor;

    private final Function routingKeyExtractor;

    public DefaultRabbitMessageCreator(AMQP.BasicProperties initialProperties, Function exchangeExtractor, Function routingKeyExtractor) {
        super(initialProperties);
        this.exchangeExtractor = exchangeExtractor;
        this.routingKeyExtractor = routingKeyExtractor;
    }

    public static  DefaultRabbitMessageCreator persistentBasicToDefaultExchange(String queue) {
        return new DefaultRabbitMessageCreator<>(MessageProperties.PERSISTENT_BASIC, t -> "", t -> queue);
    }

    public static  DefaultRabbitMessageCreator persistentBasicToDefaultExchange(Function queueExtractor) {
        return new DefaultRabbitMessageCreator<>(MessageProperties.PERSISTENT_BASIC, t -> "", queueExtractor);
    }

    public static  DefaultRabbitMessageCreator persistentBasicToExchange(String exchange, String routingKey) {
        return new DefaultRabbitMessageCreator<>(MessageProperties.PERSISTENT_BASIC, t -> exchange, t -> routingKey);
    }

    public static  DefaultRabbitMessageCreator persistentBasicToExchange(String exchange, Function routingKeyExtractor) {
        return new DefaultRabbitMessageCreator<>(MessageProperties.PERSISTENT_BASIC, t -> exchange, routingKeyExtractor);
    }

    public static  DefaultRabbitMessageCreator toDefaultExchange(AMQP.BasicProperties properties, String queue) {
        return new DefaultRabbitMessageCreator<>(properties, t -> "", t -> queue);
    }

    public static  DefaultRabbitMessageCreator toDefaultExchange(AMQP.BasicProperties properties, Function queueExtractor) {
        return new DefaultRabbitMessageCreator<>(properties, t -> "", queueExtractor);
    }

    public static  DefaultRabbitMessageCreator toExchange(AMQP.BasicProperties properties, String exchange, String routingKey) {
        return new DefaultRabbitMessageCreator<>(properties, t -> exchange, t -> routingKey);
    }

    public static  DefaultRabbitMessageCreator toExchange(AMQP.BasicProperties properties, String exchange, Function routingKeyExtractor) {
        return new DefaultRabbitMessageCreator<>(properties, t -> exchange, routingKeyExtractor);
    }

    @Override
    protected String extractExchange(T t) {
        return Objects.toString(exchangeExtractor.apply(t));
    }

    @Override
    protected String extractRoutingKey(T t) {
        return Objects.toString(routingKeyExtractor.apply(t));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy