码迷,mamicode.com
首页 > 其他好文 > 详细

简单的网络调用

时间:2018-09-11 14:49:09      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:override   cat   adp   ble   return   oca   process   练手   throwable   

自己在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"));
    }

}

 

简单的网络调用

标签:override   cat   adp   ble   return   oca   process   练手   throwable   

原文地址:https://www.cnblogs.com/nihaofenghao/p/9627458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!