You implement a new component by extending the
org.apache.camel.impl.DefaultComponent
class, which provides some
standard functionality and default implementations for some of the methods. In particular,
the DefaultComponent
class provides support for URI parsing and for
creating a scheduled executor (which is used for the scheduled poll
pattern).
The createEndpoint(String uri)
method defined in the base
Component
interface takes a complete, unparsed endpoint URI
as its sole argument. The DefaultComponent
class, on the other hand,
defines a three-argument version of the createEndpoint()
method
with the following
signature:
protected abstract Endpoint createEndpoint( String uri, String remaining, Map parameters ) throws Exception;
uri
is the original, unparsed URI; remaining
is the part of the URI that remains
after stripping off the component prefix at the start and cutting off the query options at
the end; and parameters
contains the parsed query options. It is this version
of the createEndpoint()
method that you must override when
inheriting from DefaultComponent
. This has the advantage that the
endpoint URI is already parsed for you.
The following sample endpoint URI for
the file
component shows how URI parsing works in
practice:
file:///tmp/messages/foo?delete=true&moveNamePostfix=.old
For
this URI, the following arguments are passed to the three-argument version of
createEndpoint()
:
Argument | Sample Value |
---|---|
uri | file:///tmp/messages/foo?delete=true&moveNamePostfix=.old |
remaining | /tmp/messages/foo |
parameters |
Two entries are set in
|
By default, the parameters extracted from the URI query options are injected on the
endpoint's bean properties. The DefaultComponent
class automatically
injects the parameters for you.
For example, if you want to define a custom
endpoint that supports two URI query options: delete
and
moveNamePostfix
. All you must do is define the corresponding bean methods
(getters and setters) in the endpoint
class:
public class FileEndpoint extends ScheduledPollEndpoint { ... public boolean isDelete() { return delete; } public void setDelete(boolean delete) { this.delete = delete; } ... public String getMoveNamePostfix() { return moveNamePostfix; } public void setMoveNamePostfix(String moveNamePostfix) { this.moveNamePostfix = moveNamePostfix; } }
It is also possible to inject URI query options into consumer parameters. For details, see Consumer parameter injection.
If there are no parameters defined on your Endpoint
class, you can optimize
the process of endpoint creation by disabling endpoint parameter injection. To disable
parameter injection on endpoints, override the
useIntrospectionOnEndpoint()
method and implement it to return
false
, as follows:
protected boolean useIntrospectionOnEndpoint() { return false; }
![]() | Note |
---|---|
The |
The scheduled executor is used in the scheduled poll pattern, where it is responsible for driving the periodic polling of a consumer endpoint (a scheduled executor is effectively a thread pool implementation).
To instantiate a scheduled executor service, use the
ExecutorServiceStrategy
object that is returned by the
CamelContext.getExecutorServiceStrategy()
method. For details of the Fuse Mediation Router
threading model, see Threading Model in Implementing Enterprise Integration Patterns.
![]() | Note |
---|---|
Prior to Fuse Mediation Router 2.3, the |
If you want to validate the URI before creating an endpoint instance, you can override
the validateURI()
method from the
DefaultComponent
class, which has the following signature:
protected void validateURI(String uri,
String path,
Map parameters)
throws ResolveEndpointFailedException;
If the supplied URI does not have the required format, the implementation of
validateURI()
should throw the
org.apache.camel.ResolveEndpointFailedException
exception.
Example 6.2 outlines how to implement the
DefaultComponent.createEndpoint()
method, which is responsible
for creating endpoint instances on demand.
The | |
When extending | |
Create an instance of your custom endpoint type,
|
Example 6.3 shows a sample implementation of a
FileComponent
class.
Example 6.3. FileComponent Implementation
package org.apache.camel.component.file; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.impl.DefaultComponent; import java.io.File; import java.util.Map; public class FileComponent extends DefaultComponent { public static final String HEADER_FILE_NAME = "org.apache.camel.file.name"; public FileComponent() {} public FileComponent(CamelContext context) {
super(context); } protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
File file = new File(remaining); FileEndpoint result = new FileEndpoint(file, uri, this); return result; } }
Always define a no-argument constructor for the component class in order to facilitate automatic instantiation of the class. | |
A constructor that takes the parent | |
The implementation of the |