40.2. 끝점 인터페이스 구현

40.2.1. 끝점을 구현하는 다른 방법

다음과 같은 대체 끝점 구현 패턴이 지원됩니다.

40.2.2. 이벤트 중심 끝점 구현

사용자 지정 끝점이 이벤트 중심 패턴을 준수하는 경우 38.1.3절. “소비자 패턴 및 스레드”에 표시된 것처럼 abstract 클래스 org.apache.camel.impl.DefaultEndpoint 를 확장하여 구현됩니다. 예 40.2. “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 및 상위 구성 요소 참조, 구성 요소를 인수로 사용하는 생성자가 하나 이상 있어야 합니다.
3
createProducer() 팩토리 메서드를 구현하여 생산자 엔드포인트를 생성합니다.
4
createConsumer() 팩토리 메서드를 구현하여 이벤트 중심 소비자 인스턴스를 생성합니다.
5
일반적으로 createExchange() 메서드를 재정의할 필요가 없습니다. DefaultEndpoint 에서 상속된 구현은 기본적으로 모든 Apache Camel 구성 요소에서 사용할 수 있는 DefaultExchange 오브젝트를 생성합니다. 그러나 DefaultExchange 개체에서 일부 교환 속성을 초기화해야 하는 경우 교환 속성 설정을 추가하기 위해 여기에서 createExchange() 메서드를 재정의하는 것이 적절합니다.
중요

createPollingConsumer() 메서드를 재정의 하지 마십시오.

DefaultEndpoint 클래스는 사용자 지정 엔드포인트 코드를 작성할 때 유용할 수 있는 다음 메서드의 기본 구현을 제공합니다.

  • getEndpointUri() 엔드포인트 URI를 다시 시도합니다.
  • getCamelContext() Cryostat- Cryostat에 대한 참조로 CamelContext 에 대한 참조입니다.
  • getComponent() Cryostat- parent 구성 요소에 대한 참조입니다.
  • createPollingConsumer() Cryostat- Cryostat는 폴링 소비자를 만듭니다. 생성된 폴링 소비자 기능은 이벤트 중심 소비자를 기반으로 합니다. 이벤트 중심 소비자 메서드를 재정의하는 경우 createConsumer() 를 사용하면 폴링 소비자 구현을 얻을 수 있습니다.
  • CreateExchange(Exchange e) Cryostat- Cryostat는 지정된 교환 오브젝트( e )를 이 끝점에 필요한 유형으로 변환합니다. 이 메서드는 재정의된 createExchange() 엔드포인트를 사용하여 새 엔드포인트를 생성합니다. 이렇게 하면 해당 방법이 사용자 지정 교환 유형에서도 작동합니다.

40.2.3. 예약된 폴링 끝점 구현

사용자 정의 끝점이 예약된 폴링 패턴을 준수하는 경우 38.1.3절. “소비자 패턴 및 스레드”에 표시된 것처럼 org.apache.camel.impl.ScheduledPollEndpoint 에서 상속하여 구현됩니다. 예 40.3. “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 및 상위 구성 요소 참조, 구성 요소를 인수로 사용하는 생성자가 하나 이상 있어야 합니다.
3
createProducer() 팩토리 메서드를 구현하여 생산자 엔드포인트를 생성합니다.
4
createConsumer() 팩토리 메서드를 구현하여 예약된 폴링 소비자 인스턴스를 생성합니다.
5
ScheduledPollEndpoint 기본 클래스에 정의된 configureConsumer() 메서드는 소비자 쿼리 옵션을 소비자에 주입합니다. “소비자 매개변수 삽입”을 참조하십시오.
6
일반적으로 createExchange() 메서드를 재정의할 필요가 없습니다. DefaultEndpoint 에서 상속된 구현은 기본적으로 모든 Apache Camel 구성 요소에서 사용할 수 있는 DefaultExchange 오브젝트를 생성합니다. 그러나 DefaultExchange 개체에서 일부 교환 속성을 초기화해야 하는 경우 교환 속성 설정을 추가하기 위해 여기에서 createExchange() 메서드를 재정의하는 것이 적절합니다.
중요

createPollingConsumer() 메서드를 재정의 하지 마십시오.

40.2.4. 폴링 끝점 구현

사용자 정의 끝점이 폴링 소비자 패턴을 준수하는 경우 38.1.3절. “소비자 패턴 및 스레드”에 표시된 것처럼 abstract 클래스 org.apache.camel.impl.DefaultPollingEndpoint 에서 상속하여 구현됩니다. 예 40.4. “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() 에서 반환된 소비자 인스턴스는 PollingConsumer 인터페이스에서 상속해야 합니다. 폴링 소비자를 구현하는 방법에 대한 자세한 내용은 “폴링 소비자 구현” 을 참조하십시오.

createPollingConsumer() 메서드 구현 외에도 DefaultPollingEndpoint 를 구현하는 단계는 ScheduledPollEndpoint 를 구현하는 단계와 유사합니다. 자세한 내용은 예 40.3. “ScheduledPollEndpoint 구현” 을 참조하십시오.

40.2.5. BrowsableEndpoint 인터페이스 구현

현재 끝점에서 보류 중인 교환 인스턴스 목록을 노출하려면 예 40.5. “BrowsableEndpoint 인터페이스” 에 표시된 것처럼 org.apache.camel.spi.BrowsableEndpoint 인터페이스를 구현할 수 있습니다. 끝점이 들어오는 이벤트의 버퍼링을 수행하는 경우 이 인터페이스를 구현하는 것이 좋습니다. 예를 들어 Apache Camel SEDA 끝점은 BrowsableEndpoint 인터페이스 Cryostat- Cryostat 참조 예 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.2.6. 예제

예 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 및 구성 요소 참조 인수인 구성 요소를 사용하는 생성자를 정의합니다.
3
큐 생성을 부모 구성 요소 인스턴스에 위임하는 또 다른 생성자가 제공됩니다.
4
createProducer() 팩토리 메서드는 이벤트를 큐에 추가하는 생산자 구현인 CollectionProducer 의 인스턴스를 생성합니다.
5
createConsumer() 팩토리 메서드는 큐에서 이벤트를 가져오고 처리하는 SedaConsumer 의 인스턴스를 생성합니다.
6
getQueue() 메서드는 큐에 대한 참조를 반환합니다.
7
isSingleton() 메서드는 각 고유 URI 문자열에 대해 단일 엔드포인트 인스턴스를 생성해야 함을 나타내는 true 를 반환합니다.
8
getExchanges() 메서드는 BrowsableEndpoint에서 해당 추상 메서드를 구현합니다.