简体   繁体   English

Web服务器部署问题

[英]Webserver deployment issue

My current project is writing a HTTP-Webserver in Java. 我当前的项目是用Java编写HTTP-Webserver。 The server checks a "deployment-directory" every two seconds and searches for a file named content.jar . 服务器每两秒钟检查一次“部署目录”,然后搜索名为content.jar的文件。 If the file was changed the server deployes it. 如果文件被更改,服务器将对其进行部署。 The file contains servlets which will be loaded by an URLClassLoader . 该文件包含将由URLClassLoader加载的servlet。 On every deployment a new classloader is created. 在每个部署中,都会创建一个新的类加载器。

In a different project my servlets are being developed. 在另一个项目中,正在开发我的servlet。 I wrote an Ant-buildfile that creates the content.jar file and moves it into the server's deployment directory. 我编写了一个Ant-buildfile,它创建content.jar文件并将其移至服务器的部署目录。 Unfortunately I get an Ant error: 不幸的是,我得到一个Ant错误:

BUILD FAILED [path]\\WebServerServlet\\Ant\\Build.xml:15: Unable to remove existing file [path]\\WebServer\\web\\content.jar 失败:[path] \\ WebServerServlet \\ Ant \\ Build.xml:15:无法删除现有文件[path] \\ WebServer \\ web \\ content.jar

I suppose that the file is locked on the filesystem by the JVM - is there a way to tell Ant it should not try to delete and move the file but to overwrite it instead? 我想文件已被JVM锁定在文件系统上-有没有办法告诉Ant,它不应该尝试删除和移动文件,而是要覆盖它?

Otherwise, does anyone have an idea how to avoid the file getting locked by the JVM by changing my deployment system? 否则,是否有人知道如何通过更改部署系统来避免文件被JVM锁定?

EDIT 编辑

I have now found a solution! 我现在找到了解决方案! As discussed in this question the only thing i had to add was 正如这个问题所讨论的,我唯一要添加的是

URLClassLoader loader = new URLClassLoader(..);
//class loading stuff here
loader.close() //the line all the trouble was about

With this appendix my Ant-buildfile is able to delete the "old" jar in the deployment-directory and move the new one into it. 通过此附录,我的Ant-buildfile能够删除Deployment-目录中的“旧” jar并将新的jar移入其中。

I suspect that the classloader has a lock on the jar. 我怀疑类加载器在罐子上有锁。 Either way until the lock is released you're not going to be able to move, delete or modify it. 无论哪种方式,在释放锁之前,您都无法移动,删除或修改它。

The solution is to write your own classloader. 解决方案是编写自己的类加载器。 It's this custom classloader that should be monitoring your jar for changes, not the server, then reloading the classes accordingly. 正是这个自定义类加载器应该监视您的jar中的更改,而不是服务器,然后相应地重新加载类。

There's a lot involved in writing a classloader to do dynamic reloading - fortunately, it's explained here . 编写类加载器进行动态重新加载涉及很多工作,所幸在此进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM