com.dingwang.netty.handler.PersonInHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.dingwang.netty.handler.PersonInHandler.java

Source

/*
 * Copyright 2016 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.dingwang.netty.handler;

import com.dingwang.netty.pojo.Person;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

/**
 * PersonInHandler.java??TODO ??
 * 
 * @author wangding_91@163.com 2016222 ?11:53:01
 */
public class PersonInHandler extends ChannelHandlerAdapter {

    /*
     * (non-Javadoc)
     * @see io.netty.channel.ChannelHandlerAdapter#channelRead(io.netty.channel.
     * ChannelHandlerContext, java.lang.Object)
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        Person message = (Person) msg;

        System.out
                .println("PersonInHandler channelRead message=" + message.getName() + ">>>>>>" + message.getAge());

        ctx.close();

    }

    /*
     * (non-Javadoc)
     * @see io.netty.channel.ChannelHandlerAdapter#write(io.netty.channel.
     * ChannelHandlerContext, java.lang.Object, io.netty.channel.ChannelPromise)
     */
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

        System.out.println("PersonInHandler   write>>>>>" + msg.toString());
        ctx.writeAndFlush(msg);

    }

    /*
     * (non-Javadoc)
     * @see
     * io.netty.channel.ChannelHandlerAdapter#exceptionCaught(io.netty.channel.
     * ChannelHandlerContext, java.lang.Throwable)
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        ctx.close();
    }

    /*
     * (non-Javadoc)
     * @see
     * io.netty.channel.ChannelHandlerAdapter#channelActive(io.netty.channel.
     * ChannelHandlerContext)
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        System.out.println("client active");

    }

    /*
     * (non-Javadoc)
     * @see
     * io.netty.channel.ChannelHandlerAdapter#channelInactive(io.netty.channel.
     * ChannelHandlerContext)
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client inactive");
    }
}