博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的网络调用
阅读量:5291 次
发布时间:2019-06-14

本文共 5617 字,大约阅读时间需要 18 分钟。

自己在github上找的项目,是springforall社区的项目,自己写写,练练手

package com.fh.rpc;public interface HelloService {        public String hello(String message);}
package com.fh.rpc;public class HelloServiceImpl implements HelloService{    @Override    public String hello(String message) {        return message+",it is Ok";    }    }
package com.fh.rpc;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.Method;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Publish {    static ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());        public static void publish(String host,int port) throws IOException {        ServerSocket server = new ServerSocket();        server.bind(new InetSocketAddress(host, port));        try {            while(true) {                pool.execute(new ExecutorTask(server.accept()));                }        }catch (Exception e) {            e.printStackTrace();        }finally {            server.close();        }    }        private static class ExecutorTask implements Runnable{                public Socket socket;                public ExecutorTask(Socket socket) {            this.socket=socket;        }        @Override        public void run() {            ObjectInputStream input = null;            ObjectOutputStream output = null;            try {                input = new ObjectInputStream(socket.getInputStream());                                String interfaceName = input.readUTF();                String methodName = input.readUTF();                Class
[] paramTypes = (Class
[])input.readObject(); Object[] arguments = (Object[])input.readObject(); Class
service = Class.forName(interfaceName); Method method = service.getMethod(methodName, paramTypes); Object result = method.invoke(service.newInstance(), arguments); output = new ObjectOutputStream(socket.getOutputStream()); output.writeObject(result); }catch (Exception e) { e.printStackTrace(); }finally { if(output!=null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } if(input!=null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }}
package com.fh.rpc;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.net.InetSocketAddress;import java.net.Socket;public class CallRemote {    @SuppressWarnings("unchecked")    public S callRemote(final Class
serviceClass,final InetSocketAddress address) { return (S)Proxy.newProxyInstance(serviceClass.getClassLoader(), new Class
[]{serviceClass.getInterfaces()[0]}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Socket socket = null; ObjectOutputStream output = null; ObjectInputStream input = null; try { socket = new Socket(); socket.connect(address); output = new ObjectOutputStream(socket.getOutputStream()); output.writeUTF(serviceClass.getName()); output.writeUTF(method.getName()); output.writeObject(method.getParameterTypes()); output.writeObject(args); input = new ObjectInputStream(socket.getInputStream()); return input.readObject(); }catch (Exception e) { e.printStackTrace(); return null; }finally { if(socket!=null) { socket.close(); } if(input!=null) { input.close(); } if(output!=null) { output.close(); } } } }); }}
package com.fh.rpc;import java.io.IOException;import java.net.InetSocketAddress;public class RpcTest {    public static void main(String[] args) {        new Thread(new Runnable() {                        @Override            public void run() {                try {                    Publish.publish("localhost", 8088);                } catch (IOException e) {                    e.printStackTrace();                }            }        }).start();                CallRemote
call = new CallRemote<>(); HelloService hello = call.callRemote(HelloServiceImpl.class, new InetSocketAddress("localhost",8088)); System.out.println(hello.hello("fenghao")); }}

 

转载于:https://www.cnblogs.com/nihaofenghao/p/9627458.html

你可能感兴趣的文章
随机变量的期望为什么把不是自己密度函数当成自己的权重来求期望呢?
查看>>
6-1 并行程序模拟 uva210
查看>>
JavaScript动画打开半透明提示层
查看>>
Mybatis生成resulteMap时的注意事项
查看>>
jquery-jqzoom 插件 用例
查看>>
javascript知识点记录01
查看>>
javascript事件代理
查看>>
es6 新增特性
查看>>
1007. Maximum Subsequence Sum (25)
查看>>
《算法》C++代码 快速排序
查看>>
iframe的父子层跨域 用了百度的postMessage()方法
查看>>
Js apply方法与call方法详解 附ES6新写法
查看>>
linux php全能环境一键安装,小白福利!
查看>>
Note(2): 一个JavaScript的贷款计算器
查看>>
js原型和原型链
查看>>
AJAX需要注意的
查看>>
ubuntu下中文乱码解决方案
查看>>
ES6 随记(3.4.1)-- 函数的拓展(参数默认值,扩展运算符)
查看>>
MSSQL 分组后取每组第一条(group by order by)
查看>>
图片生成缩略图
查看>>