码迷,mamicode.com
首页 > Web开发 > 详细

基于kryonet的RPC,使用kryo进行序列化

时间:2015-05-11 14:39:43      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

Kryo是一个序列化框架。

Kryonet是一个基于kryo的RPC框架,它实现了一套高效简洁的API,它通过NIO实现了TCP和UDP通讯,目前还不支持Http。

自己写了一个测试代码,运行了下,感觉还不错,记录下来。

1、listener

package com.mytestcodes.kryonet;  
  
import com.esotericsoftware.kryonet.Connection;  
import com.esotericsoftware.kryonet.Listener;  
import com.mytestcodes.serialization.fulltest.Child;  
  
public class KListener extends Listener  
{  
    public KListener()  
    {  
  
    }  
  
    public KListener(String name)  
    {  
        this.name = name;  
    }  
  
    private String name;  
  
    public void received(Connection connection, Object object)  
    {  
        System.out.println(name + " has recive a message");  
  
        if (object instanceof Child)  
        {  
            Child child = (Child) object;  
            System.out.println(child.getChildName());  
            child.setChildName(name + " response");  
            connection.sendTCP(child);  
        }  
    }  
  
    public String getName()  
    {  
        return name;  
    }  
  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
  
}  

2、client

package com.mytestcodes.kryonet;  
  
  
import java.io.IOException;  
import java.nio.ByteBuffer;  
  
  
import com.esotericsoftware.kryonet.Client;  
import com.mytestcodes.serialization.fulltest.Baby;  
import com.mytestcodes.serialization.fulltest.Child;  
import com.mytestcodes.serialization.fulltest.Parent;  
  
  
public class KClient  
{  
    public static void main(String[] args)  
    {  
        Client client = new Client();  
        client.addListener(new KListener("Client"));  
        client.start();  
        try  
        {  
            client.connect(5000, "localhost", 54555, 54777);  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
  
  
        client.getKryo().setRegistrationOptional(true);  
        Child child = new Child();  
  
  
        client.sendTCP(child);  
    }  
  
  
    public static byte[] readBuf(ByteBuffer buf)  
    {  
        int size = buf.position();  
        byte[] newBuf = new byte[size];  
        for (int i = 0; i < size; i++)  
        {  
            newBuf[i] = buf.get(i);  
        }  
        return newBuf;  
    }  
} 

3、server

package com.mytestcodes.kryonet;  
  
  
import java.io.IOException;  
  
  
import com.esotericsoftware.kryonet.Server;  
  
  
public class KServer  
{  
    public static void main(String[] args)  
    {  
        Server server = new Server();  
        server.addListener(new KListener("server"));  
  
  
        server.getKryo().setRegistrationOptional(true);  
        server.start();  
        try  
        {  
            // TCP port 54555 and UDP port 54777  
            server.bind(54555, 54777);  
        } catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
    }  
}  

 

4、child:

package com.mytestcodes.serialization.fulltest;  
  
import java.io.Serializable;  
  
public class Baby implements Serializable  
{  
  
    /**  
     *   
     */  
    private static final long serialVersionUID = 8882758100866916676L;  
  
    private String babyName = "baby";  
  
    public String getBabyName()  
    {  
        return babyName;  
    }  
  
    public void setBabyName(String babyName)  
    {  
        this.babyName = babyName;  
    }  
  
}
package com.mytestcodes.serialization.fulltest;  
  
import java.io.Serializable;  
  
public class Parent implements Serializable  
{  
  
    /**  
     *   
     */  
    private static final long serialVersionUID = 6933088125784071832L;  
      
    private String parentName="parent";  
  
    public String getParentName()  
    {  
        return parentName;  
    }  
  
    public void setParentName(String parentName)  
    {  
        this.parentName = parentName;  
    }  
      
}  

 

 

基于kryonet的RPC,使用kryo进行序列化

标签:

原文地址:http://www.cnblogs.com/duanxz/p/4494395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!