简体   繁体   English

在多个jvm实例中运行java代码

[英]Run java code in multiple jvm instances

I have a windows service application and a client which communicates with service using RMI. 我有一个Windows服务应用程序和一个使用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. 我需要对多个客户端进行一些并发测试,但我需要每个客户端运行到不同的jvm实例,因为它的代码中有一些静态变量。 Can I do that ? 我能这样做吗? Any ideas? 有任何想法吗?

Yes, you can do this using JDI - VirtualMachineManager (which you can get by calling Bootstrap.virtualMachineManager(); ) provides (at least one) launching connector . 是的,您可以使用JDI - VirtualMachineManager (您可以通过调用Bootstrap.virtualMachineManager(); )提供(至少一个) 启动连接器 You can then call launch(); 然后你可以调用launch(); on this connector which provides you with a VM mirror for the VM it creates. 在此连接器上,它为您创建的VM提供VM镜像 This mirror then lets you remotely execute methods on this VM. 然后,此镜像允许您远程执行此VM上的方法。

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. 您可以根据需要使用此方法设置尽可能多的远程VM,但显然这样做会对性能造成相对大的影响,而且这是一项相当大的努力。 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.) 除非努力是天文数字,否则我个人主张修复代码以保证线程安全(使用ThreadLocal ),然后你不用担心JDI(或类似的设置)。

You could use a shell script to spawn a new client with its own set of parameters in a loop. 您可以使用shell脚本在循环中使用自己的参数集生成新客户端。

But, in general. 但是,总的来说。 Running client code in several VMs just because there are some static variables being shared is definitely not a good solution. 仅仅因为共享一些静态变量而在多个VM中运行客户端代码绝对不是一个好的解决方案。 The fundamental problem is in your client code , as already suggested, you could use ThreadLocal , ThreadPools , or some synchronization logic to isolate a set of variables between various client threads. 根本问题在于您的客户端代码 ,如已经建议的那样,您可以使用ThreadLocalThreadPools或某些同步逻辑来隔离各种客户端线程之间的一组变量。

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

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