d********r 发帖数: 199 | 1 我有一个servlet,功能是周期性地定时(比如每周一次)
调用一个package(该package有几百个class)的功能进行某种计算,然后存下结果。
package里有一些class里有一些static 的变量,一般初始值都是0。
第一次计算后,这些变量被改变了。
当下一次调用的时候,我想让这些变量仍然变为0,就象这些class从来没有被调进内存,
是第一次被调用一样。
有什么简单的方法实现这个目的?
如果没有简单的办法的话,我只好在每次调用结束后手工将所有这些static变量清0。
当然,restart tomcat 或reload这个servlet application是可以的,不过我不希望这么手工做。
我希望程序能自动自己做这件事。
多谢先 |
g*****g 发帖数: 34805 | 2 I'd suggest you fix the package instead, if they are variables,
they shouldn't be static.
存,
这么手工做。
【在 d********r 的大作中提到】 : 我有一个servlet,功能是周期性地定时(比如每周一次) : 调用一个package(该package有几百个class)的功能进行某种计算,然后存下结果。 : package里有一些class里有一些static 的变量,一般初始值都是0。 : 第一次计算后,这些变量被改变了。 : 当下一次调用的时候,我想让这些变量仍然变为0,就象这些class从来没有被调进内存, : 是第一次被调用一样。 : 有什么简单的方法实现这个目的? : 如果没有简单的办法的话,我只好在每次调用结束后手工将所有这些static变量清0。 : 当然,restart tomcat 或reload这个servlet application是可以的,不过我不希望这么手工做。 : 我希望程序能自动自己做这件事。
|
d********r 发帖数: 199 | 3 我就是不愿意改成非static才问这个问题的。
如果在必须是static变量的前提下,有没有什么办法?
【在 g*****g 的大作中提到】 : I'd suggest you fix the package instead, if they are variables, : they shouldn't be static. : : 存, : 这么手工做。
|
o***g 发帖数: 2784 | 4 servlet的生命周期是怎么样的?
static怎么用手来初始化?
能手初始化,怎么就不能用程序初始化呢?
servlet能周期性的运行,怎么就不能周期性的初始化static呢
完全没搞懂
存,
这么手工做。
【在 d********r 的大作中提到】 : 我有一个servlet,功能是周期性地定时(比如每周一次) : 调用一个package(该package有几百个class)的功能进行某种计算,然后存下结果。 : package里有一些class里有一些static 的变量,一般初始值都是0。 : 第一次计算后,这些变量被改变了。 : 当下一次调用的时候,我想让这些变量仍然变为0,就象这些class从来没有被调进内存, : 是第一次被调用一样。 : 有什么简单的方法实现这个目的? : 如果没有简单的办法的话,我只好在每次调用结束后手工将所有这些static变量清0。 : 当然,restart tomcat 或reload这个servlet application是可以的,不过我不希望这么手工做。 : 我希望程序能自动自己做这件事。
|
r*****l 发帖数: 2859 | 5 你调用之前init一下不行吗?
【在 d********r 的大作中提到】 : 我就是不愿意改成非static才问这个问题的。 : 如果在必须是static变量的前提下,有没有什么办法?
|
d********r 发帖数: 199 | 6 servlet是一直运行的。
它每周调用一次那个package进行计算,存下结果后进入idle状态,
就是啥事也不做。
(当然,负责在webpage的调用下,显示目前的状态 是idle或busy)
那个计算用的package里很多地方有一些static的变量,我只是不想去一个个的找出来
来把它们进行“手工初始化”。
因为本来这个package只是一个正常的standalone的application,
其中的main() 每run一次就正常退出了,所以不存在static变量初始化的问题。
我现在另外加了一个servlet来周期性的调用这个package,
就会产生未初始化的问题。
我只是想问有没有办法让这些package的全部class在run一次后从内存里消失,
就象从来没用过一样。
如果没有办法的话,我只好改code,在每次调用之前,手工对那些static变量初始化了。
【在 o***g 的大作中提到】 : servlet的生命周期是怎么样的? : static怎么用手来初始化? : 能手初始化,怎么就不能用程序初始化呢? : servlet能周期性的运行,怎么就不能周期性的初始化static呢 : 完全没搞懂 : : 存, : 这么手工做。
|
d********r 发帖数: 199 | 7 这样行,
不过我想问这是唯一的办法吗?有办法让一些用过的class从内存里消失吗?
【在 r*****l 的大作中提到】 : 你调用之前init一下不行吗?
|
o***g 发帖数: 2784 | 8 你这个servlet用的很奇怪
另外,建议去研究一下ClassLoader,直接Load那个package里的main。用完了unload
如果不需要这个package和你的servlet在一个内存空间的话,用Runtime就可以了。
【在 d********r 的大作中提到】 : servlet是一直运行的。 : 它每周调用一次那个package进行计算,存下结果后进入idle状态, : 就是啥事也不做。 : (当然,负责在webpage的调用下,显示目前的状态 是idle或busy) : 那个计算用的package里很多地方有一些static的变量,我只是不想去一个个的找出来 : 来把它们进行“手工初始化”。 : 因为本来这个package只是一个正常的standalone的application, : 其中的main() 每run一次就正常退出了,所以不存在static变量初始化的问题。 : 我现在另外加了一个servlet来周期性的调用这个package, : 就会产生未初始化的问题。
|
r*****l 发帖数: 2859 | 9 To answer your question about how to reload classes.
(I personally think, in your case, this is NOT needed
and way OVERKILL), you need to do:
1. Do not put the package in classpath when you start
the application
2. Use the package's path to construct a URI; use this
URI to construct a ClassLoader
3. Use ClassLoader.loadClass() to load the classes in
the package
4. Use reflection to call the methods. Remember the
package is not in the class path so you cannot
use the classes/method |
g*****g 发帖数: 34805 | 10 It's possible to use classloader to load classes.
But it can be tricky and give you more harm than meric.
When you are using the package on web server. It's not just
the problem of reset static variables. When you have 2 concurrent
threads, you'll run into weird result, usually change them
to non-static is a must.
【在 d********r 的大作中提到】 : 我就是不愿意改成非static才问这个问题的。 : 如果在必须是static变量的前提下,有没有什么办法?
|
|
|
g*****g 发帖数: 34805 | 11 In your case, you may want to start another VM to run the standalone.
Runtime.getRuntime().exec("java ....");
【在 d********r 的大作中提到】 : 这样行, : 不过我想问这是唯一的办法吗?有办法让一些用过的class从内存里消失吗?
|
r*****l 发帖数: 2859 | 12 You beat me on this :)
Spend too long writting my last reply.
【在 g*****g 的大作中提到】 : In your case, you may want to start another VM to run the standalone. : Runtime.getRuntime().exec("java ....");
|
I*******o 发帖数: 53 | 13 zan...
i'm just wondering why LZ refuses to refactor this package...
【在 g*****g 的大作中提到】 : In your case, you may want to start another VM to run the standalone. : Runtime.getRuntime().exec("java ....");
|
d********r 发帖数: 199 | 14 似乎您的solution正是我想要的,
不过关于ClassLoader能再详细点吗?
怎么unload呢?
【在 o***g 的大作中提到】 : 你这个servlet用的很奇怪 : 另外,建议去研究一下ClassLoader,直接Load那个package里的main。用完了unload : 如果不需要这个package和你的servlet在一个内存空间的话,用Runtime就可以了。
|
c*****t 发帖数: 1879 | 15 First, I want to say that what you were trying to do really should be
solved at the source code level of the package you were trying to used.
That said, strictly from an academic point of the view, there might
be a solution.
The idea is to create an instance of URLClassLoader which would be
responsible for all the loading the classes you intend to use.
JVM supports loading multiple versions of the same class through
using different class loaders. In this case however, we are merely
trying to re |
m******t 发帖数: 2416 | 16
存,
这么手工做。
Just set up a cron job to call the tomcat deployer api and restart the app.
If you really want to do it from within the app, then reaBull's classloader
solution is what I would suggest as well - and yes, I too think it's an
overkill. 8-)
【在 d********r 的大作中提到】 : 我有一个servlet,功能是周期性地定时(比如每周一次) : 调用一个package(该package有几百个class)的功能进行某种计算,然后存下结果。 : package里有一些class里有一些static 的变量,一般初始值都是0。 : 第一次计算后,这些变量被改变了。 : 当下一次调用的时候,我想让这些变量仍然变为0,就象这些class从来没有被调进内存, : 是第一次被调用一样。 : 有什么简单的方法实现这个目的? : 如果没有简单的办法的话,我只好在每次调用结束后手工将所有这些static变量清0。 : 当然,restart tomcat 或reload这个servlet application是可以的,不过我不希望这么手工做。 : 我希望程序能自动自己做这件事。
|
d********r 发帖数: 199 | 17 似乎这个办法更好。我来试试这个。
多谢各位了。
【在 g*****g 的大作中提到】 : In your case, you may want to start another VM to run the standalone. : Runtime.getRuntime().exec("java ....");
|
r*****l 发帖数: 2859 | 18 这个办法不是更好,对你的情况应该是最佳的方案了。
Runtime.getRuntime().exec()
【在 d********r 的大作中提到】 : 似乎这个办法更好。我来试试这个。 : 多谢各位了。
|
d********r 发帖数: 199 | 19 严重同意!
thanks.
【在 r*****l 的大作中提到】 : 这个办法不是更好,对你的情况应该是最佳的方案了。 : Runtime.getRuntime().exec()
|
g******u 发帖数: 153 | 20 I feel it's a wrong design. Servlet can start the process(might be a new
thread). But shouldn't run this periodical task in servlet. |
r*****l 发帖数: 2859 | 21 This is integration of two systems. One the servlet,
one the standard alone Java app.
【在 g******u 的大作中提到】 : I feel it's a wrong design. Servlet can start the process(might be a new : thread). But shouldn't run this periodical task in servlet.
|
h*********o 发帖数: 62 | 22 there is a coded sample about this issue in reflection in action book. |