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 7.2.
Example 7.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; import org.apache.camel.impl.DefaultExchange; public classCustomEndpoint
extends DefaultEndpoint { publicCustomEndpoint
(String endpointUri, Component component) { super(endpointUri, component); // Do any other initialization... } public Producer createProducer() throws Exception { return newCustomProducer
(this); } public Consumer createConsumer(Processor processor) throws Exception { return newCustomConsumer
(this, processor); } public boolean isSingleton() { return true; } // Implement the following methods, only if you need to set exchange properties. // public Exchange createExchange() { return this.createExchange(getExchangePattern()); } public Exchange createExchange(ExchangePattern pattern) { Exchange result = new DefaultExchange(getCamelContext(), pattern); // Set exchange properties ... return result; } }
Implement an event-driven custom endpoint,
| ||||
You must have at least one constructor that takes the endpoint URI,
| ||||
Implement the | ||||
Implement the
| ||||
In general, it is not necessary to override the
|
The DefaultEndpoint
class provides default
implementations of the following methods, which you might find useful when writing your
custom endpoint code:
getEndpointUri()
—Returns the endpoint URI.getCamelContext()
—Returns a reference to theCamelContext
.getComponent()
—Returns a reference to the parent component.createPollingConsumer()
—Creates a polling consumer. The created polling consumer's functionality is based on the event-driven consumer. If you override the event-driven consumer method,createConsumer()
, you get a polling consumer implementation for free.createExchange(Exchange e)
—Converts the given exchange object,e
, to the type required for this endpoint. This method creates a new endpoint using the overriddencreateExchange()
endpoints. This ensures that the method also works for custom exchange types.Note From Apache Camel 2.0 onwards, the
camel-core
module has been refactored so that theDefaultExchange
type can be used with any component. It is therefore no longer necessary to define a custom exchange type.
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 7.3.
Example 7.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 classCustomEndpoint
extends ScheduledPollEndpoint { protectedCustomEndpoint
(String endpointUri,CustomComponent
component) { super(endpointUri, component); // Do any other initialization... } public Producer createProducer() throws Exception { Producer result = newCustomProducer
(this); return result; } public Consumer createConsumer(Processor processor) throws Exception { Consumer result = newCustomConsumer
(this, processor); configureConsumer(result); return result; } public boolean isSingleton() { return true; } // Implement the following methods, only if you need to set exchange properties. // public Exchange createExchange() { return this.createExchange(getExchangePattern()); } public Exchange createExchange(ExchangePattern pattern) { Exchange result = new DefaultExchange(getCamelContext(), pattern); // Set exchange properties ... return result; } }
Implement a scheduled poll custom endpoint,
| ||||
You must to have at least one constructor that takes the endpoint URI,
| ||||
Implement the | ||||
Implement the
| ||||
The | ||||
In general, it is not necessary to override the
|
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 7.4.
Example 7.4. DefaultPollingEndpoint 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.DefaultPollingEndpoint; public classCustomEndpoint
extends DefaultPollingEndpoint { ... public PollingConsumer createPollingConsumer() throws Exception { PollingConsumer result = newCustomConsumer
(this); configureConsumer(result); return result; } // Do NOT implement createConsumer(). It is already implemented in DefaultPollingEndpoint. ... }
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 7.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 7.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 7.6.
Example 7.5. BrowsableEndpoint Interface
package org.apache.camel.spi; import java.util.List; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; public interface BrowsableEndpoint extends Endpoint { List<Exchange> getExchanges(); }
Example 7.6 shows a sample implementation of
SedaEndpoint
. 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 7.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 implements BrowsableEndpoint { private BlockingQueue<Exchange> queue; public SedaEndpoint(String endpointUri, Component component, BlockingQueue<Exchange> queue) { super(endpointUri, component); this.queue = queue; } public SedaEndpoint(String uri, SedaComponent component, Map parameters) { this(uri, component, component.createQueue(uri, parameters)); } public Producer createProducer() throws Exception { return new CollectionProducer(this, getQueue()); } public Consumer createConsumer(Processor processor) throws Exception { return new SedaConsumer(this, processor); } public BlockingQueue<Exchange> getQueue() { return queue; } public boolean isSingleton() { return true; } public List<Exchange> getExchanges() { return new ArrayList<Exchange>(getQueue()); } }
The | |
Following the usual pattern for an event-driven consumer,
| |
Another constructor is provided, which delegates queue creation to the parent component instance. | |
The | |
The | |
The | |
The | |
The |