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等)启动以后,我们还需要进一步修改 ...
随机推荐
- Application.HookMainWindow完全替代了原来的窗口过程(但是好像也会继续传递)
unit HookMain; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialo ...
- 使用sp_configure启用 'Ad Hoc Distributed Queries'
使用sp_configure启用 'Ad Hoc Distributed Queries' 原文地址:http://blog.sina.com.cn/s/blog_531bb7630100xh88.h ...
- http://bassistance.de/jquery-plugins/
http://bassistance.de/jquery-plugins/ Query插件:手风琴 jQuery插件:自动完成 jQuery插件:留言 jQuery插件:密码验证 jQuery插件:P ...
- 2.7 Structured Regression Models
$RSS(f)=\sum_i^N \left(y_i-f(x_i)\right)^2$ 当数据量足够大时,数据存在相同$x_i$,不同$y_{il},l=1\cdots t$ 则得到的f即为条件均值$ ...
- hdu 3478 Catch(染色 dfs 或 bfs )
Problem Description A thief is running away! We can consider the city to N–. The tricky thief starts ...
- Js 中常用方法
一.获取唯一值(2014-12-23) function newGuid() { var guid = ""; var n = (((1 + Math.random()) * 0x ...
- Android,使用Json发送数据中,使用的Java转义字符 KanKan原创
kankan原创 与php后台发送数据的时候.要求用到这样的格式. private void sendJson(){ //初始化自己定义的handler CashHandler handler = n ...
- videojs 视频开发API
videojs就提供了这样一套解决方案,他是一个兼容html5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持html5的浏览器下生成一个flash的版本. 最新的3. ...
- LINQ 操作符
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace LinQ ...
- 大数值基础、for与while循环的简单对比