使用Marshalling传输信息,需要有以下两个包,可以在官网下载
jboss-marshalling-1.3.0.CR9.jar
jboss-marshalling-serial-1.3.0.CR9.jar
一、编写要作为传输的Javabean,Student类一定要继承Serializable接口,才能实现序列化
- package demo;
-
- import java.io.Serializable;
-
- public class Student implements Serializable{
- String name;
- String classs;
- int age;
-
- @Override
- public String toString() {
- return "Student [name=" + name + ", classs=" + classs + ", age=" + age + "]";
- }
-
- public Student(String name, String classs, int age) {
- super();
- this.name = name;
- this.classs = classs;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getClasss() {
- return classs;
- }
-
- public void setClasss(String classs) {
- this.classs = classs;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- }
二、编写客户端:
通过MarshallingCodeFactory获得MarshallingDecoder和MarshallingEncoder,并将这两个编解码器添加到channelpipeline中
- package client;
-
- import factory.MarshallingCodeCFactory;
- import io.netty.bootstrap.Bootstrap;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioSocketChannel;
-
- public class Client {
- public static void main(String[] args) throws Exception {
- new Client().connect("127.0.0.1", 8888);
- }
-
- public void connect(String host, int port) throws Exception {
- EventLoopGroup group = new NioEventLoopGroup();
- try {
- Bootstrap b = new Bootstrap();
- b.group(group).channel(NioSocketChannel.class);
- b.option(ChannelOption.TCP_NODELAY, true);
- b.handler(new ChannelInitializer<SocketChannel>() {
-
- @Override
- protected void initChannel(SocketChannel ch) throws Exception {
- ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
- ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
- ch.pipeline().addLast(new ClientChannelHandler());
- }
- });
- ChannelFuture f = b.connect(host, port);
- f.channel().closeFuture().sync();
- } finally {
- group.shutdownGracefully();
- }
- }
-
- }
三、MarshallingCodeFactory的代码如下:
- package factory;
-
- import org.jboss.marshalling.MarshallerFactory;
- import org.jboss.marshalling.Marshalling;
- import org.jboss.marshalling.MarshallingConfiguration;
-
- import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
- import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
- import io.netty.handler.codec.marshalling.MarshallerProvider;
- import io.netty.handler.codec.marshalling.MarshallingDecoder;
- import io.netty.handler.codec.marshalling.MarshallingEncoder;
- import io.netty.handler.codec.marshalling.UnmarshallerProvider;
-
- public class MarshallingCodeCFactory {
-
- public static MarshallingDecoder buildMarshallingDecoder() {
- final MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");
- final MarshallingConfiguration configuration = new MarshallingConfiguration();
- configuration.setVersion(5);
- UnmarshallerProvider provider = new DefaultUnmarshallerProvider(factory, configuration);
- MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024);
- return decoder;
- }
-
- public static MarshallingEncoder buildMarshallingEncoder() {
- final MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");
- final MarshallingConfiguration configuration = new MarshallingConfiguration();
- configuration.setVersion(5);
- MarshallerProvider provider = new DefaultMarshallerProvider(factory, configuration);
- MarshallingEncoder encoder = new MarshallingEncoder(provider);
- return encoder;
- }
-
- }
四、ClientChannelHandler的代码如下: 客户端与服务端连通后,客户端直接将Student对象写入channelpipeline中
- package demo;
-
- import io.netty.channel.ChannelHandlerAdapter;
- import io.netty.channel.ChannelHandlerContext;
-
- public class ClientChannelHandler extends ChannelHandlerAdapter {
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- Student s = new Student("小红", "5班", 12);
- ctx.writeAndFlush(s);
- }
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
-
- }
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-
- }
- }
五、服务端代码: - package demo;
-
- import factory.MarshallingCodeCFactory;
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
-
- public class Server {
- public static void main(String[] args) {
- try {
- new Server().bind(8888);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void bind(final int port) throws Exception {
- EventLoopGroup bossGroup = new NioEventLoopGroup();
- EventLoopGroup workGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b.option(ChannelOption.TCP_NODELAY, true);
- b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer<SocketChannel>() {
-
- @Override
- protected void initChannel(SocketChannel ch) throws Exception {
- ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
- ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
- ch.pipeline().addLast(new ServerChannelHandler());
- }
-
- });
- ChannelFuture f = b.bind(port).sync();
- System.out.println("服务端已启动");
- f.channel().closeFuture().sync();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- bossGroup.shutdownGracefully();
- workGroup.shutdownGracefully();
- }
- }
-
- }
六、ServerChannelHandler的代码如下: - package demo;
-
- import io.netty.channel.ChannelHandlerAdapter;
- import io.netty.channel.ChannelHandlerContext;
-
- public class ServerChannelHandler extends ChannelHandlerAdapter {
-
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- System.out.println("active");
- }
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- System.out.println(msg);
- }
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- cause.printStackTrace();
- }
-
- }
服务端将输出如下:
- 服务端已启动
- active
- Student [name=小红, classs=5班, age=12]
表明服务端成功接收了客户端发送的Student对象 注意:
作为传输对象的JavaBean必须要继承Serializable接口,如上述例子,如果Student继承了Person,那么Person也必须继承Serializable接口,否则将报错