http://blog.csdn.net/itleochen/article/details/11270829
自己实现一个服务框架
用java程序自己写一个简单的服务框架
通信协议:socket
网络io:bio
线程方式:无限线程池
远程调用的透明化方案:jdk 动态代理Proxy
序列化:java本身
通信协议:socket
网络io:bio
线程方式:无限线程池
远程调用的透明化方案:jdk 动态代理Proxy
序列化:java本身
贴上核心代码:
一、发布服务方法
- /**
- * 发布服务
- *
- * @param service
- * 服务实现
- * @param port
- * 服务端口
- * @throws Exception
- */
- public static void export(final Object service, final int port)
- throws Exception {
- if (service == null)
- throw new IllegalArgumentException("service instance == null");
- if (port <= 0 || port > 65535)
- throw new IllegalArgumentException("Invalid port " + port);
- System.out.println("Export service " + service.getClass().getName()
- + " on port " + port);
- /* 服务端socket,最多同时处理1000个服务请求 */
- ServerSocket server = new ServerSocket(port, 1000);
- ExecutorService executor = Executors.newCachedThreadPool();
- ;
- for (;;) {
- try {
- // 监听client socket请求
- final Socket socket = server.accept();
- executor.execute(new Runnable() {
- @Override
- public void run() {
- try {
- try {
- ObjectInputStream input = new ObjectInputStream(
- socket.getInputStream());
- try {
- // 发布服务的方法名
- String methodName = input.readUTF();
- // 入参
- Class<?>[] parameterTypes = (Class<?>[]) input
- .readObject();
- Object[] arguments = (Object[]) input
- .readObject();
- ObjectOutputStream output = new ObjectOutputStream(
- socket.getOutputStream());
- try {
- Method method = service.getClass()
- .getMethod(methodName,
- parameterTypes);
- Object result = method.invoke(service,
- arguments);
- output.writeObject(result);
- } catch (Throwable t) {
- output.writeObject(t);
- } finally {
- output.close();
- }
- } finally {
- input.close();
- }
- } finally {
- if (socket != null) {
- socket.close();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
二、调用服务
- /**
- * 引用服务
- *
- * @param <T>
- * 接口泛型
- * @param interfaceClass
- * 接口类型
- * @param host
- * 服务器主机名
- * @param port
- * 服务器端口
- * @return 远程服务
- * @throws Exception
- */
- @SuppressWarnings("unchecked")
- public static <T> T refer(final Class<T> interfaceClass, final String host,
- final int port) throws Exception {
- if (interfaceClass == null)
- throw new IllegalArgumentException("Interface class == null");
- if (!interfaceClass.isInterface())
- throw new IllegalArgumentException("The "
- + interfaceClass.getName() + " must be interface class!");
- if (host == null || host.length() == 0)
- throw new IllegalArgumentException("Host == null!");
- if (port <= 0 || port > 65535)
- throw new IllegalArgumentException("Invalid port " + port);
- System.out.println("Get remote service " + interfaceClass.getName()
- + " from server " + host + ":" + port);
- return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
- new Class<?>[] { interfaceClass }, new InvocationHandler() {
- public Object invoke(Object proxy, Method method,
- Object[] arguments) throws Throwable {
- // 建立socket并尝试连接,失败则抛出异常
- // Socket socket = new Socket(host, port);
- Socket socket = new Socket();
- // socket.close()执行时 若数据没有发送完成则阻塞 10秒
- socket.setSoLinger(true, 10);
- socket.connect(new InetSocketAddress(host, port));
- try {
- // 往socket写数据
- ObjectOutputStream output = new ObjectOutputStream(
- socket.getOutputStream());
- try {
- output.writeUTF(method.getName());
- output.writeObject(method.getParameterTypes());
- output.writeObject(arguments);
- // 读出数据
- ObjectInputStream input = new ObjectInputStream(
- socket.getInputStream());
- try {
- Object result = input.readObject();
- if (result instanceof Throwable) {
- throw (Throwable) result;
- }
- return result;
- } finally {
- input.close();
- }
- } finally {
- if (output != null) {
- output.close();
- }
- }
- } finally {
- if (socket != null) {
- socket.close();
- }
- }
- }
- });
- }
没法传附件么?