在多个jvm实例中运行java代码 [英] Run java code in multiple jvm instances

查看:437
本文介绍了在多个jvm实例中运行java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务应用程序和一个使用RMI与服务进行通信的客户端。
我需要对多个客户端进行一些并发测试,但我需要每个客户端运行到不同的jvm实例,因为它的代码中有一些静态变量。
我可以这样做吗?有任何想法吗?

I have a windows service application and a client which communicates with service using RMI. I need to do some concurrency testing with multiple clients but I need every client runs to different jvm instance because there are some static variables in its code. Can I do that ? Any ideas?

推荐答案

是的,您可以使用JDI - VirtualMachineManager (您可以通过调用 Bootstrap.virtualMachineManager(); )提供(至少一个)发射连接器。然后,您可以在此连接器上调用 launch(); ,为您提供 VM镜像

Yes, you can do this using JDI - VirtualMachineManager (which you can get by calling Bootstrap.virtualMachineManager();) provides (at least one) launching connector. You can then call launch(); on this connector which provides you with a VM mirror for the VM it creates. This mirror then lets you remotely execute methods on this VM.

您可以使用此方法设置任意数量的远程虚拟机,虽然显然有一个相对较大的性能损失以这种方式做事情,这是一个公平的努力。除非努力是天文学的,我个人主张修复代码以保证线程安全(使用 ThreadLocal ),然后你不需要担心JDI类似的设置。)

You can set up as many remote VMs using this method as you choose, though obviously there's a relatively big performance penalty for doing things this way, and it's a fair bit of effort. Unless the effort would be astronomical, I'd personally advocate fixing the code to guarantee thread safety (using ThreadLocal) and then you do away with the need to worry about JDI (or a similar setup.)

这篇关于在多个jvm实例中运行java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