使用jrebel来实现J2EE项目的热部署

JRebel是一套JavaEE开发工具。JRebel允许开发团队在有限的时间内完成更多的任务修正更多的问题,发布更高质量的软件产品。 JRebel是收费软件,用户可以在JRebel官方站点下载30天的评估版本。

Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率。

JRebel是一款JAVA虚拟机插件,它使得JAVA程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响。JRebel使你能即时分别看到代码、类和资源的变化,你可以一个个地上传而不是一次性全部部署。当程序员在开发环境中对任何一个类或者资源作出修改的时候,这个变化会直接反应在部署好的应用程序上,从而跳过了构建和部署的过程,每年可以省去部署用的时间花费高达5.25个星期。

JRebel支持Eclipse、MyEclipse、IntelliJ、NetBeans等众多IDE,我们以Eclipse为例,在Eclipse中想要使用JRebel,我们需要安装一个插件,可以在Eclipse Marketplace中搜索安装,也可以使用install new software通过URL来安装。url为:http://www.zeroturnaround.com/update-site/。或者下载离线安装包来安装,本站提供相关的下载地址:http://pan.baidu.com/s/1i5c2rGx

由于jrebel是收费软件,但是也提供了注册免费使用,需要先在jrebel官网上注册,然后会生成一个license。

一、生成license

1471409373762068616.png

1471409444824094351.png

1471409477230009148.png

1471409501073049857.png

注册成功后会提示填写相应的信息,最后会生成license:

1471409926852079535.png

二、安装并配置eclipse

由于我使用的是eclipse来做j2ee的开发,所以以eclipse为例来讲解如何安装jrebel。安装有三种方式:

1、直接在Eclipse Marketplace安装

搜索jrebel,默认首页第一个就是,直接点击Install即可。

2、通过update来安装

url地址为:http://www.zeroturnaround.com/update-site/

3、离线安装

下载离线安装包update-site.zip来安装。

初次运行jrebel的时候提示需要输入license,将之前注册生成的license输入进去,会自动得到注册的用户名并进入configuration页面。

jrebel6.4.7版本已经很智能了,基本不需要怎么配置。只需要在project选项选择使用jrebel来监控的项目。

选择完后会在Tomcat配置中的Enable JRebel agent被选中。

1471410522739021294.png

同时会在项目的WEB_INF/classes目录下生成一个rebel.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<application generated-by="eclipse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_1.xsd">

	<classpath>
		<dir name="E:/workspace/Demo/target/classes">
		</dir>
		<dir name="E:/workspace/Demo/common/target/classes">
		</dir>
	</classpath>

	<web>
		<link target="/">
			<dir name="E:/workspace/Demo/src/main/webapp">
			</dir>
		</link>
	</web>

</application>

其中<classpath>中可以手动加上需要监听的目录。如果是多model的maven项目,则需要在parent项目中建一个rebel.xml文件,然后把每个model项目的target/classes目录添加进来。

三、测试

1、运行tomcat的时候就会输出jrebel相关的console信息,会看到哪些目录处于监控中。截取部分tomcat启动输出内容:

