LibraryLink ToToggle FramesPrintFeedback

Implementing the Endpoint Interface

The following alternative endpoint implementation patterns are supported:

If your custom endpoint conforms to the event-driven pattern (see Consumer Patterns and Threading), it is implemented by extending the abstract class, org.apache.camel.impl.DefaultEndpoint, as shown in Example 6.2.

Example 6.2. Implementing DefaultEndpoint

import java.util.Map;
import java.util.concurrent.BlockingQueue;

import org.apache.camel.Component;
import org.apache.camel.Consumer;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;

public class CustomEndpoint extends DefaultEndpoint<CustomExchange> { 1

    public CustomEndpoint(String endpointUri, Component component) { 2
        super(endpointUri, component);
        // Do any other initialization...
    }

    public Producer createProducer() throws Exception { 3
        return new CustomProducer(this);
    }

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

    public boolean isSingleton() { 
        return true;
    }

    // Implement the following two methods, only if you need a custom exchange class.
    //
    public CustomExchange createExchange() { 5
        return new CustomExchange(getCamelContext(), getExchangePattern());
    }

    public CustomExchange createExchange(ExchangePattern pattern) {
        return new CustomExchange(getCamelContext(), pattern);
    }
}

1

Implement an event-driven custom endpoint, CustomEndpoint, by extending the DefaultEndpoint class.

2

You must have at least one constructor that takes the endpoint URI, endpointUri, and the parent component reference, component, as arguments.

3

Implement the createProducer() factory method to create producer endpoints.

4

Implement the createConsumer() factory method to create event-driven consumer instances.

[Important]Important

Do not override the createPollingConsumer() method.

5

If you intend to customize the exchange implementation, you should override the createExchange() and the createExchange(ExchangePattern) methods, to ensure that the correct exchange type is created. If you do not override these methods, the implementations inherited from DefaultEndpoint will create a DefaultExchange instance.

The DefaultEndpoint class provides default implementations of the following methods, which you might find useful when writing your custom endpoint code:

If your custom endpoint conforms to the scheduled poll pattern (see Consumer Patterns and Threading) it is implemented by inheriting from the abstract class, org.apache.camel.impl.ScheduledPollEndpoint, as shown in Example 6.3.

Example 6.3. ScheduledPollEndpoint Implementation

import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Message;
import org.apache.camel.impl.ScheduledPollEndpoint;

public class CustomEndpoint extends ScheduledPollEndpoint<CustomExchange> { 1

    protected CustomEndpoint(String endpointUri, CustomComponent component) { 2
        super(endpointUri, component);
        // Do any other initialization...
    }

    public Producer<CustomExchange> createProducer() throws Exception { 3
        Producer<CustomExchange> result = new CustomProducer(this);
        return result;
    }

    public Consumer<CustomExchange> createConsumer(Processor processor) throws Exception { 4
        Consumer<CustomExchange> result = new CustomConsumer(this, processor);
        configureConsumer(result); 5
        return result;
    }

    public boolean isSingleton() {
        return true;
    }

    // Implement the following two methods, only if you need a custom exchange class.
    //
    public CustomExchange createExchange() { 6
        return new CustomExchange(...);
    }

    public CustomExchange createExchange(ExchangePattern pattern) {
        return new CustomExchange(getCamelContext(), pattern);
    }
}

1

Implement a scheduled poll custom endpoint, CustomEndpoint, by extending the ScheduledPollEndpoint class.

2

You must to have at least one constructor that takes the endpoint URI, endpointUri, and the parent component reference, component, as arguments.

3

Implement the createProducer() factory method to create a producer endpoint.

4

Implement the createConsumer() factory method to create a scheduled poll consumer instance.

[Important]Important

Do not override the createPollingConsumer() method.

5

The configureConsumer() method, defined in the ScheduledPollEndpoint base class, is responsible for injecting consumer query options into the consumer. See Consumer parameter injection.

6

If you intend to customize the exchange implementation, you should override the createExchange() and the createExchange(ExchangePattern) methods, to ensure that the correct exchange type is created. If you do not override these methods, the implementations inherited from DefaultEndpoint will create a DefaultExchange instance.

If your custom endpoint conforms to the polling consumer pattern (see Consumer Patterns and Threading), it is implemented by inheriting from the abstract class, org.apache.camel.impl.DefaultPollingEndpoint, as shown in Example 6.4.


Because this CustomEndpoint class is a polling endpoint, you must implement the createPollingConsumer() method instead of the createConsumer() method. The consumer instance returned from createPollingConsumer() must inherit from the PollingConsumer interface. For details of how to implement a polling consumer, see Polling consumer implementation.

Apart from the implementation of the createPollingConsumer() method, the steps for implementing a DefaultPollingEndpoint are similar to the steps for implementing a ScheduledPollEndpoint. See Example 6.3 for details.

If you want to expose the list of exchange instances that are pending in the current endpoint, you can implement the org.apache.camel.spi.BrowsableEndpoint interface, as shown in Example 6.5. It makes sense to implement this interface if the endpoint performs some sort of buffering of incoming events. For example, the FUSE Mediation Router SEDA endpoint implements the BrowsableEndpoint interface—see Example 6.6.


Example 6.6 shows the implementation of SedaEndpoint, which is taken from the FUSE Mediation Router SEDA component implementation. The SEDA endpoint is an example of an event-driven endpoint. Incoming events are stored in a FIFO queue (an instance of java.util.concurrent.BlockingQueue) and a SEDA consumer starts up a thread to read and process the events. The events themselves are represented by org.apache.camel.Exchange objects.

Example 6.6. SedaEndpoint Implementation

package org.apache.camel.component.seda;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import org.apache.camel.Component;
import org.apache.camel.Consumer;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.spi.BrowsableEndpoint;

public class SedaEndpoint extends DefaultEndpoint<Exchange> implements BrowsableEndpoint<Exchange> { 1
    private BlockingQueue<Exchange> queue;

    public SedaEndpoint(String endpointUri, Component component, BlockingQueue<Exchange> queue) { 2
        super(endpointUri, component);
        this.queue = queue;
    }

    public SedaEndpoint(String uri, SedaComponent component, Map parameters) { 3
        this(uri, component, component.createQueue(uri, parameters));
    }

    public Producer createProducer() throws Exception { 4
        return new CollectionProducer(this, getQueue());
    }

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

    public BlockingQueue<Exchange> getQueue() { 6
        return queue;
    }

    public boolean isSingleton() { 7
        return true;
    }

    public List<Exchange> getExchanges() { 8
        return new ArrayList<Exchange>(getQueue());
    }
}

1

The SedaEndpoint class follows the pattern for implementing an event-driven endpoint by extending the DefaultEndpoint class. The SedaEndpoint class also implements the BrowsableEndpoint interface, which provides access to the list of exchange objects in the queue.

2

Following the usual pattern for an event-driven consumer, SedaEndpoint defines a constructor that takes an endpoint argument, endpointUri, and a component reference argument, component.

3

Another constructor is provided, which delegates queue creation to the parent component instance.

4

The createProducer() factory method creates an instance of CollectionProducer, which is a producer implementation that adds events to the queue.

5

The createConsumer() factory method creates an instance of SedaConsumer, which is responsible for pulling events off the queue and processing them.

6

The getQueue() method returns a reference to the queue.

7

The isSingleton() method returns true, indicating that a single endpoint instance should be created for each unique URI string.

8

The getExchanges() method implements the corresponding abstract method from BrowsableEndpoint.