239.9. 생성된 파이프라인을 완전히 제어할 수 있도록 사용자 정의 채널 파이프라인 팩토리 추가

Camel 2.5에서 사용 가능

사용자 지정 채널 파이프라인은 사용자 정의 처리기(s), 인코더(s) 및 디코더를 매우 간단한 방식으로 지정할 필요 없이 처리기/인터 체인에서 완전한 제어를 사용자에게 제공합니다.

사용자 지정 파이프라인을 추가하려면 컨텍스트 레지스트리(JNDIRegistry 또는 camel-spring ApplicationContextRegistry 등)를 통해 사용자 정의 채널 파이프라인 팩토리를 생성하고 컨텍스트에 등록해야 합니다.

사용자 정의 파이프라인 팩토리는 다음과 같이 구성되어야 합니다.

  • Producer 링크 채널 파이프라인 팩토리는 추상 클래스 클라이언트PipelineFactory 를 확장해야 합니다.
  • 소비자 연결 채널 파이프라인 팩토리는 추상 클래스 ServerPipelineFactory 를 확장해야 합니다.
  • 클래스는 사용자 지정 처리기, 인코더 및 디코더를 삽입하기 위해 getPipeline() 메서드를 재정의해야 합니다. getPipeline() 메서드를 재정의하지 않으면 핸들러, 인코더 또는 디코더가 파이프라인에 연결된 파이프라인이 없습니다.

아래 예는 ServerChannel 파이프라인 팩토리를 생성하는 방법을 보여줍니다.

사용자 정의 파이프라인 팩토리 사용

public class SampleServerChannelPipelineFactory extends ServerPipelineFactory {
    private int maxLineSize = 1024;

    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline channelPipeline = Channels.pipeline();

        channelPipeline.addLast("encoder-SD", new StringEncoder(CharsetUtil.UTF_8));
        channelPipeline.addLast("decoder-DELIM", new DelimiterBasedFrameDecoder(maxLineSize, true, Delimiters.lineDelimiter()));
        channelPipeline.addLast("decoder-SD", new StringDecoder(CharsetUtil.UTF_8));
        // here we add the default Camel ServerChannelHandler for the consumer, to allow Camel to route the message etc.
        channelPipeline.addLast("handler", new ServerChannelHandler(consumer));

        return channelPipeline;
    }
}

그런 다음 사용자 정의 채널 파이프라인 팩 팩 팩 팩 팩 팩 팩토리에 다음과 같은 방식으로 레지스트리에 추가하고 인스턴스화/사용할 수 있습니다.

Registry registry = camelContext.getRegistry();
serverPipelineFactory = new TestServerChannelPipelineFactory();
registry.bind("spf", serverPipelineFactory);
context.addRoutes(new RouteBuilder() {
  public void configure() {
      String netty_ssl_endpoint =
         "netty:tcp://0.0.0.0:5150?serverPipelineFactory=#spf"
      String return_string =
         "When You Go Home, Tell Them Of Us And Say,"
         + "For Your Tomorrow, We Gave Our Today.";

      from(netty_ssl_endpoint)
       .process(new Processor() {
          public void process(Exchange exchange) throws Exception {
            exchange.getOut().setBody(return_string);
          }
       }
  }
});