Java中Web程序修改配置文件不重启服务器的方法

来源:互联网 发布:python字符串转二进制 编辑:程序博客网 时间:2024/05/16 04:38

见:http://blog.sina.com.cn/s/blog_69398ed9010191jg.html

另:http://ekisstherain.iteye.com/blog/1701463



jrebel 、JavaRebel是什么,见另一博客:jrebel/JavaRebel



开发环境

1.  JDK

2.  MyEclipse

3.  Tomcat

4.  Struts2

5.  JavaRebel/JRebel

 

第一步:修改struts.properties来实现热加载Struts2的配置文件


(注:开发环境是在Struts2下)

在src目录下新建一个文件struts.properties,打开编辑,加入以下语句

#Whether Struts is in development mode or not

struts.devMode=true

#Whether the localization messages should automatically be reloaded

struts.i18n.reload=true

#Whether to reload the XML configuration or not

struts.configuration.xml.reload=true

上面的语句分别为是否为struts开发模式、是否国际化信息自动加载、是否加载xml配置(true,false),修改后重启服务器后,就能体现效果。在我们修改Struts2的配置信息的时候就不需再重启服务器了,而且Struts2的配置还包括include标签(防止配置文件膨胀),例如下面的配置:

 

    "struts-back.xml">

    "struts-custom.xml">

上面的struts.properties会对每个配置文件都产生作用。

对于上面的测试,我实际上发现:上面的配置在纯Struts2开发中,是可以实现对配置文件和国际化文件热加载功能的,但我在SSH开发中并不能实现功能,而只能实现对国际化的热加载,不能对配置文件的热加载。

 

第二步:解决类的热加载

 

方法一:修改MyEclipse 中的Tomcat配置,加入JRebel的应用

既然要使用JRebel,那就要我们下载JRebel,到JRebel的官网上面下载最新的JRebel包,需要说明的是JRebel并非免费的产品,提供30天的试用期限,不过网上可以搜索到破解版的哟。

下载后解压,就可以看到jrebel.jar了。

使用配置?

Jrebel的使用一般都依赖于服务器,就那tomcat来说,主要是配置JDK的Optional Java VM arguments(打开Myeclipse中的windows->Preferences->MyEclipse …->Servers->Tomcat->

选择配置的Tomcat,单击,展开JDK选项),加入下面的语句

-noverify -javaagent:D:\Jars\jrebel-2.1a\jrebel.jar

说明一下,“-noverify -javaagent:”是固定的,后面加上的是jrebel.jar在你电脑上面的物理路径,请修改为你的电脑配置,不然不会成功。

这一步是为了解决Java类文件热加载的问题,实际上,有一个更简单的方法吧,并不需要使用JRebel包,而是我们进行适当的配置

 

方法二:

自动加载修改后的项目不需重启服务器(只对Java文件,对配置文件不可以)

在WebRoot下的META-INF文件夹中新建一个名为context.xml文件,在里面写

"true">

注意大小写

方法二的思想是源自张孝祥老师的讲解,不需要修改tomcat的配置,减少tomcat的启动时间,最重要的是,能够完成一样的作用——对Java类进行热加载,但是有的时候,特别是在SSH开发中,会出现异常,建议不要使用这种方法,而是使用第一种方法使用JRebel。

 

这两步下来, 可以在新加入类以及Struts配置修改后, 完全无需重启或者重新发布即可立即测试! 在JAR包暴多的情况下, 可以让我们不再等待10到20秒了。

 

第三步:测试是否有效


测试类的热加载(具体的测试请大家完成,我仅贴出我的测试结果)

我在一个已知的登录系统中,加入一个自己留的后台用户,不重启服务器登录

if("TestJRebel".equals(operator.getOperatorName()))

       {

           System.out.println("通过自己留后台用户TestJRebel登录,大家不要这样哦。");

           return true;

       }




另附:jrebel热部署配置


1. 配置tomcat服务器:修改tomcat的部署配置为:从不自动发布,禁用Web模块的自动从新装入为:禁用


2. 在tomcat运行配置上添加vm参数:-noverify -javaagent:D:\RUNTIME\jrebel\jrebel.jar -Dmyproject.root=D:/project/operamasks/workspaces/xxxx -Drebel.disable_update=true
   其中:-javaagent的值表示jrebel的jar包路径
   -Dmyproject.root的值表示当前的需要热部署的工程路径


