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

org.yestech.lib.camel.TerracottaEndpoint Maven / Gradle / Ivy

Go to download

A collection of classes that can be used across yestech artifacts/components, but must not be dependant on any yestech component. Most of the code is utility type code. When more than a few classes are found to be in a package or the package start to handle more that a few reposibilities then a new independant component is created and the existing code in yeslib is ported to the new component.

The newest version!
/*
 * Copyright LGPL3
 * YES Technology Association
 * http://yestech.org
 *
 * http://www.opensource.org/licenses/lgpl-3.0.html
 */
package org.yestech.lib.camel;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.apache.camel.Component;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
import org.terracotta.message.pipe.Pipe;
import org.terracotta.modules.annotations.Root;

/**
 * An implementation of the asynchronous Pipe exchanges on a {@link org.terracotta.message.pipe.Pipe} within a CamelContext
 *
 * A component based on camel SEDA component.
 */
public class TerracottaEndpoint extends DefaultEndpoint {
    @Root
    private Pipe pipe;
    private int concurrentConsumers = 1;
    private Set producers = new CopyOnWriteArraySet();
    private Set consumers = new CopyOnWriteArraySet();

    public TerracottaEndpoint() {
    }

    public TerracottaEndpoint(String endpointUri, Component component, Pipe pipe) {
        this(endpointUri, component, pipe, 1);
    }

    public TerracottaEndpoint(String endpointUri, Component component, Pipe pipe, int concurrentConsumers) {
        super(endpointUri, component);
        this.pipe = pipe;
        this.concurrentConsumers = concurrentConsumers;
    }

    public TerracottaEndpoint(String endpointUri, Pipe pipe) {
        this(endpointUri, pipe, 1);
    }

    public TerracottaEndpoint(String endpointUri, Pipe pipe, int concurrentConsumers) {
        super(endpointUri);
        this.pipe = pipe;
        this.concurrentConsumers = concurrentConsumers;
    }

    public Producer createProducer() throws Exception {
        return new TerracottaProducer(this, getPipe());
    }

    public Consumer createConsumer(Processor processor) throws Exception {
        return new TerracottaConsumer(this, processor);
    }

    public Pipe getPipe() {
        return pipe;
    }

    public void setPipe(Pipe queue) {
        this.pipe = queue;
    }

    public void setConcurrentConsumers(int concurrentConsumers) {
        this.concurrentConsumers = concurrentConsumers;
    }

    public int getConcurrentConsumers() {
        return concurrentConsumers;
    }

    public boolean isSingleton() {
        return true;
    }

    /**
     * Returns the current active consumers on this endpoint
     */
    public Set getConsumers() {
        return new HashSet(consumers);
    }

    /**
     * Returns the current active producers on this endpoint
     */
    public Set getProducers() {
        return new HashSet(producers);
    }

    void onStarted(TerracottaProducer producer) {
        producers.add(producer);
    }

    void onStopped(TerracottaProducer producer) {
        producers.remove(producer);
    }

    void onStarted(TerracottaConsumer consumer) {
        consumers.add(consumer);
    }

    void onStopped(TerracottaConsumer consumer) {
        consumers.remove(consumer);
    }
}