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

com.geneea.celery.backends.rabbit.RabbitBackendFactory 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.backends.rabbit;

import com.google.common.collect.ImmutableSet;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.apache.http.client.utils.URIBuilder;
import org.kohsuke.MetaInfServices;
import com.geneea.celery.spi.Backend;
import com.geneea.celery.spi.BackendFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;

@MetaInfServices(BackendFactory.class)
public class RabbitBackendFactory implements BackendFactory {
    @Override
    public Set getProtocols() {
        return ImmutableSet.of("rpc");
    }

    @Override
    public Backend createBackend(URI uri, ExecutorService executor) throws IOException, TimeoutException {
        // Replace rpc:// -> amqp:// to be consistent with the Python API. The Python API uses rpc:// to designate AMQP
        // used in the manner of one return queue per client as opposed to one queue per returned message (the original
        // amqp:// protocol).
        //
        // The underlying RabbitMQ library wouldn't understand the rpc scheme so we construct the URI it can understand.
        URI correctSchemeUri;
        try {
            assert "rpc".equals(uri.getScheme());
            correctSchemeUri = new URIBuilder(uri).setScheme("amqp").build();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        ConnectionFactory factory = new ConnectionFactory();
        try {
            factory.setUri(correctSchemeUri);
        } catch (NoSuchAlgorithmException | KeyManagementException | URISyntaxException e) {
            throw new IOException(e);
        }

        Connection connection = factory.newConnection(executor);
        return new RabbitBackend(connection.createChannel());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy