Example usage for io.netty.channel ChannelHandlerContext channel

List of usage examples for io.netty.channel ChannelHandlerContext channel

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext channel.

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:com.alibaba.dubbo.remoting.transport.netty.NettyClientHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {/*from   www.  j a  v  a 2  s . c o  m*/
        handler.caught(channel, cause);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty.NettyServerHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.fireChannelActive();/*w  w  w. j  a  v  a2 s  .  c  om*/

    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {
        if (channel != null) {
            channels.put(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()), channel);
        }
        handler.connected(channel);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty.NettyServerHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {/* w w  w .ja  v  a 2s  .  c o  m*/
        channels.remove(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()));
        handler.disconnected(channel);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty4.NettyHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {

    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {//  w w  w. j  ava2  s. c o m
        if (channel != null) {
            channels.put(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()), channel);
        }
        handler.connected(channel);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty4.NettyHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {/*from w  ww.j  a  v a  2  s.c  o  m*/
        channels.remove(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()));
        handler.disconnected(channel);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }
}

From source file:com.alibaba.dubbo.remoting.transport.netty4.NettyHandler.java

License:Apache License

@Override
public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg) throws java.lang.Exception {

    @SuppressWarnings("unchecked")
    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {/*ww  w .  j ava 2s.c  o m*/
        handler.received(channel, msg);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }

}

From source file:com.alibaba.dubbo.remoting.transport.netty4.NettyHandler.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ctx.writeAndFlush(msg, promise);/*from ww w  .  ja v a  2s . co  m*/
    NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
    try {
        handler.sent(channel, msg);
    } finally {
        NettyChannel.removeChannelIfDisconnected(ctx.channel());
    }
}

From source file:com.alibaba.middleware.race.mom.netty.NettyServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {
    Channel channel = ctx.channel();
    LOG.info("message read: \r\n" + msg);
    if (msg instanceof Message) {
        Message myMsg = (Message) msg;//  w  w w . j av  a2 s .  c o m
        //          
        //          System.out.println("message id : " + myMsg.getMsgId() + " , message delay after netty server received : " + (System.currentTimeMillis() - myMsg.getBornTime()));

        brokerEngine.onMessageReceived(myMsg, ctx.channel());
    } else {
        ControlMessage myMsg = (ControlMessage) msg;
        brokerEngine.onCtrlMsgReceived(myMsg, channel);
    }
}

From source file:com.alibaba.middleware.race.mom.netty.NettyServerHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();//from   w  w  w  .j av a 2  s  .  co  m

    brokerEngine.onChannelCloseAbnormally(ctx.channel());
    ctx.close();
}

From source file:com.alibaba.rocketmq.broker.processor.AbstractSendMessageProcessor.java

License:Apache License

protected SendMessageContext buildMsgContext(ChannelHandlerContext ctx,
        SendMessageRequestHeader requestHeader) {
    if (!this.hasSendMessageHook()) {
        return null;
    }/*from   w w w  . ja va  2s  . c  o m*/
    SendMessageContext mqtraceContext;
    mqtraceContext = new SendMessageContext();
    mqtraceContext.setProducerGroup(requestHeader.getProducerGroup());
    mqtraceContext.setTopic(requestHeader.getTopic());
    mqtraceContext.setMsgProps(requestHeader.getProperties());
    mqtraceContext.setBornHost(RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
    mqtraceContext.setBrokerAddr(this.brokerController.getBrokerAddr());
    return mqtraceContext;
}