之前BEA用遇到的有关问题~Java

之前BEA用遇到的问题~~~Java
1. 
http://m.blog.csdn.net/blog/ak47wjs/7454516

[原]JNLP 客户端不缓存及jar包的版本号问题

1.JNLP时实时更新问题

        根据我的测试及网上看到的文章了解到,客户端java缓存JNLP是根据URL及response反回的Last-Modified(即最后一次修改时间)来缓存的(具体位置在C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Sun\\Java\\Deployment\\cache\\6.0中的某个文件夹下)。我用的是servlet生成JNLP文件的所以可以在servlet中增加如下代码:

        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setHeader("Expires", "0");
        resp.setContentType("application/x-java-jnlp-file");
        ((HttpServletResponse)resp).setDateHeader("Last-Modified", System.currentTimeMillis());     //这条语句是JNLP更新的核心

你可以做这样一个测试,将缓存的JNLP文件更改一下,你的applet将无法启动。

2.jar包的版本号问题

         jar包可以利用Last-Modified进行实时更新,但有时候没有这个必要,因为每次去下载jar包的话会很慢,特别是jar包比较多的时候,更何况我们还可能会利用JNLP离线运行的特点,所以我的jar包是要缓存的,但如何更新呢?这就是jar包的版本问题了。

         (1)首选来说说JNLP中的jar包版本号的实现,见代码

                   <jar href="my.jar" main="true" version=" 1.0.1"  size="13813329" download="lazy" />

           (2)   html中<Applet>标签中的jar包版本号的实现,在<Applet>标签中加入如下代码:

                   <PARAM NAME="cache_archive" VALUE="my.jar">
                   <PARAM NAME="cache_version" VALUE="1.0.1">

          这样就可以实现jar包版本的管理了。

顺便说一下JNLP的离线远行只需在<information>标签中加入<offline-allowed/>标签即可.

如有不对之此或有更好的方法请指教,谢谢。

=======================================

2.
http://hllvm.group.iteye.com/group/topic/28230?page=2
System.exit(0) 无法退出JVM.有没有好的办法定位问题?

3.
http://hllvm.group.iteye.com/group/topic/28988
[讨论] System.exit(0) 退出进程的疑惑

4.
http://tianya23.blog.51cto.com/1081650/247041
System.exit

System.exit    
  public   static   void   exit(int&nbsp;status)  
  Terminates   the   currently   running   Java   Virtual   Machine.    
  The   argument   serves   as   a   status   code;    by   convention,   a   nonzero   status   code   indicates   abnormal   termination.    This   method   calls   the   exit   method   in   class   Runtime.    
  This   method   never   returns   normally.    
  The   call   System.exit(n)   is   effectively   equivalent   to   the   call:      
  Runtime.getRuntime().exit(n)
  => 对于java程序,运行System.exit()会终止JVM, 0表示正常退出,非0表示异常退出

举个例子,在bat里(但愿您会batch)  
   
  java   abc.HelloWorld  
   
  ECHO   exit=%ERRORLEVEL%  
   
  IF   ERRORLEVEL   0   ECHO   正常结束,或者调用了System.exit(0)  
  IF   ERRORLEVEL   1   ECHO   System.exit(1)  
   
  其实两者都不是“正常”退出  
  try   {  
      System.exit(0);   //试试return或者throw  
  }   finally   {  
      System.out.println("!!!!!");   //这行,无论return,   throw都会执行,但是System.exit却不是  
  }  

=========================================

5.
http://blog.csdn.net/tangwing/article/details/5764578
Java应用程序添加退出事件响应 (关闭虚拟机而不是程序-system.exit)

一个完整的Java应用程序,通常至少要有一个应用程序的结束点。对于一般程序来说,系统开发者根据需要和个人的偏好,会在程序结束位置,通过添加System.exit(0),或System.out(-1),来结束程序,或不加这些指令,让程序自然运行到结束。

  如:下列典型代码



package untitled14;

/**
* This application is to demo how an applcation end
*/
public class Test {
 public Test() {}
 public static void main(String[] args) {
  Test test1 = new Test();
  //.................
  System.out.println("hello world");
  //Do something before system exit
  System.exit(0);//也可以不写这句代码,让程序自然结束。
 }
}

  对于简单的应用系统,我们直接可以在System.exit(0)代码执行前,添加需要在应用程序退出前需要完成的工作,如:关闭网络连接,关闭数据库连接等。

  然而,对于比较复杂的多线程应用,线程运行的状态较复杂,我们就很难预料程序何时结束,如何能在应用程序结束事件到来时,处理我们要做的工作呢?这就用到了Java对应用程序的退出的事件出处理机制。

  对当前应用程序对象的获得,Java通过Runtime静态方法:Runtime.getRuntime()通过Runtime的 void addShutdownHook(Thread hook) 法向Java虚拟机注册一个shutdown钩子事件,这样一旦程序结束事件到来时,就运行线程hook,我们在实际应用时候,只要将程序需要完成之前做的一些工作直接通过线程hook来完成。具体演示代码如下:

/*****************************************************************************
本程序仅演示,如何在Java应用程序中添加系统退出事件处理机制
*****************************************************************************/
package untitled14;
import java.util.*;
import java.io.*;

/**
* This application is used to demo how to hook the event of an application
*/
public class Untitled1 {

 public Untitled1() {
  doShutDownWork();
 }

 /***************************************************************************
 * This is the right work that will do before the system shutdown
 * 这里为了演示,为应用程序的退出增加了一个事件处理,
 * 当应用程序退出时候,将程序退出的日期写入 d:/t.log文件
 **************************************************************************/
 private void doShutDownWork() {
  Runtime.getRuntime().addShutdownHook(new Thread() {

   public void run() {
    try {
     FileWriter fw = new FileWriter("d://t.log");
     System.out.println("Im going to end");
     fw.write("the application ended! " + (new Date()).toString());
     fw.close();
    }
    catch (IOException ex) {
    }

   }
  });
 }

 /****************************************************
 * 这是程序的入口,仅为演示,方法中的代码无关紧要
 ***************************************************/

 public static void main(String[] args) {
  Untitled1 untitled11 = new Untitled1();
  long s = System.currentTimeMillis();
  for (int i = 0; i < 1000000000; i++) {
   //在这里增添您需要处理代码
  }
  long se = System.currentTimeMillis();
  System.out.println(se - s);
 }
}

  在上述程序中,我们可以看到通过在程序中增加Runtime.getRuntime().addShutdownHook(new Thread()) 事件监听,捕获系统退出消息到来,然后,执行我们所需要完成工作,从而使我们的程序更健壮!