Multithreaded bundle and service instances

我是研究僧i 提交于 2020-01-25 10:40:08

问题


Assume I have 3 bundles A, B and B1. Bundle A is the starting point of my application. Bundle B provides the API of the service used by A. Bundle B1 is an implementation of the service.

Basically, bundle A has a set of records that it processes one after the other. There is no order for processing the records.

I would like to improve performance of my application by processing subsets of records concurrently.

I thought about two different ways: multiple instances of bundle A and, bundle A with multiple threads.

AFAIK, it is not possible to add multiple instances of the same bundle (i.e. same OSGi identity) in an OSGi container.

Regarding the second possibility, each thread created by bundle A would have its own identity. And the service exported by B1 needs to know the identity of the thread that uses it. Thus, I thought a ServiceFactoy would fit here. However, I've read that once a service instance is obtained by a bundle, it is cached. Therefore, all the threads would get the same service instance.

Am I right? If yes, what is the "right way" to implement this model? Feel free to propose me a completely different approach that is more OSGi friendly.

Thanks, Mickael


EDIT:

Another possibility would be to modify the Service interface to allow the service's consumers to pass their identity to the Service. The Service would then become "stateless" and the use of ServiceFactory would not be required. However, the fact the identity is required is an implementation detail (i.e. it is required only for this specific implementation), therefore for future implementations, the parameters added to the interface would not be used. This is why I prefer not to touch the interface.


回答1:


The "right way" in OSGi is to provide a service that is stateless.

As you have already discovered, the ServiceFactory concept does not help you, it only differentiates between invoking bundles, not threads, contexts or anything else that can be a container for state.

If your service must keep track of state, the best way is to make that explicit and provide some kind of parameter to pass the state. The RFC that Balazs mentions is another option in the future (provided it makes it into the specification).



来源:https://stackoverflow.com/questions/21598163/multithreaded-bundle-and-service-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!