国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java熱部署的簡單例子

2019-11-17 04:05:00
字體:
來源:轉載
供稿:網友
本文純粹就是將一篇英文文章的一個段落翻譯一下,并將例子跑通。

原文出處: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:

可以看到效果。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 车险| 托克逊县| 昌江| 文化| 景宁| 贡山| 武宣县| 大田县| 昆明市| 永济市| 平乐县| 庄浪县| 石门县| 万盛区| 太白县| 萨嘎县| 抚顺县| 都江堰市| 内黄县| 阿拉善盟| 龙海市| 左云县| 安龙县| 灵寿县| 上蔡县| 汉中市| 斗六市| 修水县| 石泉县| 甘肃省| 郧西县| 余干县| 蓬溪县| 蒲江县| 茂名市| 兴和县| 枣阳市| 富顺县| 金沙县| 宁强县| 越西县|