JRebel: Starting logging to file: C:\Users\wangyc\.jrebel\jrebel.log
2016-08-17 13:13:14 JRebel: Contacting myJRebel server ..
八月 17, 2016 1:13:15 下午 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: C:\Program Files\Java\jdk1.8.0_92\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_92/bin/server;C:/Program Files/Java/jre1.8.0_92/bin;C:/Program Files/Java/jre1.8.0_92/lib/amd64;C:\ProgramData\Oracle\Java\javapath;D:\Python27\;D:\Python27\Scripts;.;C:\Program Files\Java\jdk1.7.0_51\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;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;D:\Program Files (x86)\Windows7Master;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;E:\soft\apache-maven-3.0.5\bin;E:\gradle-2.2.1\bin;E:\Program Files\redis\redis-2.4.5-win32-win64\64bit;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\MongoDB\Server\3.0\bin;C:\Program Files\Java\jre7\bin;D:\nodejs\npm;D:\nvmw;C:\Program Files (x86)\Git\bin;E:\soft\adt-bundle-windows\sdk\platform-tools;E:\soft\adt-bundle-windows\sdk\tools;D:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\;D:\nosql\redis\64bit;C:\Users\wangyc\AppData\Roaming\npm;E:\soft\eclipse8;;.
八月 17, 2016 1:13:16 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:atlas3' did not find a matching property.
八月 17, 2016 1:13:16 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
八月 17, 2016 1:13:16 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
八月 17, 2016 1:13:16 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 769 ms
八月 17, 2016 1:13:16 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
八月 17, 2016 1:13:16 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.42
八月 17, 2016 1:13:17 下午 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\atlas3\WEB-INF\lib\servlet-api-2.3.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
2016-08-17 13:13:17 JRebel: Directory 'E:\workspace\Demo\target\classes' will be monitored for changes.
2016-08-17 13:13:17 JRebel: Directory 'E:\workspace\Demo\common\target\classes' will be monitored for changes.
2016-08-17 13:13:17 JRebel: Directory 'E:\workspace\Demo\src\main\webapp' will be monitored for changes.
2016-08-17 13:13:17 JRebel:  
2016-08-17 13:13:17 JRebel:  #############################################################
2016-08-17 13:13:17 JRebel:  
2016-08-17 13:13:17 JRebel:  JRebel Legacy Agent 6.4.7 (201607271052)
2016-08-17 13:13:17 JRebel:  (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2016-08-17 13:13:17 JRebel:  
2016-08-17 13:13:17 JRebel:  Over the last 2 days JRebel prevented
2016-08-17 13:13:17 JRebel:  at least 107 redeploys/restarts saving you about 4.3 hours.
2016-08-17 13:13:17 JRebel:  
2016-08-17 13:13:17 JRebel:  Licensed to Yonghcao Wang (using myJRebel).
2016-08-17 13:13:17 JRebel:  
2016-08-17 13:13:17 JRebel:  
2016-08-17 13:13:17 JRebel:  #############################################################
2016-08-17 13:13:17 JRebel:  
八月 17, 2016 1:13:20 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
八月 17, 2016 1:13:20 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
2016-08-17 13:13:20.756 INFO [localhost-startStop-1][ContextLoader.java:305] - Root WebApplicationContext: initialization started
2016-08-17 13:13:20.950 INFO [localhost-startStop-1][AbstractApplicationContext.java:581] - Refreshing Root WebApplicationContext: startup date [Wed Aug 17 13:13:20 CST 2016]; root of context hierarchy
2016-08-17 13:13:21 JRebel: Monitoring Spring bean definitions in 'E:\workspace\Demo\target\classes\spring\applicationContext.xml'.
2016-08-17 13:13:21.262 INFO [localhost-startStop-1][XmlBeanDefinitionReader.java:317] - Loading XML bean definitions from URL [file:/E:/workspace/Demo/target/classes/spring/applicationContext.xml]
2016-08-17 13:13:21 JRebel: Monitoring Spring bean definitions in 'E:\workspace\Demo\target\classes\spring\applicationContext-db.xml'.
2016-08-17 13:13:21.392 INFO [localhost-startStop-1][XmlBeanDefinitionReader.java:317] - Loading XML bean definitions from URL [file:/E:/workspace/Demo/target/classes/spring/applicationContext-db.xml]
2016-08-17 13:13:21 JRebel: Monitoring Spring bean definitions in 'E:\workspace\Demo\target\classes\spring\applicationContext-crm-db.xml'.
2016-08-17 13:13:21.514 INFO [localhost-startStop-1][XmlBeanDefinitionReader.java:317] - Loading XML bean definitions from URL [file:/E:/workspace/Demo/target/classes/spring/applicationContext-crm-db.xml]
2016-08-17 13:13:21 JRebel: Monitoring Spring bean definitions in 'E:\workspace\Demo\target\classes\spring\applicationContext-security_cas.xml'.
2016-08-17 13:13:21.527 INFO [localhost-startStop-1][XmlBeanDefinitionReader.java:317] - Loading XML bean definitions from URL [file:/E:/workspace/Demo/target/classes/spring/applicationContext-security_cas.xml]
2016-08-17 13:13:21.568 INFO [localhost-startStop-1][SpringSecurityCoreVersion.java:75] - You are running with Spring Security Core 4.1.1.RELEASE
2016-08-17 13:13:21.573 INFO [localhost-startStop-1][SecurityNamespaceHandler.java:78] - Spring Security 'config' module version is 4.1.1.RELEASE
2016-08-17 13:13:21.634 INFO [localhost-startStop-1][GlobalMethodSecurityBeanDefinitionParser.java:207] - Expressions were enabled for method security but no SecurityExpressionHandler was configured. All hasPermision() expressions will evaluate to false.
2016-08-17 13:13:21.686 INFO [localhost-startStop-1][FilterInvocationSecurityMetadataSourceParser.java:174] - Creating access control expression attribute 'authenticated' for /**
2016-08-17 13:13:21.707 INFO [localhost-startStop-1][HttpSecurityBeanDefinitionParser.java:306] - Checking sorted filter chain: [Root bean: class [org.springframework.security.web.context.SecurityContextPersistenceFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 200, Root bean: class [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 400, Root bean: class [org.springframework.security.web.header.HeaderWriterFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 500, <requestSingleLogoutFilter>, order = 799, <singleLogoutFilter>, order = 1099, <casFilter>, order = 1200, Root bean: class [org.springframework.security.web.savedrequest.RequestCacheAwareFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 1700, Root bean: class [org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 1800, Root bean: class [org.springframework.security.web.session.SessionManagementFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 2200, Root bean: class [org.springframework.security.web.access.ExceptionTranslationFilter]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null, order = 2300, <customFilter>, order = 2399, <org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0>, order = 2400]
2016-08-17 13:13:21 JRebel: Monitoring Spring bean definitions in 'E:\workspace\Demo\target\classes\spring\applicationContext-scheduler.xml'.
2016-08-17 13:13:21.720 INFO [localhost-startStop-1][XmlBeanDefinitionReader.java:317] - Loading XML bean definitions from URL [file:/E:/workspace/Demo/target/classes/spring/applicationContext-scheduler.xml]
2016-08-17 13:13:21 JRebel: Monitoring Spring bean definitions in 'E:\workspace\Demo\target\classes\spring\applicationContext-velocity.xml'.
2016-08-17 13:13:21.742 INFO [localhost-startStop-1][XmlBeanDefinitionReader.java:317] - Loading XML bean definitions from URL [file:/E:/workspace/Demo/target/classes/spring/applicationContext-velocity.xml]

当我们修改一个java文件之后会发生什么呢?

2016-08-17 13:17:27 JRebel: Reloading log4j2 configuration.
2016-08-17 13:17:28 JRebel: Reloading class 'com.business.controller.DemoController'.
2016-08-17 13:17:30 JRebel: Reconfiguring bean 'demoController' [com.business.controller.DemoController]

此时不需要重启tomcat即可生效。同时如果修改配置文件重新请求页面的时候会自动重新加载生效。

猜你喜欢

转载自blog.csdn.net/H12KJGJ/article/details/80764239