Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.7.2. Implementing the Endpoint Interface
Alternative ways of implementing an endpoint Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The following alternative endpoint implementation patterns are supported:
Event-driven endpoint implementation Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
If your custom endpoint conforms to the event-driven pattern (see Section 5.1.3, “Consumer Patterns and Threading”), it is implemented by extending the abstract class,
org.apache.camel.impl.DefaultEndpoint
, as shown in Example 7.2, “Implementing DefaultEndpoint”.
Example 7.2. Implementing DefaultEndpoint
- 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.ImportantDo not override thecreatePollingConsumer()
method. - 5
- In general, it is not necessary to override the
createExchange()
methods. The implementations inherited fromDefaultEndpoint
create aDefaultExchange
object by default, which can be used in any Apache Camel component. If you need to initialize some exchange properties in theDefaultExchange
object, however, it is appropriate to override thecreateExchange()
methods here in order to add the exchange property settings.
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.
Scheduled poll endpoint implementation Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
If your custom endpoint conforms to the scheduled poll pattern (see Section 5.1.3, “Consumer Patterns and Threading”) it is implemented by inheriting from the abstract class,
org.apache.camel.impl.ScheduledPollEndpoint
, as shown in Example 7.3, “ScheduledPollEndpoint Implementation”.
Example 7.3. ScheduledPollEndpoint Implementation
- 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.ImportantDo not override thecreatePollingConsumer()
method. - 5
- The
configureConsumer()
method, defined in theScheduledPollEndpoint
base class, is responsible for injecting consumer query options into the consumer. See the section called “Consumer parameter injection”. - 6
- In general, it is not necessary to override the
createExchange()
methods. The implementations inherited fromDefaultEndpoint
create aDefaultExchange
object by default, which can be used in any Apache Camel component. If you need to initialize some exchange properties in theDefaultExchange
object, however, it is appropriate to override thecreateExchange()
methods here in order to add the exchange property settings.
Polling endpoint implementation Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
If your custom endpoint conforms to the polling consumer pattern (see Section 5.1.3, “Consumer Patterns and Threading”), it is implemented by inheriting from the abstract class,
org.apache.camel.impl.DefaultPollingEndpoint
, as shown in Example 7.4, “DefaultPollingEndpoint Implementation”.
Example 7.4. DefaultPollingEndpoint Implementation
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 the section called “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, “ScheduledPollEndpoint Implementation” for details.
Implementing the BrowsableEndpoint interface Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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, “BrowsableEndpoint Interface”. It makes sense to implement this interface if the endpoint performs some sort of buffering of incoming events. For example, the Apache Camel SEDA endpoint implements the BrowsableEndpoint
interface—see Example 7.6, “SedaEndpoint Implementation”.
Example 7.5. BrowsableEndpoint Interface
Example Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Example 7.6, “SedaEndpoint Implementation” 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
- 1
- The
SedaEndpoint
class follows the pattern for implementing an event-driven endpoint by extending theDefaultEndpoint
class. TheSedaEndpoint
class also implements theBrowsableEndpoint
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 ofCollectionProducer
, which is a producer implementation that adds events to the queue. - 5
- The
createConsumer()
factory method creates an instance ofSedaConsumer
, 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 returnstrue
, indicating that a single endpoint instance should be created for each unique URI string. - 8
- The
getExchanges()
method implements the corresponding abstract method fromBrowsableEndpoint
.