|
| 1 | +package org.jee.rpc; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.ObjectInput; |
| 5 | +import java.io.ObjectInputStream; |
| 6 | +import java.io.ObjectOutputStream; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.net.ServerSocket; |
| 10 | +import java.net.Socket; |
| 11 | +import java.util.concurrent.Executor; |
| 12 | +import java.util.concurrent.Executors; |
| 13 | + |
| 14 | +/** |
| 15 | + * 描述:RPC服务发布者 |
| 16 | + * Created by bysocket on 16/2/28. |
| 17 | + */ |
| 18 | +public class RpcExporter { |
| 19 | + // 创建线程池 |
| 20 | + static Executor executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
| 21 | + |
| 22 | + public static void exporter(String hostName,int port) throws IOException { |
| 23 | + ServerSocket serverSocket = new ServerSocket(); |
| 24 | + serverSocket.bind(new InetSocketAddress(hostName,port)); |
| 25 | + try { |
| 26 | + while (true) { |
| 27 | + /** |
| 28 | + * 监听Client的TCP连接,将其封装成Task,由线程池执行. |
| 29 | + */ |
| 30 | + executor.execute(new ExporterTask(serverSocket.accept())); |
| 31 | + } |
| 32 | + } finally { |
| 33 | + serverSocket.close(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * 线程Task: |
| 39 | + * 1. 将客户端发送的二进制流反序列化成对象,反射调用服务实现者,获取执行结果 |
| 40 | + * 2. 将执行结果对象反序列化,通过Socket发送给客户端 |
| 41 | + * 3. 远程服务调用完成之后,释放Socket等连接资源,防止句柄泄漏 |
| 42 | + */ |
| 43 | + private static class ExporterTask implements Runnable { |
| 44 | + Socket client = null; |
| 45 | + public ExporterTask(Socket accept) { |
| 46 | + this.client = accept; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void run() { |
| 51 | + ObjectInputStream input = null; |
| 52 | + ObjectOutputStream output = null; |
| 53 | + try { |
| 54 | + // 对象输入流 |
| 55 | + input = new ObjectInputStream(client.getInputStream()); |
| 56 | + |
| 57 | + // 获取接口名 |
| 58 | + String interfaceName = input.readUTF(); |
| 59 | + // 获取方法名 |
| 60 | + String methodName = input.readUTF(); |
| 61 | + // 获取方法的参数数组 |
| 62 | + Class<?>[] paramTypes = (Class<?>[]) input.readObject(); |
| 63 | + // 获取传入参数对象数组 |
| 64 | + Object[] arguments = (Object[]) input.readObject(); |
| 65 | + |
| 66 | + // 获取服务对象类 |
| 67 | + Class<?> service = Class.forName(interfaceName); |
| 68 | + // 获取服务方法 |
| 69 | + Method method = service.getMethod(methodName,paramTypes); |
| 70 | + // 获取服务方法返回对象 |
| 71 | + Object result = method.invoke(service.newInstance(),arguments); |
| 72 | + |
| 73 | + // 对象输出流 |
| 74 | + output = new ObjectOutputStream(client.getOutputStream()); |
| 75 | + output.writeObject(result); |
| 76 | + |
| 77 | + } catch (Exception e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } finally { |
| 80 | + // 关闭流的操作 |
| 81 | + if (output != null) { |
| 82 | + try { |
| 83 | + output.close(); |
| 84 | + } catch (IOException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (input != null) { |
| 90 | + try { |
| 91 | + input.close(); |
| 92 | + } catch (IOException e) { |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (client != null) { |
| 98 | + try { |
| 99 | + client.close(); |
| 100 | + } catch (IOException e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments