service providers
Service Interface:
public interface HelloService { public String sayHello(String name); }
Service implementation class:
public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "hello:" + name; } }
Service Registration:
public class ProviderReflect { private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<Runnable>(1000), new MyThreadFactory("providerReflact")); /** * 服务发布 * * @param service * @param port */ public static void provider(final Object service, int port) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); while (true) { Socket socket = serverSocket.accept(); executor.execute(new Runnable() { @Override public void run() { ObjectOutputStream oos = null; ObjectInputStream ois = null; try { ois = newThe ObjectInputStream (Socket.getInputStream ()); // request to invoke a method name String methodName = ois.readUTF (); // request parameter Object [] args = (Object []) ois.readObject (); OOS = new new the ObjectOutputStream (Socket.getOutputStream ()); Object result = MethodUtils.invokeExactMethod (-Service, methodName, args); System.out.println (result); // write result oos.writeObject (result); } the catch (IOException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (oos != null) { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } if (ois != null) { try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } }); } } catch (IOException e) { e.printStackTrace(); } finally { if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } static class MyThreadFactory the implements{A ThreadFactory / ** * name prefix threads in the thread pool * / Private String namePrefix; / ** * atoms integer * / Private of AtomicInteger AtomicInteger = new new of AtomicInteger (. 1 ); public MyThreadFactory (String whatFeaturOfGroup) { the this .namePrefix = "MyThreadFactory the From:" + + whatFeaturOfGroup "-worker-" ; } @Override public the Thread newthread (the Runnable R & lt) { String name = namePrefix + atomicInteger.getAndIncrement (); the Thread Thread = new new Thread(r, name); System.out.println(thread.getName()); return thread; } } /** * 发布服务 * * @param args */ public static void main(String[] args) { HelloService helloService = new HelloServiceImpl(); ProviderReflect.provider(helloService, 8082); } }
pom.xml:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency>
Consumer Services
Consumer Interface:
public interface HelloService { public String sayHello(String name); }
Interface agents:
public class ConsumerProxy { public static <T> T consume(final Class<?> serviceClass, final String host, final int port) { return (T) Proxy.newProxyInstance(serviceClass.getClassLoader(), new Class<?>[]{serviceClass}, 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(host, port); 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(); } finally { if (socket != null) { socket.close(); } if (output != null) { output.close(); } if (input != null) { input.close(); } } } }); } public static void main(String[] args) { HelloService helloService = ConsumerProxy.consume(HelloService.class, "127.0.0.1", 8082); String response = helloService.sayHello("yangyongjie"); System.out.println(response); } }
Output:
hello: yangyongjie
Reproduced in: https: //www.cnblogs.com/yangyongjie/p/11081116.html