类加载器实现简单远程任务执行器示意代码(学习版) - - JavaEye技术网站
本文所贴示意代码主要是学习使用。
任务接口:
package cs.classloader;
/**
 * 任务类接口 ,任何实现了此接口的类,都可以在服务端简单任务执行器中执行
 * */
public interface TaskIntf {

	void execute();
}

任务实现类:
package cs.classloader;

public class TaskIntfImp implements TaskIntf {
	
	static {
		System.out.println("我被加载了");
	}

	@Override
	public void execute() {
		System.out.println("我被执行了   我被"+this.getClass().getClassLoader()+"加载");
	}

}

类加载器:
package cs.classloader;

import java.io.File;
import java.io.FileInputStream;
/**
 * 自定义类加载器
 * */
public class NewClassLoader extends ClassLoader {
	private String currentRoot = null;

	public NewClassLoader(String userdir) {
		this.currentRoot = userdir;
	}

	public NewClassLoader() {
	}
	//根据类名从文件中读取类字节码
	public byte[] findClassBytes(String className) {
		if (currentRoot == null) {
			currentRoot = "";
		}
		try {
			String pathName = currentRoot + File.separatorChar
					+ className.replace('.', File.separatorChar) + ".class";
			FileInputStream inFile = new FileInputStream(pathName);
			byte[] classBytes = new byte[inFile.available()];
			inFile.read(classBytes);
			return classBytes;
		} catch (java.io.IOException ioEx) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public Class findClass(String name) throws ClassNotFoundException {

		byte[] classBytes = findClassBytes(name);
		if (classBytes == null) {
			throw new ClassNotFoundException();
		} else {
			return defineClass(name, classBytes, 0, classBytes.length);
		}
	}
	//根据传入的字节码 返回class信息
	@SuppressWarnings("unchecked")
	public Class findClass(String name, byte[] classBytes)
			throws ClassNotFoundException {

		if (classBytes == null) {
			throw new ClassNotFoundException("(classBytes==null)");
		} else {
			return defineClass(name, classBytes, 0, classBytes.length);
		}
	}
	//执行 实现了TaskIntf 接口的 任务类
	@SuppressWarnings("unchecked")
	public void execute(String codeName, byte[] code) {

		Class klass = null;
		try {
			klass = findClass(codeName, code);
			TaskIntf task = (TaskIntf) klass.newInstance();
			task.execute();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}
}

服务端代码:
package cs.classloader;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 服务端类,实现监听客户端请求
 * */
public class Server {

	private ServerSocket ss = null;
	private int port = 8067;
	private Socket sc = null;

	public Server() {
		try {
			ss = new ServerSocket(port);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listener() {
		while (true) {
			try {
				sc = ss.accept();
				Thread t = new Thread(new TaskDo(sc));//交给任务处理线程处理
				t.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		Server s = new Server();
		s.listener();
	}
}
/**
 * 此类主要是读取客户端传来的字节码,并实现任务执行
 * */
class TaskDo implements Runnable {
	Socket s;
	InputStream is;
	OutputStream os;
	BufferedInputStream bis;
	byte[] b = null;

	public TaskDo(Socket s) {
		this.s = s;
		try {
			os = s.getOutputStream();
			is = s.getInputStream();
			bis = new BufferedInputStream(is);
			b = new byte[bis.available()];
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try {
			read(b);
			execute(null, b);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
				is.close();
				s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private void read(byte[] b) throws Exception {
		bis.read(b);
	}

	private void execute(String codeName, byte[] code) {

		NewClassLoader fileSystemClassLoader = null;

		try {
			fileSystemClassLoader = new NewClassLoader();
			fileSystemClassLoader.execute(codeName, code);
		} catch (Exception exception) {
		}
	}
}

客户端代码:
package cs.classloader;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/**
 * 客户端类,负责读取实现了TaskIntf接口的类的字节码,并发送给服务端
 * */
public class Client {
	public static void main(String[] args) {

		try {
			for (int i = 0; i < 2; i++) {
				byte[] code = getClassDefinition("cs.classloader.TaskIntfImp");
				send(code);
			}
		} catch (Exception ex) {
		}
	}

	private static byte[] getClassDefinition(String codeName) {
		String userDir = System.getProperty("user.dir") + "\\bin";
		NewClassLoader fscl1 = null;

		try {
			fscl1 = new NewClassLoader(userDir);
		} catch (Exception fileNotFoundException) {
			fileNotFoundException.printStackTrace();
		}
		return fscl1.findClassBytes(codeName);
	}

	private static void send(byte[] b) {
		OutputStream os = null;
		Socket s = null;
		try {
			s = new Socket("127.0.0.1", 8067);
			os = s.getOutputStream();
			os.write(b);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				os.close();
				s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

此示意代码主要是从客户端发送实现了特定接口的任务类的字节码发送给服务端执行.
郑重声明:资讯 【类加载器实现简单远程任务执行器示意代码(学习版) - - JavaEye技术网站】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——