zoukankan      html  css  js  c++  java
  • 简单的网络调用

    自己在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<S> {
    
        @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<HelloService> call = new CallRemote<>();
            
            HelloService hello = call.callRemote(HelloServiceImpl.class, new InetSocketAddress("localhost",8088));
            System.out.println(hello.hello("fenghao"));
        }
    
    }
  • 相关阅读:
    AcWing 1135. 新年好 图论 枚举
    uva 10196 将军 模拟
    LeetCode 120. 三角形最小路径和 dp
    LeetCode 350. 两个数组的交集 II 哈希
    LeetCode 174. 地下城游戏 dp
    LeetCode 面试题 16.11.. 跳水板 模拟
    LeetCode 112. 路径总和 递归 树的遍历
    AcWing 1129. 热浪 spfa
    Thymeleaf Javascript 取值
    Thymeleaf Javascript 取值
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/9627458.html
Copyright © 2011-2022 走看看