Java类重载
我正在使用 jboss 服务器并将所有类都放在一个 jar 文件中。
现在,如果我更改任何 java 文件,我需要替换 jar 中的类文件,并且必须重新启动服务器。
有没有办法在不重启服务器的情况下动态加载新创建的类文件?
提前致谢。
I'm using jboss server and having all my classes inside a jar file.
Now if I change any of the java files, I need to replace the class file in the jar and have to restart the server.
Is there any way to dynamically load the newly created class file without the server restart?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用 JRebel (http://www.zeroturnaround.com/jrebel/) 取得了巨大成功。这是一个非常好的产品,可以对 Java 类进行的绝大多数修改实现无缝类重新加载。不需要重新启动应用程序服务器,甚至不需要重新启动应用程序,类只需在幕后重新加载即可。
它提供 30 天免费试用期,因此您可以查看它是否适合您。
(免责声明:我与零周转没有任何关系)
I've had great success with JRebel (http://www.zeroturnaround.com/jrebel/). This is a very good product that enables seamless class reloading for the vast majority of modifications you can make to a Java class. There is no restarting of the app sever or even the application required, classes simply reload behind the scenes.
It comes with a free 30 day trial so you can see if it works for you.
(Disclaimer: I'm in no way connected to Zero Turnaround)
看来您必须通过修改 web.xml 来欺骗服务器重新加载您的应用程序 - 这意味着您可以在编辑器中打开 web.xml 输入一个空格,然后删除并保存文件或使用实用程序更改文件的修改日期。
JBoss 似乎没有像 Tomcat 的 Server.xml 文件中的 Tomcat 的 reloadable="true" 标志那样方便的功能。
It appears that you have to trick the server into reloading your application by modifying web.xml -- meaning you can open web.xml in an editor enter a space then delete and save the file or change the modification date on the file with a utility.
JBoss doesn't seem to have a handy feature like Tomcat's reloadable="true" flag in Tomcat's Server.xml file.
有很多解决方案,但没有一个特别干净或简单。
如前所述,更改 web.xml 将导致上下文重新加载,从而刷新源代码,并且无需重新启动服务器即可完成此操作。这是有效的,因为“WEB-INF/web.xml”在 TOMCAT/conf/Context.xml 文件中配置为 WatchedResource。也就是说,每个上下文都会继承此设置并自动监视此文件。您可以删除此行为,但也可以在自己的 web.xml 中提供 WatchedResource 值来监视其他文件。虽然我不推荐这样做,但您可以将所有类文件添加到此文件中,并且当您更改一个文件时上下文将重新加载。
更好的解决方案依赖于这样一个事实:当您丢弃加载该类的 ClassLoader 时,可以重新加载该类。因此,如果您在自己的类加载器中管理热插拔代码,那么您可以在刷新类加载器时刷新代码,而无需重新启动上下文。不幸的是,说起来容易做起来难,但这可能会让你开始。
there are a number of solutions, none of them particularly clean or easy.
As stated, changing the web.xml will cause the context to reload and hence refresh the source code and this can be done without restarting the server. This works because "WEB-INF/web.xml" is configured as WatchedResource in the TOMCAT/conf/Context.xml file. That is every context inherits this setting and automatically watches this file. You can remove this behaviour but you can also provide WatchedResource values in your own web.xml to watch additional files. While I don't recommend it, you could add all of your class files to this and the context would reload when you change one file.
A better solution relies on the fact that a Class can be reloaded when you discard the ClassLoader that loaded the Class. Therefore if you manage your hot swappable code in your own ClassLoader then you can refresh your code without restarting the context if you refresh the ClassLoader. Easier said than done, unfortunately but it may get you started.