40.2. エンドポイントインターフェイスの実装

エンドポイントを実装する代替方法

以下の代替エンドポイント実装パターンがサポートされます。

イベント駆動型のエンドポイント実装

カスタムエンドポイントがイベント駆動型のパターン (「コンシューマーパターンおよびスレッド」 を参照) に準拠する場合、例40.2「DefaultEndpoint の実装」 に示されているように org.apache.camel.impl.DefaultEndpoint 抽象クラスを拡張することで実装されます。

例40.2 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 class CustomEndpoint extends DefaultEndpoint { 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 methods, only if you need to set exchange properties.
    //
    public Exchange createExchange() { 5
        return this.createExchange(getExchangePattern());
    }

    public Exchange createExchange(ExchangePattern pattern) {
        Exchange result = new DefaultExchange(getCamelContext(), pattern);
        // Set exchange properties
        ...
        return result;
    }
}
1
DefaultEndpoint クラスを拡張して、イベント駆動型 CustomEndpoint カスタムエンドポイントを実装します。
2
エンドポイント URI の endpointUri および親コンポーネントの参照 component を引数として取るコンストラクターが少なくとも 1 つ必要です。
3
createProducer() ファクトリーメソッドを実装し、プロデューサーエンドポイントを作成します。
4
createConsumer(): イベント駆動型のコンシューマーインスタンスを作成するためのファクトリーメソッド。
5
通常、createExchange() メソッドを上書きする必要は ありませんDefaultEndpoint から継承された実装は、デフォルトで DefaultExchange オブジェクトを作成します。これは任意の Apache Camel コンポーネントで使用できます。ただし、DefaultExchange オブジェクトで一部のエクスチェンジプロパティーを初期化する必要がある場合は、エクスチェンジプロパティー設定を追加するために、この createExchange() メソッドを上書きすることが推奨されます。
重要

createPollingConsumer() メソッドをオーバーライド しないでください

DefaultEndpoint クラスは、以下のメソッドのデフォルト実装を提供します。これは、カスタムエンドポイントコードを書き込む際に役立つ場合があります。

  • getEndpointUri(): エンドポイント URI を返します。
  • getCamelContext(): CamelContext への参照を返します。
  • getComponent(): 親コンポーネントへの参照を返します。
  • createPollingConsumer(): ポーリングコンシューマーを作成します。作成されたポーリングコンシューマーの機能は、イベント駆動型のコンシューマーに基づいています。createConsumer() でイベント駆動型のコンシューマーメソッドを上書きする場合は、ポーリングコンシューマー実装を取得できます。
  • createExchange(Exchange e): 指定のエクスチェンジオブジェクト e を、このエンドポイントに必要な型に変換します。このメソッドは、上書きされた createExchange() エンドポイントを使用して、新しいエンドポイントを作成します。これにより、メソッドがカスタムのエクスチェンジタイプでも機能するようになります。

スケジュールされたポーリングエンドポイントの実装

カスタムエンドポイントがスケジュールされたポーリングパターン (「コンシューマーパターンおよびスレッド」 を参照) に準拠する場合、例40.3「ScheduledPollEndpoint 実装」 に示されている org.apache.camel.impl.ScheduledPollEndpoint の抽象クラスから継承して実装されます。

例40.3 ScheduledPollEndpoint 実装

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 {  1

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

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

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

    public boolean isSingleton() {
        return true;
    }

    // Implement the following methods, only if you need to set exchange properties.
    //
    public Exchange createExchange() { 6
        return this.createExchange(getExchangePattern());
    }

    public Exchange createExchange(ExchangePattern pattern) {
        Exchange result = new DefaultExchange(getCamelContext(), pattern);
        // Set exchange properties
        ...
        return result;
    }
}
1
ScheduledPollEndpoint クラスを拡張して、スケジュールされたポーリングのカスタムエンドポイント CustomEndpoint を実装します。
2
エンドポイント URI の endpointUri および親コンポーネントの参照 component を引数として取るコンストラクターが少なくとも 1 つ必要です。
3
createProducer() ファクトリーメソッドを実装し、プロデューサーエンドポイントを作成します。
4
スケジュールされたポーリングコンシューマーインスタンスを作成するには、createConsumer() ファクトリーメソッドを実装します。
5
ScheduledPollEndpoint ベースクラスで定義される configureConsumer() メソッドは、コンシューマークエリーオプションをコンシューマーに注入します。「コンシューマーパラメーターの注入」を参照してください。
6
通常、createExchange() メソッドを上書きする必要は ありませんDefaultEndpoint から継承された実装は、デフォルトで DefaultExchange オブジェクトを作成します。これは任意の Apache Camel コンポーネントで使用できます。ただし、DefaultExchange オブジェクトで一部のエクスチェンジプロパティーを初期化する必要がある場合は、エクスチェンジプロパティー設定を追加するために、この createExchange() メソッドを上書きすることが推奨されます。
重要

