com.geneea.celery.brokers.CeleryBrokers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of celery-java Show documentation
Show all versions of celery-java Show documentation
Java library for interfacing with http://celeryproject.org including code for both task submissions and task
execution.
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