3. 在工程的src源文件夹下添加名称为:rebel.xml的配置文件,内容格式如下:

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://www.zeroturnaround.com/alderaan/rebel-2_0.xsd">  
  3.     <classpath>  
  4.         <dir name="${myproject.root}/bin">  
  5.         </dir>  
  6.         <dir name="${myproject.root}/web/WebContent/WEB-INF/classes">  
  7.         </dir>  
  8.     </classpath>  
  9.     <web>  
  10.         <link target="/">  
  11.             <dir name="${myproject.root}/web/WebContent">  
  12.             </dir>  
  13.         </link>  
  14.     </web>  
  15. </application>  

 

 

其中:myproject.root就是tomcat vm参数中指定的工程路径,你也可以使用绝对路径
这个文件定义的就是jrebel要监控的具体文件夹,即class文件和资源文件(比如:jsp等)



4.最后,启动tomcat服务器的成功提示:

 

Fri Oct 19 10:34:29 CST 2012 com.zeroturnaround.javarebel.hD#new V( false )
Fri Oct 19 10:34:29 CST 2012 com.zeroturnaround.javarebel.bH#public boolean a(byte abyte0[])

#############################################################

 JRebel 3.0-M1 (200910151623)
 (c) Copyright ZeroTurnaround, Ltd, 2007-2009. All rights reserved.

 A rough estimate: Over the last 5 days JRebel 
 prevented the need for at least 36 redeploys/restarts.
 Using industry standard build and redeploy times, 
 JRebel saved you between 1 and 2 hours.

 This product is licensed to  Java Hack Organization
 for unlimited number of developer seats on site.

#############################################################


JRebel: A newer version '5.0.1' is available for download 
JRebel: from http://www.zeroturnaround.com/download.

2012-10-19 10:34:29 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\RUNTIME\java\jdk1.6.0_11\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/RUNTIME/java/jdk1.6.0_11/bin/../jre/bin/client;D:/RUNTIME/java/jdk1.6.0_11/bin/../jre/bin;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;D:\RUNTIME\java\jdk1.6.0_11\bin;D:\RUNTIME\java\jdk1.6.0_11\jre\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;D:\RUNTIME\MySQL\MySQL Server 7.0\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\MacType;C:\PROGRA~1\DISKEE~1\DISKEE~1\;d:\Program Files (x86)\DBank\ClickUp;C:\Program Files (x86)\DBank\ClickUp
2012-10-19 10:34:29 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:XXX' did not find a matching property.
2012-10-19 10:34:29 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2012-10-19 10:34:29 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 324 ms
2012-10-19 10:34:29 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2012-10-19 10:34:29 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.35
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\bin' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent' will be monitored for changes.

=============================== [JRebel Spring Framework Plugin] ===============================
Plugins are contributed by third party and can cause compatibility problems.
If you have any troubles set -Drebel.spring_plugin=false to disable it.
------------------------------------------------------------------------------------------
Description: Supports adding new beans and adding new bean dependencies using
annotations or XML. Singletons will be reconfigured after the change. It also
supports adding or changing Spring MVC controllers or handlers.
=============================== [/JRebel Spring Framework Plugin] ==============================


=============================== [JRebel AspectJ Plugin DISABLED] ==============================
You can enable AspectJ Plugin by setting -Drebel.aspectj_plugin=true.
------------------------------------------------------------------------------------------
Description: Allows the AspectJ load-time weaving (javaagent launched by aj5 or
-javaagent:aspectjweaver.jar) to be used with JavaRebel. Note that AspectJ
weaver will still show "java.lang.Exception: AspectJ5 does not weave hotswapped
class" in the beginning, but this can be safely ignored.
=============================== [/JRebel AspectJ Plugin DISABLED] =============================

2012-10-19 10:34:30 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-dao.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-service.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\bin\applicationContext-service-log.xml'.
...

 

 

到此为止,你成功配置jrebel了。


值得注意的是:
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\bin' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes' will be monitored for changes.
JRebel: Directory 'D:\project\eclipse\workspaces\XXX\web\WebContent' will be monitored for changes.

JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-dao.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\web\WebContent\WEB-INF\classes\applicationContext-service.xml'.
JavaRebel-Spring: Monitoring Spring bean definitions in 'D:\project\eclipse\workspaces\XXX\bin\applicationContext-service-log.xml'.


表示在运行期间jrebel会监控这些位置的class文件和资源文件的变化情况!






阅读全文
0 0
原创粉丝点击