简体   繁体   English

Java类重新加载

[英]Java class reloading

I'm using jboss server and having all my classes inside a jar file. 我正在使用jboss服务器并将所有类都放在jar文件中。
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. 现在,如果我更改任何java文件,我需要替换jar中的类文件,并且必须重新启动服务器。
Is there any way to dynamically load the newly created class file without the server restart? 有没有办法动态加载新创建的类文件而不重启服务器?

Thanks in advance. 提前致谢。

I've had great success with JRebel (http://www.zeroturnaround.com/jrebel/). 我在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. 这是一个非常好的产品,可以为您可以对Java类进行的绝大多数修改实现无缝类重新加载。 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. 它提供30天的免费试用,因此您可以查看它是否适合您。

(Disclaimer: I'm in no way connected to Zero Turnaround) (免责声明:我与零周转无关)

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. 您似乎必须通过修改web.xml来欺骗服务器重新加载应用程序 - 这意味着您可以在编辑器中打开web.xml然后输入空格然后删除并保存文件或使用实用程序更改文件的修改日期。

JBoss doesn't seem to have a handy feature like Tomcat's reloadable="true" flag in Tomcat's Server.xml file. 在Tomcat的Server.xml文件中,JBoss似乎没有像Tomcat的reloadable =“true”标志那样的便利功能。

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. 如上所述,更改web.xml将导致上下文重新加载并因此刷新源代码,这可以在不重新启动服务器的情况下完成。 This works because "WEB-INF/web.xml" is configured as WatchedResource in the TOMCAT/conf/Context.xml file. 这是因为“WEB-INF / web.xml”在TOMCAT / conf / Context.xml文件中配置为WatchedResource。 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. 您可以删除此行为,但您也可以在自己的web.xml中提供WatchedResource值以观察其他文件。 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. 更好的解决方案依赖于当您丢弃加载Class的ClassLoader时可以重新加载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. 因此,如果您在自己的ClassLoader中管理热插拔代码,那么如果刷新ClassLoader,则可以刷新代码而无需重新启动上下文。 Easier said than done, unfortunately but it may get you started. 说起来容易做起来难,但不幸的是它可能会让你开始。

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

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