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

com.geneea.celery.brokers.CeleryBrokers Maven / Gradle / Ivy

Go to download

Java library for interfacing with http://celeryproject.org including code for both task submissions and task execution.

There is a newer version: 1.10.4
Show newest version
package com.geneea.celery.brokers;


import com.google.common.collect.ImmutableSet;
import com.geneea.celery.UnsupportedProtocolException;
import com.geneea.celery.spi.Broker;
import com.geneea.celery.spi.BrokerFactory;

import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;

/**
 * Internal utility to get the right {@link Broker} instance from a URI.
 */
public class CeleryBrokers {

    /**
     * Create a new broker.
     *
     * @param uri connection to broker
     * @param executor for background tasks
     * @return new broker instance
     */
    public static Broker createBroker(String uri, ExecutorService executor) {

        URI parsedUri = URI.create(uri);
        ImmutableSet.Builder knownProtocols = ImmutableSet.builder();

        for (BrokerFactory factory: ServiceLoader.load(BrokerFactory.class)) {
            Collection factoryProtocols = factory.getProtocols();
            knownProtocols.addAll(factoryProtocols);

            if (factoryProtocols.contains(parsedUri.getScheme())) {
                try {
                    return factory.createBroker(parsedUri, executor);
                } catch (IOException | TimeoutException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        throw new UnsupportedProtocolException(parsedUri.getScheme(), knownProtocols.build());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy