如何实例化多个OSGi服务?
在 Eclipse RCP 应用程序的上下文中,我决定使用 OSGi 服务从插件(即捆绑包)中提供“接口”。
在我的一个插件中,我有以下解析器接口:
public interface Parser {
public void start(File file);
public boolean hasNext();
public Object next();
}
消费者插件将使用此接口来解析文件。因为可以同时完成多个解析,并且因为该接口的实现将需要多个“状态”私有字段,所以该服务的每个使用者必须使用专用的服务实例。
在这种情况下,manu OSGi 教程提供的默认解决方案(包括在解析器包的启动方法中注册一个服务实例)不起作用。处理这种解决方案的最佳解决方案是什么? 我可以使用一种独特的方法创建 ParserFactory 服务:
public Parser create(File file);
??
有什么意见欢迎留言,
In the context of an Eclipse RCP application I decided to use OSGi services to provide "Interfaces" out of a plugin (i.e a bundle).
In one of my plugin I have the following Parser interface:
public interface Parser {
public void start(File file);
public boolean hasNext();
public Object next();
}
Consumer plugins will use this interface to parse files. Because several parsing can be done in the same time and because an implementation of this interface will need several "state" private field each consumer of this service must use a dedicated service instance.
In this case, the default solution provided by manu OSGi tutorials consisting in registering ONE service instance in the start method of the parser bundle doesn't work. What is the best solution to handle such a solution ?
I can create a ParserFactory service with one unique method:
public Parser create(File file);
??
Any comment is welcome,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所建议的,我会将您的服务接口更改为解析器的提供者。
你的解析器只是一个迭代器,所以也许像
使用输入流或读取器这样的东西也使它比需要文件更灵活。
As you're suggesting, I would change your service interface to be a provider of Parsers.
And your Parser is just an Iterator, so maybe something like
Using an InputStream or Reader also makes it more flexible that requiring a File.
看一下 OSGi ServiceFactory;这允许您为不同的请求包实例化服务。您可以在核心规范的第 5.6 节中阅读更多相关信息。
Have a look at the OSGi ServiceFactory; this allows you to instantiate services for different requesting bundles. You can read more about it in section 5.6 of the core specification.