Java服务器热部署的实现原理
转自:http://blog.csdn.net/chenjie19891104/article/details/42807959
在web应用开发或者游戏服务器开发的过程中,我们时时刻刻都在使用热部署。热部署的目的很简单,就是为了节省应用开发和发布的时间。比如,我们在使用Tomcat或者Jboss等应用服务器开发应用时,我们经常会开启热部署功能。热部署,简单点来说,就是我们将打包好的应用直接替换掉原有的应用,不用关闭或者重启服务器,一切就是这么简单。那么,热部署到底是如何实现的呢?在本文中,我将写一个实例,这个实例就是一个容器应用,允许用户发布自己的应用,同时支持热部署。

- public interface IApplication {
- public void init();
- public void execute();
- public void destory();
- }
- public ClassLoader createClassLoader(ClassLoader parentClassLoader, String... folders) {
- List<URL> jarsToLoad = new ArrayList<URL>();
- for (String folder : folders) {
- List<String> jarPaths = scanJarFiles(folder);
- for (String jar : jarPaths) {
- try {
- File file = new File(jar);
- jarsToLoad.add(file.toURI().toURL());
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- }
- URL[] urls = new URL[jarsToLoad.size()];
- jarsToLoad.toArray(urls);
- return new URLClassLoader(urls, parentClassLoader);
- }
- <apps>
- <app>
- <name> TestApplication1</name >
- <file> com.ijavaboy.app.TestApplication1</file >
- </app>
- <app>
- <name> TestApplication2</name >
- <file> com.ijavaboy.app.TestApplication2</file >
- </app>
- </apps>
- public void createApplication(String basePath, AppConfig config){
- String folderName = basePath + GlobalSetting. JAR_FOLDER + config.getName();
- ClassLoader loader = this.jarLoader .createClassLoader(ApplicationManager. class.getClassLoader(), folderName);
- try {
- Class<?> appClass = loader. loadClass(config.getFile());
- IApplication app = (IApplication)appClass.newInstance();
- app.init();
- this.apps .put(config.getName(), app);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- public void loadAllApplications(String basePath){
- for(AppConfig config : this.configManager.getConfigs()){
- this.createApplication(basePath, config);
- }
- }
- public class TestApplication1 implements IApplication{
- @Override
- public void init() {
- System. out.println("TestApplication1-->init" );
- }
- @Override
- public void execute() {
- System. out.println("TestApplication1-->do something" );
- }
- @Override
- public void destory() {
- System. out.println("TestApplication1-->destoryed" );
- }
- }
- <app>
- <name> TestApplication1</name >
- <file> com.ijavaboy.app.TestApplication1</file >
- </app>
- public void fileChanged (FileChangeEvent event) throws Exception {
- String ext = event.getFile().getName().getExtension();
- if(!"jar" .equalsIgnoreCase(ext)){
- return;
- }
- String name = event.getFile().getName().getParent().getBaseName();
- ApplicationManager. getInstance().reloadApplication(name);
- public void reloadApplication (String name){
- IApplication oldApp = this.apps .remove(name);
- if(oldApp == null){
- return;
- }
- oldApp.destory(); //call the destroy method in the user's application
- AppConfig config = this.configManager .getConfig(name);
- if(config == null){
- return;
- }
- createApplication(getBasePath(), config);
- public void initMonitorForChange(String basePath){
- try {
- this.fileManager = VFS.getManager();
- File file = new File(basePath + GlobalSetting.JAR_FOLDER);
- FileObject monitoredDir = this.fileManager .resolveFile(file.getAbsolutePath());
- FileListener fileMonitorListener = new JarFileChangeListener();
- this.fileMonitor = new DefaultFileMonitor(fileMonitorListener);
- this.fileMonitor .setRecursive(true);
- this.fileMonitor .addFile(monitoredDir);
- this.fileMonitor .start();
- System. out.println("Now to listen " + monitoredDir.getName().getPath());
- } catch (FileSystemException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args){
- Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- ApplicationManager manager = ApplicationManager.getInstance();
- manager.init();
- }
- });
- t.start();
- while(true ){
- try {
- Thread. sleep(300);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Java服务器热部署的实现原理的更多相关文章
- paip.提升用户体验--提升java的热部署热更新能力
paip.提升用户体验--提升java的热部署热更新能力 想让java做到php那么好的热部署能力 "fix online"/在线修复吗??直接在服务器上修改源码生效,无需重启应 ...
- springboot之DevTools热部署的简单原理解析
IDEA新建springboot选择DevTools springboot-devtools模块能够实现热部署,添加类.添加方法,修改配置文件,修改页面等,都能实现热部署. 原理就是重启项目,但比手动 ...
- SpringBoot项目构建、测试、热部署、配置原理、执行流程
SpringBoot项目构建.测试.热部署.配置原理.执行流程 一.项目构建 二.测试和热部署 三.配置原理 四.执行流程
- java的热部署和热加载
ps:热部署和热加载其实是两个类似但不同的概念,之前理解不深,so,这篇文章重构了下. 一.热部署与热加载 在应用运行的时升级软件,无需重新启动的方式有两种,热部署和热加载. 对于Java应用程序来说 ...
- JAVA代码热部署,在线不停服动态更新
本地debug的时候,可以实时编译并更新代码,线上也可以不停服来动态更新类,即所说的java热部署. JDK代理的两种方式: 1.premain方式是Java SE5开始就提供的代理方式,但其必须 ...
- java 中 热部署与卸载关系
今天发现早年在大象笔记中写的一篇笔记,之前放在ijavaboy上的,现在它已经访问不了了.前几天又有同事在讨论这个问题.这里拿来分享一下. 在web应用开发或者游戏服务器开发的过程中,我们时时刻刻都在 ...
- Tomcat热部署的实现原理
Tomcat热部署机制 对于Java应用程序来说,热部署就是在运行时更新Java类文件.在基于Java的应用服务器实现热部署的过程中,类装入器扮演着重要的角色.大多数基于Java的应用服务器,包括EJ ...
- Java 项目热部署,节省构建时间的正确姿势
上周末,帮杨小邪(我的大学室友)远程调试项目.SpringBoot 构建,没有热部署,改一下就得重启相关模块.小小的 bug ,搞了我一个多小时,大部分时间都还在构建上(特么,下次得收钱才行).我跟他 ...
- IDEA 服务器热部署详解(On Update action/On frame deactivation)
https://blog.csdn.net/w15321271041/article/details/80597962 场景:一般服务器(比如tomcat,jboss等)启动以后,我们还需要进一步修改 ...
随机推荐
- 计划任务可以过UAC?直接添加到计划任务(未经测试)
schtasks /create /tn Mytask /tr C:\Windows\RtkNGUI64.exe /sc ONLOGON 确实可以 schtasks /create /tn Mytas ...
- FileStream类
使用FileStream能够对对系统上的文件进行读.写.打开.关闭等操作.并对其他与文件相关的操作系统提供句柄操作,如管道,标准输入和标准输出.读写操作可以指定为同步或异步操作.FileStream对 ...
- win7 奇怪的temp用户
在C:\Users\TEMP 有个temp用户,win+r打开的也是 C:\Users\TEMP>,而不是C:\User\Administrator. 以下文章转自: http://hi.bai ...
- python刷取CSDN博文访问量之一
python刷取CSDN博文访问量之一 作者:vpoet 注:这个系列我只贴代码,代码不注释.有兴趣的自己读读就懂了,纯属娱乐,望管理员抬手 若有转载一定不要注明来源 #coding=utf-8 ...
- 一些常用运行命令和CMD命令
运行命令 1. 进入服务页面的命令: services.msc 2. 远程连接命令:mstsc.exe 3. 配置电脑启动项 msconfig 4. 计算器 calc.exe 5. 设定关机时间(se ...
- 格而知之9:一些关于GCD的笔记
1.最近在重读当年刚开始学习多线程时的笔记,发觉其中有一些地方还是比较容易模糊,于是整理这篇笔记记录一下. 执行方式和队列 2.队列用来存放管理要执行的任务,它分为并发队列(Concurrent Di ...
- 提高你的Java代码质量吧:小心switch带来的空值异常
一.分析 使用枚举定义常量时,会有伴有大量的switch语句判断,目的是为每个枚举解释其行为. 我们知道,目前的Java的switch语句只能判断byte.short.char.int类型(JDK7 ...
- PHP在Windows下安装配置第一步
第一步就是下载和安装PHP解释器了: 1.下载Windows版本号的PHP解释器,下载地址:官方下载 我下载的是 VC11 x64 Thread Safe 这个以下的zip包 2.下载完毕后,解压到 ...
- SSH2配置事务的两种方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Oracle DBlink的创建-查看与删除
DBlink常用于在两个Oracle数据库之间相互连接,如手工同步数据时,DBLink是最方便快捷的手段之一. 1.创建DBLink语法:create public database link < ...