简体   繁体   English

简单的Java服务发现框架

[英]Simple Java service discovery framework

I would like to discover all implementations of an interface during runtime in a Java app, and load those classes dynamically from JAR files that the user can add to a folder. 我想在Java应用程序运行时发现接口的所有实现,并从用户可以添加到文件夹的JAR文件中动态加载这些类。 It's a plug-in system, basically. 基本上,这是一个插件系统。

I found a few solutions for this: 我找到了一些解决方案:

Are there any other widely used solutions for this? 还有其他广泛使用的解决方案吗?

Update: There is no need for code separation, and OSGi seems to be far too complex for my simple needs right now. 更新:无需进行代码分离,对于我现在的简单需求,OSGi似乎过于复杂。 I also added "Simple" to the title of this question to clarify my intentions. 我还向该问题的标题添加了“简单”,以阐明我的意图。

It's bit on the heavy side, but you should consider Apache Felix or Eclipse Equinox , both are OSGi implementations which are very much alive and kicking, but possibly overkill for your needs. 这有点沉重,但您应该考虑使用Apache FelixEclipse Equinox ,它们都是OSGi实现,非常活跃,但是对于您的需求而言可能有些过高。 However, this is one of the very problems that OSGi is designed to solve. 但是,这是OSGi旨在解决的非常问题之一。

You could try the service discovery of Apache River (formerly Jini) 您可以尝试Apache River(以前称为Jini)的服务发现

http://river.apache.org/ http://river.apache.org/

It discovers services by a Remote interface and downloads the proxy for you, which is topically an RMI stub. 它通过远程接口发现服务,并为您下载代理,该代理通常是RMI存根。

Like @skaffman told, OSGi and modern implementations built upon seems like a perfect solution. 就像@skaffman所说,OSGi和基于此的现代实现似乎是一个完美的解决方案。 If you're on the guice bandwagon, consider iPOJO , which perfectly incorporates annotations in the OSGi stack (notice iPOJO works on any OSGi platform). 如果您正在使用guice潮流,请考虑iPOJO ,它可以完美地将注释合并到OSGi堆栈中(注意iPOJO可在任何OSGi平台上使用)。 If you're more in the XML/Spring bandwagon, consider using Blueprint . 如果您更了解XML / Spring潮流,请考虑使用Blueprint

Finally, considering JSPF, I won't share your opinon about its immaturity. 最后,考虑到JSPF,我不会分享您对它的不成熟的看法。 Having collaborated a little on that project, I find it really useful in its field, as it allows easy plugin use, without the hassle and classpath separaion that OSGi provides. 在该项目上进行了一些合作之后,我发现它在其领域中非常有用,因为它可以轻松使用插件,而没有OSGi提供的麻烦和类路径分离。

+1 SPI -- it is lightweight, a little bit of maintenance (the services/* text files) but the way Java's classloaders work any "autodiscovery" isn't going to be 100% reliable; +1 SPI —它是轻量级的,需要一点维护(服务/ *文本文件),但是Java的类加载器工作任何“自动发现”的方式都不会100%可靠; you can reduce the hassle of the text files with a simple program which for a given project/directory generates and/or (I recommend) tests that all implementations are included 您可以使用简单的程序来减少文本文件的麻烦,该程序可以为给定的项目/目录生成和/或(我建议)测试是否包含所有实现

+2 OSGi -- if you're willing to go welterweight, and note the implementations are getting lighter and easier (at least some of them!) +2 OSGi-如果您愿意进行次中量级,并注意实现变得越来越轻巧(至少其中一些!)。

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

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