如何保持Java虚拟机运行并发送请求
我是Java的初学者,我想将计划请求发送到 planner 。
问题在于,由于我每次都在启动jvm
,执行时间很大!
您能告诉我如何保持JVM
作为服务器运行并发送请求,而不是每次启动jvm
以减少执行时间?提前致谢。
我正在使用python
来调用jvm
这样:
def solver(
libraryPath='PDDL/pddl4j-devel/build/libs/pddl4j-3.8.3.jar',
planner = 'fr.uga.pddl4j.planners.htn.stn.tfd.TFDPlanner',
Memory = ['-Xms12288m' ,'-Xmx12288m'],
domainPath = 'new_domain.pddl',
problemPath = 'problem_main.pddl',
save=False
):
"""
Function: solver, to run the solver and get a plan if there is one.
---
Parameters:
@param: planner, string, planner name.
@param: libraryPath, string, the path to the java library(extension included)
@param: Memory, list of two ['-Xms size m', -Xmx size m'].
@param: domainPath, string, the path to the domain, (extension included).
@param: problemPath, string, the path to the problem, (extension included).
@param: save, boolean, to save the plan as log file.
---
@return: None.
"""
cmd = ['java', '-javaagent:'+libraryPath, '-server' , Memory[0] , Memory[1], planner, '-d', self.path+domainPath, '-p', self.path+problemPath]
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
I'm a beginner in Java, and I want to send planning requests to a planner.
The problem is that the execution time is huge due to the fact that I'm launching the JVM
at each time!
Can you please tell me how to keep the JVM
running as a server and send requests instead of launching the JVM
each time to reduce the execution time? thanks in advance.
I'm using python
to call the JVM
like this:
def solver(
libraryPath='PDDL/pddl4j-devel/build/libs/pddl4j-3.8.3.jar',
planner = 'fr.uga.pddl4j.planners.htn.stn.tfd.TFDPlanner',
Memory = ['-Xms12288m' ,'-Xmx12288m'],
domainPath = 'new_domain.pddl',
problemPath = 'problem_main.pddl',
save=False
):
"""
Function: solver, to run the solver and get a plan if there is one.
---
Parameters:
@param: planner, string, planner name.
@param: libraryPath, string, the path to the java library(extension included)
@param: Memory, list of two ['-Xms size m', -Xmx size m'].
@param: domainPath, string, the path to the domain, (extension included).
@param: problemPath, string, the path to the problem, (extension included).
@param: save, boolean, to save the plan as log file.
---
@return: None.
"""
cmd = ['java', '-javaagent:'+libraryPath, '-server' , Memory[0] , Memory[1], planner, '-d', self.path+domainPath, '-p', self.path+problemPath]
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开销的启动时间小于50%。我知道这不是理想的选择,但是除非您优化计划师,否则我不会担心。 ]
如前所述,构建长期运行的Java服务器应用程序是另一种选择。有许多框架,例如春季。
对于您来说,这可能是个过度杀伤力,因此您可以检查一些有关构建“简单http服务器”的教程,例如
tps:// docs .oracle.com/javase/tutorial/networking/sockets/index.html“ rel =“ nofollow noreferrer”> https://docs.oracle.com/javase.com/javase/javase/tutorial/networior/networlorial/sockets/sockets/index.htex.html
The startup time overhead is less than 50%. I understand that it's not ideal but unless you optimize your planner quite a bit I wouldn't be worried that much. ]
Building a long-running java server app is another option as already mentioned. There are numerous frameworks for that such as Spring.
For you, that's likely an overkill so you can check some tutorials about building "simple http server", e.g.
Or go even lower level with sockets: https://docs.oracle.com/javase/tutorial/networking/sockets/index.html