OSGi bundle -> Singleton

Posted on 2009-12-23
Last Modified: 2013-12-15
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 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 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 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,

Question by:Aranbarri
    LVL 4

    Accepted 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


    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,



    Author Closing Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now