createPollingConsumer() メソッドをオーバーライド しないでください

ポーリングエンドポイントの実装

カスタムエンドポイントがポーリングコンシューマーパターン (「コンシューマーパターンおよびスレッド」 を参照) に準拠する場合、例40.4「DefaultPollingEndpoint 実装」 に示されている org.apache.camel.impl.DefaultPollingEndpoint の抽象クラスから継承して実装されます。

例40.4 DefaultPollingEndpoint 実装

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 class CustomEndpoint extends DefaultPollingEndpoint {
    ...
    public PollingConsumer createPollingConsumer() throws Exception {
        PollingConsumer result = new CustomConsumer(this);
        configureConsumer(result);
        return result;
    }

    // Do NOT implement createConsumer(). It is already implemented in DefaultPollingEndpoint.
    ...
}

この CustomEndpoint クラスはポーリングエンドポイントであるため、createConsumer() メソッドの代わりに createPollingConsumer() メソッドを実装する必要があります。createPollingConsumer() から返されるコンシューマーインスタンスは、PolingConsumer インターフェイスから継承する必要があります。ポーリングコンシューマーの実装方法は、「ポーリングコンシューマーの実装」 を参照してください。

createPollingConsumer() メソッドの実装以外に、DefaultPollingEndpoint を実装する手順は、ScheduledPollEndpoint を実装する手順と似ています。詳細は、例40.3「ScheduledPollEndpoint 実装」 を参照してください。

BrowsableEndpoint インターフェイスの実装

現在のエンドポイントで保留中のエクスチェンジインスタンスの一覧を公開する場合は、例40.5「BrowsableEndpoint インターフェイス」 に示されているように org.apache.camel.spi.BrowsableEndpoint インターフェースを実装することができます。エンドポイントが受信イベントのバッファー処理を実行する場合は、このインターフェイスを実装することが理にかなっています。たとえば、Apache Camel SEDA エンドポイントは BrowsableEndpoint インターフェースを実装します。例40.6「SedaEndpoint 実装」 を参照してください。

例40.5 BrowsableEndpoint インターフェイス

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();
}

例40.6「SedaEndpoint 実装」 は、SedaEndpoint の実装例を示しています。SEDA エンドポイントは、イベント駆動型のエンドポイント の例です。受信イベントは FIFO キュー (java.util.concurrent.BlockingQueue のインスタンス) に格納され、SEDA コンシューマーは、イベントの読み取りおよび処理のためにスレッドを起動します。イベント自体は org.apache.camel.Exchange オブジェクトによって表されます。

例40.6 SedaEndpoint 実装

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 { 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
SedaEndpoint クラスは、DefaultEndpoint クラスを拡張してイベント駆動型のエンドポイントを実装するパターンに従います。この SedaEndpoint クラスは、キュー内のエクスチェンジオブジェクトの一覧へのアクセスを提供する BrowsableEndpoint インターフェイスも実装します。
2
SedaEndpoint はイベント駆動型のコンシューマーの通常のパターンに従い、エンドポイント引数 endpointUri およびコンポーネント参照引数 component を使用するコンストラクターを定義します。
3
もう 1 つのコンストラクターが提供され、キューの作成を親コンポーネントインスタンスに委譲します。
4
ファクトリーメソッド createProducer() は、イベントをキューに追加するプロデューサーの実装である CollectionProducer のインスタンスを作成します。
5
ファクトリーメソッド createConsumer() は、SedaConsumer のインスタンスを作成します。これはキューからイベントをプルし、それらを処理します。
6
getQueue() メソッドはキューへの参照を返します。
7
isSingleton() メソッドは true を返します。これは、一意の URI 文字列ごとに単一のエンドポイントインスタンスを作成する必要があることを示します。
8
この getExchanges() メソッドは、対応する BrowsableEndpoint からの抽象メソッドを実装します。