Hessian远程调用(基于Netty)

 

public class HessianMethodInvocation implements InvocationHandler, Serializable {

	private static final long serialVersionUID = 7304512179586775133L;
	
	private TestServiceImpl testService;
	private HessianSkeleton skeleton;
	
	private AbstractHessianInput input;
	private AbstractHessianOutput output;
	
	public HessianMethodInvocation(AbstractHessianInput input, AbstractHessianOutput output) {
		testService = new TestServiceImpl();
		skeleton = new HessianSkeleton(testService, TestService.class);
		
		this.input = input;
		this.output = output;
	}
	
	public void invoke() {
		try {
			skeleton.invoke(input, output);
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		return null;
	}

}

 

 

public class HessianDecoder extends ByteToMessageDecoder {

	private final ClassResolver classResolver;
	
	private static TestServiceImpl testService;
	
	private static HessianSkeleton skeleton;
	
	static {
		testService = new TestServiceImpl();
		
		skeleton = new HessianSkeleton(testService, TestService.class);
	}
	
	public HessianDecoder(ClassResolver classResolver) {
        this(1048576, classResolver);
    }
	
	public HessianDecoder(int maxObjectSize, ClassResolver classResolver) {
        super();
        this.classResolver = classResolver;
    }

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		int length = in.readInt();
		
		ByteBuf frame = Unpooled.buffer(length);
		in.readBytes(frame);
        if (frame == null) {
            return;
        }
        
        ByteArrayInputStream is = new ByteArrayInputStream(frame.array());
		
		
		OutputStream osToUse = new ByteArrayOutputStream();
		
		AbstractHessianInput input = new HessianInput(is);
		AbstractHessianOutput output = new HessianOutput(osToUse);
		
		HessianMethodInvocation invocation = new HessianMethodInvocation(input, output);
        if (invocation != null) {
            out.add(invocation);
        }
	}
}

 

 

            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
//                if (sslCtx != null) {
//                    p.addLast(sslCtx.newHandler(ch.alloc()));
//                }
//                p.addLast(new LoggingHandler(LogLevel.INFO));
                p.addLast(
                    new HessianDecoder(ClassResolvers.cacheDisabled(null)),
                    new HessianMethodInvocationHandler());
            }

 

 

class HessianMethodInvocationHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
    	System.out.println(msg.getClass().getName());
    	if (! (msg instanceof HessianMethodInvocation)) {
    		
    	}
    	HessianMethodInvocation invocation = (HessianMethodInvocation) msg;
    	invocation.invoke();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}

 

 

public class HessianMethodInvocationProxy implements InvocationHandler, Serializable {

	private static final long serialVersionUID = 6587104138220238229L;
	
	private String method;
	private Object[] args;
	
	public HessianMethodInvocationProxy(String method, Object[] args) {
		this.method = method;
		this.args = args;
	}
	
	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public Object[] getArgs() {
		return args;
	}

	public void setArgs(Object[] args) {
		this.args = args;
	}

	public void invoke(ChannelHandlerContext ctx) {
		ctx.writeAndFlush(this);
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		return null;
	}

}

 

 

public class HessianEncoder extends MessageToByteEncoder<Serializable> {

	private static final byte[] LENGTH_PLACEHOLDER = new byte[4];
	
	@Override
	protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
		if (! (msg instanceof HessianMethodInvocationProxy)) {
			
		}
		HessianMethodInvocationProxy proxy = (HessianMethodInvocationProxy) msg;
		int startIdx = out.writerIndex();

        ByteBufOutputStream bout = new ByteBufOutputStream(out);
//        ObjectOutputStream oout = null;
        try {
            bout.write(LENGTH_PLACEHOLDER);
            
            ByteArrayOutputStream osToUse = new ByteArrayOutputStream();
			AbstractHessianOutput output = new HessianOutput(osToUse);
			try {
				output.call(proxy.getMethod(), proxy.getArgs());
			} catch (Throwable e) {
				e.printStackTrace();
			}
			bout.write(osToUse.toByteArray());
        } finally {
                bout.close();
        }

        int endIdx = out.writerIndex();

        out.setInt(startIdx, endIdx - startIdx - 4);
	}

}

 

 

				public void initChannel(SocketChannel ch) throws Exception {
					ChannelPipeline p = ch.pipeline();
//					if (sslCtx != null) {
//						p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
//					}
					p.addLast(new HessianEncoder());
					p.addLast(new HessianMethodInvocationHandlerProxy());
				}

 

 

class HessianMethodInvocationHandlerProxy extends ChannelInboundHandlerAdapter {

	/**
	 * Creates a client-side handler.
	 */
	public HessianMethodInvocationHandlerProxy() {
		//TODO
	}


	@Override
	public void channelActive(ChannelHandlerContext ctx) {
		HessianMethodInvocationProxy proxy = new HessianMethodInvocationProxy("test", new Object[] {"helloworld1234567"});
		proxy.invoke(ctx);
	}


	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) {
		System.out.println(msg);
	}


	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) {
		ctx.flush();
	}


	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		// Close the connection when an exception is raised.
		cause.printStackTrace();
		ctx.close();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值