简体   繁体   English

不同Jar / Classloaders之间的通信

[英]Communication between different Jar's/Classloaders

I've got the following problem to solve: 我有以下问题需要解决:

There are two jar files. 有两个jar文件。 These jar's start independently from each other. 这些罐子彼此独立地开始。

Now, let's say the first jar A.jar calculates or computes something and has to commit the results to B.jar . 现在,让我们说第一个jar A.jar计算或计算一些东西,并且必须将结果提交给B.jar

I tried to communicate via a central Singleton (enum Singleton and a Singleton that uses a own classloader as mentioned here: Singleton class with several different classloaders ). 我尝试通过一个中心Singleton进行通信(enum Singleton和一个使用自己的类加载器的Singleton,如下所述:Singleton类有几个不同的类加载器 )。

But it didn't seem to work for me. 但它似乎对我不起作用。 When i start the two jar's the hashcode of the instances are different. 当我启动两个jar时,实例的哈希码是不同的。

Can anyone tell me what i'm doing wrong? 谁能告诉我我做错了什么? Or other ideas how to solve my problem? 或者其他想法如何解决我的问题?

There are two jar files. 有两个jar文件。 These jar's start independently from each other. 这些罐子彼此独立地开始。

So they're separate processes . 所以他们是独立的过程 They won't share instances of classes, variables etc. You need some form of inter-process communication to communicate between them. 它们不会共享类,变量等的实例。您需要某种形式的进程间通信来在它们之间进行通信。

That would normally mean some form of network protocol (eg TCP/UDP sockets, HTTP etc.). 这通常意味着某种形式的网络协议(例如TCP / UDP套接字,HTTP等)。 You could also do something really simple like reading/writing a shared file (that's not particularly nice, but it is straightforward for simple cases) 你也可以做一些非常简单的事情,比如读/写共享文件(这不是特别好,但对于简单的情况来说很简单)

If you are running 2 jar files separately, then each jar file runs its own JVM instance and nothing is shared between them. 如果您分别运行2个jar文件,那么每个jar文件都会运行自己的JVM实例,并且它们之间不会共享任何内容。 They are 2 seperate process. 他们是2个单独的过程。 Full-Stop. 句号。

If you wish to communicate between them then here are alternatives: It depends on what kind of data you wish to transfer. 如果您希望在它们之间进行通信,那么这里有其他选择:它取决于您希望传输的数据类型。

If it is only Strings, then: if number of process = 2 and if you are sure of it, then stdin &8 stdout is the best way forward. 如果它只是字符串,那么:如果number of process = 2 ,如果你确定它,那么stdin &8 stdout是最好的前进方式。 One process can start run another Jar file by creating a Process using ProcessBuilder and then using the streams to communicate. 一个进程可以通过使用ProcessBuilder创建Process然后使用流进行通信来开始运行另一个Jar文件。 The other process can just do System.out to transfer message. 另一个进程可以只执行System.out来传递消息。 This is preferred to Socket, because you dont have to handle graceful closing of socket etc. (In case it fails and the port is not un-binded successfully, it can be a big trouble) 这比Socket更受欢迎,因为您不必处理套接字等的优雅关闭(如果它失败并且端口没有成功解锁,则可能是一个很大的麻烦)

if number of jar files > 2 (ie number of total processes) and less than say 10 , you can probably use Sockets and communicate through Socket. 如果number of jar files > 2 (即总进程数)且less than 10 ,则可以使用套接字并通过Socket进行通信。 This should work well, though extra effort goes in gracefully managing sockets. 这应该很好,虽然额外的努力是优雅地管理套接字。

if number of process is Large , then JMS should be used. 如果number of processLarge ,则应使用JMS It does a lot of things which you dont need to handle. 它做了很多你不需要处理的事情。 Too big a task if the number of processes are less. 如果进程数量较少,则任务太大。

So in your case, process is the best way forward. 因此,在您的情况下,流程是最好的前进方式。 If the data you wish to transfer, can even be Objects. 如果您要传输的数据,甚至可以是对象。 RMI can be used given the number of processes are less. 考虑到进程数量较少,可以使用RMI If more, use JMS again. 如果更多,请再次使用JMS

Edit: Now for all the above, there is a lot of dirty work involved. 编辑:现在对于以上所有,涉及很多肮脏的工作。 For a change, if you are looking at something new & exciting, I would advice akka. 如果你正在寻找新的和令人兴奋的东西,我会建议akka。 It is a actor based model which communicate with each other using Messages. 它是基于actor的模型,它使用Messages进行相互通信。 The beauty is, the actors can be on same JVM or another (very little config) and akka takes care the rest for you. 美女是,演员可以在同一个JVM或另一个(非常小的配置)和akka照顾其余的你。 I haven't seen a more cleaner way than doing this :) 我没有看到比这更干净的方式:)

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

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