Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2714
  • Last Modified:

OSGi bundle -> Singleton

Hello everybody;

I'm starting to use OSGi and I'm a little "noob" in the bussiness.

My problem is the next;

I have a bundle that I need to be singleton, because it must be accessed by another two bundles at the same time; I mean, two different bundles are going to use it, and when one of them modifies some of the values inside the first bundle, the change must be seen by the second bundle.

I'm working with Eclipse and in the manifest.mf file there's an option to make the bundle singleton, but I don't know if that's working.

I explain: To probe if the bundle is singleton, I used the option in the manifest.mf file to make "plug-in as singleton". Then, I inserted a new property inside the first bundle(the one that must be singleton). The property is just an String, message, who's default value is "Not altered".I also implemented two function, setMessage(String) and getMessage.
Well, with the second bundle I modify the value to "Altered by bundle2". But when I read the value with bundle3, the message value is ALWAYS "Not altered" , UNLESS I set Message as STATIC.

My doubt is the next; When I put Message as static, it always works. I modify the value and when I read it , it writes "Altered by bundle2", perfect. But that works always, even If I don't choose the "this plug-in as singleton" option in the manifest.mf file. It seems that it doesn't care if I mark it as singleton if I just put static in the property.

But this way I don't know if it's really a singleton or what. It doesn't make sense to me... does this mean that if I want the actualized values of the properties I need to mark them as static, and nothing else?

I attach an Image because my english is soooo poor and I don't know if I explain correctly what I want to do.

Thanks a lot for your time dudes,

1 Solution
Dear Aranbarri:

    I'm going to avoid turning this into a discussion of whether a singleton is a design pattern or anti-design pattern. If you Google that topic, you'll find some interesting reading.

    I can see your confusion with the singleton parameter for the Bundle-SymbolName directive, which can also be set through the This plug-in is a singleton checkbox on the manifest's Overview tab.

    The term "singleton" as used in the OSGI specification does not imply the Singleton design pattern, but rather specifies that the plugin is a singleton bundle. When enabled, the symbolic bundle name can only resolve to a single version of the bundle, as opposed to being capable of resolving to two or more version having the same bundle name.

    As you mentioned, the tests you performed worked regardless of this setting. This is because Eclipse will only load a single instance of your plugin's activator. In essence, the activator behaves as a singleton class. As a matter of fact, most reference warn that applications should not manually instantiate activator, but rather use the getDefault() method to retrieve a reference.

    I recommend that you let the activator do what it needs to do, rather than forcing it to be your own singleton. Create a separate class for your singleton and define an acessor method in your activator. For example

package com.me.singleton;

public class MySingleton {
    private static final MySingleton ref = new MySingleton();

    private MySingleton() {
        // initialization tasks

    public MySingleton getInstance() {
        return ref;
Define the following accessor method in your activator.

public static MySingleton getMySingleton() {
    return MySingleton.getInstance();
You don't necessarily need this accessor method, as you can get a reference directly using MySingleton.getInstance(). However, the extra layer of abstraction might be beneficial if the "behind-the-scenes" implementation of your actual singleton(s) is still in flux.

Best regards,


AranbarriAuthor Commented:
Dear Frelling:

Thanks for your answer. I could finally manage making a singleton and know it seems to work. It was easy but when you are not "familiarized" with OSGi any little problem becomes a big one.

Thank you very much!

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now