2017-01-30 19 views

答えて

-1

ErrorHandlerを使用して、例えばLoggingErrorHandler:

RouteBuilder builder = new RouteBuilder() { 
    public void configure() { 
     // use logging error handler 
     errorHandler(loggingErrorHandler("com.mycompany.foo")); 

     // here is our regular route 
     from("{{uri.client.address}}").to("mock:out"); 
    } 
}; 
0

その少し複雑個々のクライアントの切断をログに記録するが、私は私がしなければならなかったものを共有しようとします。

独自のクラスでSimpleChannelInboundHandlerを拡張し、必要なメソッドを追加する必要があります。たとえば、クラスはこちらをご覧ください:私があればログイン一部

@Override 
    public void channelInactive(ChannelHandlerContext ctx) throws Exception { 
    //command to send when there is a disconnect from client side 
    String command = "clientDisconnect"; 
    if (LOG.isTraceEnabled()) { 
     LOG.trace("Channel closed: {}", ctx.channel()); 
    } 
    LOG.info("Channel has disconnected: affects ", uniqueClientId); 
    //create Exchange and let the consumer process it 
    final Exchange exchange = consumer.getEndpoint().createExchange(ctx, command); 
    //create inOnly as there is no client waiting for a response 
    exchange.setPattern(ExchangePattern.InOnly); 
    // set the exchange charset property for converting 
    if (consumer.getConfiguration().getCharsetName() != null) { 
     exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(consumer.getConfiguration().getCharsetName())); 
    } 
    // we want to handle the UoW 
    consumer.createUoW(exchange); 
    //set the uniqueClientId 
    beforeProcess(exchange, ctx, command); 
    processAsynchronously(exchange, ctx, command); 
    // to keep track of open sockets 
    consumer.getNettyServerBootstrapFactory().removeChannel(ctx.channel()); 
    super.channelInactive(ctx); 
} 

注:

https://github.com/apache/camel/blob/master/components/camel-netty4/src/main/java/org/apache/camel/component/netty4/handlers/ServerChannelHandler.java

が主な方法は、その時私は、クライアントの切断が含まれるように変更されました見てはこの1つですチャンネルが接続されています。

メインルートの異なるクライアントに異なる処理を実行する場合は、別のクライアントのIPアドレスを記録することもできます。これはあなたを助けるはずです。

編集: そのクラスを拡張したら、拡張クラスTCPServerHandlerを呼び出します。次に、以前のクラスを参照してエンコーダ/デコーダを設定するためにTCPServerHandlerFactoryを作成する必要があります。このクラスは次のようになります。

public class TCPServerInitializerFactory extends ServerInitializerFactory { 
    private NettyConsumer consumer; 

    public TCPServerInitializerFactory(NettyConsumer consumer) { 
    this.consumer = consumer; 
    } 
    @Override 
    protected void initChannel(Channel channel) throws Exception { 
    ChannelPipeline pipeline = channel.pipeline(); 
    pipeline.addLast("string-encoder", new StringEncoder(CharsetUtil.UTF_8)); 
    pipeline.addLast("string-decoder", new StringDecoder(CharsetUtil.UTF_8)); 
    **pipeline.addLast("handler", new TCPServerHandler(consumer));** 
    } 

    @Override 
    public ServerInitializerFactory createPipelineFactory(NettyConsumer nettyConsumer) { 
    return new TCPServerInitializerFactory(nettyConsumer); 
    } 
} 

あなたはTCPServerHandlerクラスを作成しているところ**でマークされた行があります。

はその後戻ってラクダであなたのルートクラスでは、このようなあなたのレジストリに工場を追加する必要があります。

TCPServerInitializerFactory serverFactory = new TCPServerInitializerFactory(null); 
    final CamelContext camelContext = getContext(); 
    final org.apache.camel.impl.SimpleRegistry registry = new org.apache.camel.impl.SimpleRegistry(); 
    final org.apache.camel.impl.CompositeRegistry compositeRegistry = new org.apache.camel.impl.CompositeRegistry(); 
    compositeRegistry.addRegistry(camelContext.getRegistry()); 
    compositeRegistry.addRegistry(registry); 
    ((org.apache.camel.impl.DefaultCamelContext) camelContext).setRegistry(compositeRegistry); 
registry.put("spf", serverFactory); 

はその後のような、それを参照してください。

from("netty4:tcp://0.0.0.0:3000?serverInitializerFactory=#spf&sync=true")... 

今、あなたは不思議に思って、これらすべてが切断のためのログを登録します。これは、クライアントがNettyコードに必要なクライアントのIPを切断して実際に取得するときを区別できるためです。あなたは交換のいくつかの価値を保存することができますし、後であなたのルートで何かを行う。

+0

ありがとう、しかし私はキャメルで新しくなっています。 SimpleChannelInboundHandlerの拡張クラスはどのように使うべきですか? –

+0

私の答えを編集しました。 –

+0

@ Souciance Eqdam Rashti getContext()。getStatus()で各リクエストのチャンネルステータスを確認できますか? –

関連する問題