LibraryToggle FramesPrintFeedback

Example 6.2 outlines how to implement the DefaultComponent.createEndpoint() method, which is responsible for creating endpoint instances on demand.

Example 6.2. Implementation of createEndpoint()

public class CustomComponent extends DefaultComponent { 1
    ...
    protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { 2
        CustomEndpoint result = new CustomEndpoint(uri, this); 3
        // ...
        return result;
    }
}

1

The CustomComponent is the name of your custom component class, which is defined by extending the DefaultComponent class.

2

When extending DefaultComponent, you must implement the createEndpoint() method with three arguments (see URI parsing).

3

Create an instance of your custom endpoint type, CustomEndpoint, by calling its constructor. At a minimum, this constructor takes a copy of the original URI string, uri, and a reference to this component instance, this.

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() { 1
    }

    public FileComponent(CamelContext context) { 2
        super(context);
    }

    protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { 3
        File file = new File(remaining);
        FileEndpoint result = new FileEndpoint(file, uri, this);
        return result;
    }
}

1

Always define a no-argument constructor for the component class in order to facilitate automatic instantiation of the class.

2

A constructor that takes the parent CamelContext instance as an argument is convenient when creating a component instance by programming.

3

The implementation of the FileComponent.createEndpoint() method follows the pattern described in Example 6.2. The implementation creates a FileEndpoint object.

Comments powered by Disqus