java热部署的简单例子
2009-11-09 21:44
295 查看
本文纯粹就是将一篇英文文章的一个段落翻译一下,并将例子跑通。
原文出处:http://java.sun.com/developer/TechTips/2000/tt1027.html
我的目录结构是
d://hotdeploy//Client.java
d://hotdeploy//ServerItf.java
d://hotdeploy//server//ServerImpl.java
文件内容依次为:
//file Client.java
import java.net.URL;
import java.net.URLClassLoader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Client {
static ClassLoader cl;
static ServerItf server;
public static void loadNewVersionOfServer() throws Exception {
URL[] serverURLs = new URL[] { new URL("file://d:/hotdeploy/server/") };
cl = new URLClassLoader(serverURLs);
server = (ServerItf) cl.loadClass("ServerImpl").newInstance();
}
public static void test() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
loadNewVersionOfServer();
while (true) {
System.out.print("Enter QUOTE, RELOAD, GC, or QUIT: ");
String cmdRead = br.readLine();
String cmd = cmdRead.toUpperCase();
if (cmd.equals("QUIT")) {
return;
} else if (cmd.equals("QUOTE")) {
System.out.println(server.getQuote());
} else if (cmd.equals("RELOAD")) {
loadNewVersionOfServer();
} else if (cmd.equals("GC")) {
System.gc();
System.runFinalization();
}
}
}
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------
public interface ServerItf {
public String getQuote();
}
-------------------------------------------------------------------------
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "i love you";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
运行的命令依次为:
D:/hotdeploy>javac Client.java
D:/hotdeploy>javac ServerItf.java
D:/hotdeploy>javac -cp d:/hotdeploy d:/hotdeploy/server/ServerImpl.java
D:/hotdeploy>java Client
ServerImpl class 1641745 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
i love you
Enter QUOTE, RELOAD, GC, or QUIT:
-------------------------------------------------------------------------
编辑ServerImpl.java为:
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "you love me";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
打开另外一个dos窗口,运行javac -cp d:/hotdeploy d:/hotdeploy/server/ServerImpl.java
-------------------------------------------------------------------------
回到原先的doc窗口,依次运行
Enter QUOTE, RELOAD, GC, or QUIT: reload
ServerImpl class 12677476 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
you love me
Enter QUOTE, RELOAD, GC, or QUIT:
可以看到效果。
原文出处:http://java.sun.com/developer/TechTips/2000/tt1027.html
我的目录结构是
d://hotdeploy//Client.java
d://hotdeploy//ServerItf.java
d://hotdeploy//server//ServerImpl.java
文件内容依次为:
//file Client.java
import java.net.URL;
import java.net.URLClassLoader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Client {
static ClassLoader cl;
static ServerItf server;
public static void loadNewVersionOfServer() throws Exception {
URL[] serverURLs = new URL[] { new URL("file://d:/hotdeploy/server/") };
cl = new URLClassLoader(serverURLs);
server = (ServerItf) cl.loadClass("ServerImpl").newInstance();
}
public static void test() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
loadNewVersionOfServer();
while (true) {
System.out.print("Enter QUOTE, RELOAD, GC, or QUIT: ");
String cmdRead = br.readLine();
String cmd = cmdRead.toUpperCase();
if (cmd.equals("QUIT")) {
return;
} else if (cmd.equals("QUOTE")) {
System.out.println(server.getQuote());
} else if (cmd.equals("RELOAD")) {
loadNewVersionOfServer();
} else if (cmd.equals("GC")) {
System.gc();
System.runFinalization();
}
}
}
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------
public interface ServerItf {
public String getQuote();
}
-------------------------------------------------------------------------
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "i love you";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
运行的命令依次为:
D:/hotdeploy>javac Client.java
D:/hotdeploy>javac ServerItf.java
D:/hotdeploy>javac -cp d:/hotdeploy d:/hotdeploy/server/ServerImpl.java
D:/hotdeploy>java Client
ServerImpl class 1641745 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
i love you
Enter QUOTE, RELOAD, GC, or QUIT:
-------------------------------------------------------------------------
编辑ServerImpl.java为:
public class ServerImpl implements ServerItf {
// catch the class being unloaded from the VM
static Object reporter = new Reporter(ServerImpl.class);
public String getQuote() {
return "you love me";
}
}
// file ServerImpl.java. Place this file
// in a subdirectory named 'server'.
class Reporter {
Class cls;
Reporter(Class cls) {
this.cls = cls;
System.out.println("ServerImpl class " + cls.hashCode()
+ " loaded into VM");
}
protected void finalize() {
System.out.println("ServerImpl class " + cls.hashCode()
+ " unloaded from VM");
}
}
-------------------------------------------------------------------------
打开另外一个dos窗口,运行javac -cp d:/hotdeploy d:/hotdeploy/server/ServerImpl.java
-------------------------------------------------------------------------
回到原先的doc窗口,依次运行
Enter QUOTE, RELOAD, GC, or QUIT: reload
ServerImpl class 12677476 loaded into VM
Enter QUOTE, RELOAD, GC, or QUIT: quote
you love me
Enter QUOTE, RELOAD, GC, or QUIT:
可以看到效果。
相关文章推荐
- Java序列化简单的例子
- 最简单的例子告诉你什么是面向对象(java)
- java 使用 comet4j 主动向客户端推送信息 简单例子
- java鬼混笔记:springboot之thymeleaf 1:简单的thymeleaf例子
- 编写存储过程,并且使用java代码调用存储过程的简单例子
- java 接口实现简单并能说明问题的例子
- Java产生死锁的一个简单例子
- Java中JSON格式的简单应用例子
- 《与Java相恋》(高级篇)一简单的java web应用程序部署
- JAVA如何插入MySql的datetime类型的简单的例子
- 最简单的例子理解java回调
- Mule安装与开发部署一个简单例子 推荐
- JavaBean入门 - 概念和简单例子
- java自身的动态代理简单例子。
- iOS使用http post多图片上传带描述+java后台服务器,简单实现例子.
- 关于java中Properties类的简单的使用例子
- 看JAVA序列化的性能 -- 一个简单的例子
- 一个简单的Java序列化的例子
- 转:jsp 调用java简单例子
- java后端实现websocket简单例子