一、UML图
抽空再画
二、可行性验证的示意代码
public class TaskManager {
public static void main(String[] args) throws Exception {
ClassLoader mycl = MyClassLoader.getAnotherSysClassLoader();
System.out.println("========invoke directly================");
TestBean.main(args);
System.out.println("========invoke by myClassLoader========");
Class testBeanClazz = mycl
.loadClass("com.alibaba.china.industry.task.common.TaskLoader");
Object obj = testBeanClazz.newInstance();
Method[] ms = testBeanClazz.getMethods();
Method m = getInvokeMethod(mycl, testBeanClazz);
if (m == null) {
System.out.println("error!");
return;
}
Object target = obj;
if(Modifier.isPublic(m.getModifiers())){
m.invoke(Modifier.isStatic(m.getModifiers())?testBeanClazz:obj, new Object[] { args });
}
System.out.println("========invoke directly 2================");
TestBean.main(args);
}
private static Method getInvokeMethod(ClassLoader cl, Class clazz)
throws ClassNotFoundException {
Method[] ms = clazz.getMethods();
Method m = null;
for (Method method : ms) {
Class annClass = cl
.loadClass("com.alibaba.china.industry.task.common.InvokeMethod");
if (method.isAnnotationPresent(annClass)) {
m = method;
}
}
return m;
}
private static class MyClassLoader extends URLClassLoader {
private static URLClassLoader scl = (URLClassLoader) getSystemClassLoader();
private MyClassLoader() {
super(scl.getURLs(), null);
}
static ClassLoader getAnotherSysClassLoader() {
return new MyClassLoader();
}
}
}
public class TaskLoader {
/**
* @param args
*/
@InvokeMethod
public static void main(String[] args) {
System.out.println("TestBean classLoader:"+TaskLoader.class.getClassLoader());
System.out.println("String classLoader:"+String.class.getClassLoader());
}
}
public class Server {
private ServerSocket server;
private static final int ServerPort = 8765;// 要監控的port
public static void main(String[] args){
Server server = new Server();
server.listen();
}
public Server() {
try {
server = new ServerSocket(ServerPort);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void listen() {
TaskDirector taskManager = null;
Socket socket;
java.io.BufferedInputStream in;
System.out.println("server is already!");
while (true) {
socket = null;
try {
synchronized (server) {
socket = server.accept();
}
in = new java.io.BufferedInputStream(socket.getInputStream());
byte[] b = new byte[1024];
String data = "";
in.close();
in = null;
socket.close();
taskManager = new TaskDirector();
taskManager.start();
} catch (Exception e) {
}
}
}
public static class TaskDirector extends Thread {
TaskDirector() {
super();
setDaemon(true);
}
public void run() {
try {
TaskManager.main(null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Client {
private String address = "127.0.0.1";// 連線的ip
private int port = 8765;// 連線的port
public Client() {
Socket client = new Socket();
InetSocketAddress isa = new InetSocketAddress(this.address, this.port);
try {
client.connect(isa, 10000);
BufferedOutputStream out = new BufferedOutputStream(client
.getOutputStream());
// 送出字串
out.write("Send From Client ".getBytes());
out.flush();
out.close();
out = null;
client.close();
client = null;
} catch (java.io.IOException e) {
System.out.println("Socket連線有問題 !");
System.out.println("IOException :" + e.toString());
}
}
/**
* @param args
*/
public static void main(String[] args) {
new Client();
}
}