There are many ambiguities here. At least to me :) Some notes:

1) So every Function implementation represents some "formula" like this?

FunctionA() = call "B" + call "C"

2) And you want to set "B" and "C" before you call "A"?

3) You have multiple instances identified with the same (name, version) pair? Isn't this problematic? How do you distinguish between them :)

4) You essentially want to configure an environment of (Name->Function) pairs and than do a call to a FunctionX. Within the current thread you want this environment to hold while in another thread the environment can differ? I.e. you need a thread-local environment rather than a thread-safe functions? If this is the case you solve this in a "library" way by storing a ThreadLocal in some well known static field. Or alternatively thread the environment as a parameter to work() (same deal).

6) Is the RetrieverService an OSGi service? If so it will be the one using the FunctionX instances and it has to track them to make sure they are released if the FunctionX bundle goes away. The users of RetrieverService must never cache the FunctionX instances returned to them. Alternatively RetrieverService can be a is a utility that wraps a the BundleContext of the bundle owning a FunctionX service. In this case consider using a ServiceTracker instead. Or better still use iPojo, Peaberry or Spring DM to handle service binding, unbinding for you. iPojo and Peaberry in particular cache service references and release them when not used for a while (I am not sure for iPojo but Peaberry does it in this way).

Cheers,
Todor

Lars Fischer wrote:

Hello,

I have some special needs for accessing OSGi services and would be happy, if someone could give me a hint how I can archive this in a safe way.

Some common facts:
- a generic "Function" interface defines a method "work"
- all functions have to implement this interface
- all functions should be threadsafe
- all functions have some identifiers (name, version)
- different implementations of a named function should be available at runtime in multiple instances with different versions

By registering different function-instances with specific filter attributes as OSGi services, I can make the functions available.

But how do I solve this?
- functionA should be able to use functionB#work() only by name
- the used instance of functionB has to by dynamic changable per call, depending on some "context"-parameters without making a decision in functionA itself. For example: 2 calls of the same functionA instance result in calls to 2 different versions of functionB.


Variant 1 is to use a "Retriever"-Service. It should create a filter with the context-parameters and return the matching service from the local stored BundleContext. The retriever itself would be a registered service too and would be referenced by every function instance.

FunctionA would use something like this in its work-method:


Function functionB = retriever.getByName("B");
functionB.work();



In variant 2 all functions extend an abstractFunction with a method "getByName". So every function instance itself holds the BundleContext or uses it from the bundleactivator to retrieve other functions dynamically.



Can I use services in that way?

Which variant is better? Or are there other solutions for retrieving services dynamically?

Do I have to release the recieved functionB after leaving the work-scope of functionA?


Thank you in advance!

Regards,
Lars

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]




---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to