46.9. 사용자 정의 파이프라인
사용자 정의 채널 파이프라인은 매우 간단한 방법으로 Netty Endpoint URL에 지정하지 않고도 사용자 정의 처리기(s), 인코더 및 디코더를 삽입하여 처리기/차단 체인에 완전한 제어를 제공합니다.
사용자 지정 파이프라인을 추가하려면 컨텍스트 레지스트리(Registry 또는 camel-spring ApplicationContextRegistry 등)를 통해 컨텍스트로 사용자 정의 채널 파이프라인 팩토리를 생성하고 등록해야 합니다.
사용자 정의 파이프라인 팩토리는 다음과 같이 구성되어야 합니다.
-
Producer 연결 채널 파이프라인 팩토리에서 추상 클래스
ClientPipelineFactory를 확장해야 합니다. -
소비자 연결 채널 파이프라인 팩토리에서 추상 클래스
Server>-<rFactory를 확장해야 합니다. -
클래스는 사용자 정의 처리기, 인코더 및 디코더를 삽입하기 위해 initChannel() 메서드를 재정의해야 합니다.
initChannel()메서드를 재정의하지 않으면 파이프라인에 연결된 처리기, 인코더 또는 디코더 없이 파이프라인이 생성됩니다.
아래 예제에서는 Server>-<rFactory 팩토리를 만드는 방법을 보여줍니다.
46.9.1. 사용자 정의 파이프라인 팩토리 사용 링크 복사링크가 클립보드에 복사되었습니다!
링크 복사링크가 클립보드에 복사되었습니다!
public class SampleServerInitializerFactory extends ServerInitializerFactory {
private int maxLineSize = 1024;
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline channelPipeline = ch.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));
}
}
그런 다음 사용자 정의 채널 파이프라인 팩토리를 레지스트리에 추가하고 다음과 같은 방식으로 camel 경로에서 인스턴스화/사용할 수 있습니다.
Registry registry = camelContext.getRegistry();
ServerInitializerFactory factory = new TestServerInitializerFactory();
registry.bind("spf", factory);
context.addRoutes(new RouteBuilder() {
public void configure() {
String netty_ssl_endpoint =
"netty:tcp://0.0.0.0:5150?serverInitializerFactory=#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);
}
}
}
});