<p>What makes a module/service/bit of application functionality a particularly good candidate for an OSGi module? </p>

<p>I'm interested in using <a href="http://en.wikipedia.org/wiki/OSGi" rel="noreferrer">OSGi</a> in my applications. We're a Java shop and we use Spring pretty extensively, so I'm leaning toward using <a href="http://www.springframework.org/osgi" rel="noreferrer">Spring Dynamic Modules for OSGi(tm) Service Platforms</a>. I'm looking for a good way to incorporate a little bit of OSGi into an application as a trial. Has anyone here used this or a similar OSGi technology? Are there any pitfalls? </p>

<p>@Nicolas - Thanks, I've seen that one. It's a good tutorial, but I'm looking more for ideas on how to do my first "real" OSGi bundle, as opposed to a Hello World example.</p>

<p>@david - Thanks for the link! Ideally, with a greenfield app, I'd design the whole thing to be dynamic. What I'm looking for right now, though, is to introduce it in a small piece of an existing application. Assuming I can pick any piece of the app, what are some factors to consider that would make that piece better or worse as an OSGi guinea pig?</p>

[{'answer_id': 16411, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 1, 'selected': False, 'text': '<p>I really like the <a href="http://felix.apache.org/site/tutorials-examples-and-presentations.html" rel="nofollow noreferrer">Apache Felix tutorials</a>. However, I think in general leveraging OSGi in your application isn\'t one of those "let\'s use this framework, because it\'s hype" decision. It\'s more of a design question, but then everything that OSGi gives you in terms of design, you can have with vanilla Java as well.</p>\n\n<p>As for the runtime, you cannot just add an existing application and make it OSGi enabled. It needs to be design to be dynamic. Spring DM makes it easy to hide that from you, but it\'s still there and you need to be aware of it.</p>\n'}, {'answer_id': 16817, 'author': 'Boris Terzic', 'author_id': 1996, 'author_profile': 'https://Stackoverflow.com/users/1996', 'pm_score': 6, 'selected': True, 'text': '<p>Well, since you can not have one part OSGi and one part non-OSGi you\'ll need to make your entire app OSGi. In its simplest form you make a single OSGi bundle out of your entire application. Clearly this is not a best practice but it can be useful to get a feel for deploying a bundle in an OSGi container (Equinox, Felix, Knoplerfish, etc).</p>\n\n<p>To take it to the next level you\'ll want to start splitting your app into components, components should typically have a set of responsibilities that can be isolated from the rest of your application through a set of interfaces and class dependencies. Identifying these purely by hand can range from rather straightforward for a well designed highly cohesive but loosely coupled application to a nightmare for interlocked source code that you are not familiar with.</p>\n\n<p>Some help can come from tools like <a href="http://clarkware.com/software/JDepend.html" rel="noreferrer">JDepend</a> which can show you the coupling of Java packages against other packages/classes in your system. A package with low efferent coupling should be easier to extract into an OSGi bundle than one with high efferent coupling. Even more architectural insight can be had with pro tools like <a href="http://www.headwaysoftware.com/products/structure101/index.php" rel="noreferrer">Structure 101</a>.</p>\n\n<p>Purely on a technical level, working daily with an application that consists of 160 OSGi bundles and using Spring DM I can confirm that the transition from "normal" Spring to Spring DM is largely pain free. The extra namespace and the fact that you can (and should) isolate your OSGi specific Spring configuration in separate files makes it even easier to have both with and without OSGi deployment scenarios.</p>\n\n<p>OSGi is a deep and wide component model, documentation I recommend:</p>\n\n<ul>\n<li><a href="http://www.osgi.org/Release4/Download" rel="noreferrer">OSGi R4 Specification</a>: Get the PDFs of the Core and Compendium specification, they are canonical, authoritative and very readable. Have a shortcut to them handy at all times, you will consult them.</li>\n<li>Read up on OSGi best practices, there is a large set of things you <strong>can</strong> do but a somewhat smaller set of things you <strong>should</strong> do and there are some things you should <strong>never do</strong> (DynamicImport: * for example). </li>\n</ul>\n\n<p>Some links: </p>\n\n<ul>\n<li><a href="http://felix.apache.org/site/presentations.data/best-practices-apachecon-20060628.pdf" rel="noreferrer">OSGi best practices and using Apache Felix</a></li>\n<li><a href="http://www.osgi.org/wiki/uploads/CommunityEvent2007/OSGiBestPractices.pdf" rel="noreferrer">Peter Kriens and BJ Hargrave in a Sun presentation on OSGi best practices</a> </li>\n<li>one key OSGi concept are Services, learn why and how they supplant the Listener pattern with the <a href="http://www.osgi.org/wiki/uploads/Links/whiteboard.pdf" rel="noreferrer">Whiteboard pattern</a></li>\n<li><strike><a href="http://groups.google.com/group/spring-osgi" rel="noreferrer">The Spring DM Google Group</a> is very responsive and friendly in my experience</strike><br>\n<a href="http://groups.google.com/group/spring-osgi" rel="noreferrer">The Spring DM Google Group</a> is <a href="https://groups.google.com/forum/#!topic/spring-osgi/e-3gVCgl-_M" rel="noreferrer">no longer active</a> and has moved to Eclipse.org as the Gemini Blueprint project which has a forum <a href="http://www.eclipse.org/forums/index.php?t=thread&amp;frm_id=153" rel="noreferrer">here</a>.</li>\n</ul>\n'}, {'answer_id': 17037, 'author': 'Steven Dick', 'author_id': 1809, 'author_profile': 'https://Stackoverflow.com/users/1809', 'pm_score': 2, 'selected': False, 'text': '<p>Is your existing application monolithic or tiered in seperate processes/layers?</p>\n\n<p>If tiered, you can convert the middle/app-tier to run in an OSGi container.</p>\n\n<p>In my team\'s experience, we\'ve found trying to do web-stuff in OSGi painful.  Other pain points are Hibernate and Jakarta Commons Logging.</p>\n\n<p>I find the OSGi specs pretty readable and I recommend you print out the flowchart that shows the algorithm for class loading.  I\'ll guarantee you\'ll have moments of, "why am I getting a NoClassDefFoundError?": the flowchart will tell you why.</p>\n'}, {'answer_id': 46888, 'author': 'Andreas Kraft', 'author_id': 4799, 'author_profile': 'https://Stackoverflow.com/users/4799', 'pm_score': 2, 'selected': False, 'text': '<p>There are a couple of thinks to keep in mind if you are starting with OSGi.</p>\n\n<p>As mentioned elsewhere in this thread, knowing about classloading is really important. In my experience everybody sooner or later runs into problems with it.</p>\n\n<p>Another important thing to remember is: never hold references! Have a look at the whiteboard pattern on which the services concept of OSGi is build (see the link in one of the other answers).</p>\n\n<p>In my experience you should not try to convert a monolitic application into an OSGi-based one. This usually leads to a badly and unmanageable mess. Start anew.</p>\n\n<p>Download one of the freely available stand-alone OSGi implementations. I found Knopflerfish rather good and stable (I use it in many projects). It also comes with lots of source code. You can find it here: <a href="http://www.knopflerfish.org/index.html" rel="nofollow noreferrer">http://www.knopflerfish.org</a></p>\n\n<p>Another good tutorial can be found here. <a href="https://pro40.abac.com/deanhiller/cgi-bin/moin.cgi/OsgiTutorial" rel="nofollow noreferrer">https://pro40.abac.com/deanhiller/cgi-bin/moin.cgi/OsgiTutorial</a></p>\n\n<p>Peter Kriens of the OSGi Alliance gave a nice interview: <a href="http://www.infoq.com/interviews/osgi-peter-kriens" rel="nofollow noreferrer">http://www.infoq.com/interviews/osgi-peter-kriens</a>. His homepage and blog (which is always a good read can be found here: <a href="http://www.aqute.biz" rel="nofollow noreferrer">http://www.aqute.biz</a></p>\n'}, {'answer_id': 144273, 'author': 'Kamlesh', 'author_id': 23024, 'author_profile': 'https://Stackoverflow.com/users/23024', 'pm_score': 2, 'selected': False, 'text': '<p>Try <a href="http://neilbartlett.name/blog/osgibook/" rel="nofollow noreferrer">http://neilbartlett.name/blog/osgibook/</a>. The book has hands on examples with OSGi best practices.</p>\n'}, {'answer_id': 152412, 'author': 'Toni Menzel', 'author_id': 1990802, 'author_profile': 'https://Stackoverflow.com/users/1990802', 'pm_score': 3, 'selected': False, 'text': '<p>When learning a new technology rich tooling gets you into things without big headaches.\nAt this point the community at <a href="http://wiki.ops4j.org/confluence/x/Bg/" rel="noreferrer">ops4j.org</a> provides a rich toolset called "PAX" which includes:</p>\n\n<ul>\n<li><strong>Pax Runner</strong>: Run and switch between Felix, Equinox, Knopflerfish and Concierge easily</li>\n<li><strong>Pax Construct</strong>: Construct, Organize &amp; Build OSGi projects with maven easily</li>\n<li><strong>Pax Drone</strong>: Test your OSGi bundles with Junit while being framework independent (uses PaxRunner)</li>\n</ul>\n\n<p>Then there are many implementations of OSGi compendium services: </p>\n\n<ul>\n<li><strong>Pax Logging</strong> (logging), </li>\n<li><strong>Pax Web</strong> (http service), </li>\n<li><strong>Pax Web Extender</strong> (war support), </li>\n<li><strong>Pax Coin</strong> (configuration), </li>\n<li><strong>Pax Shell</strong> (shell implementation, part of the next osgi release) </li>\n<li>and much more.</li>\n</ul>\n\n<p>.. and there is a helpful, framework independend community, - but thats now advertisement ;-) </p>\n'}, {'answer_id': 5767828, 'author': 'Jérôme Verstrynge', 'author_id': 520957, 'author_profile': 'https://Stackoverflow.com/users/520957', 'pm_score': 3, 'selected': False, 'text': '<p>This answer comes nearly 3 years after the question was asked, but the <a href="http://wso2.org/library/tutorials/develop-osgi-bundles-using-maven-bundle-plugin" rel="noreferrer">link</a> I just found is <strong>really good</strong>, especially for starters using maven. A step-by-step explanation.</p>\n'}, {'answer_id': 17801701, 'author': 'Wazza', 'author_id': 1488389, 'author_profile': 'https://Stackoverflow.com/users/1488389', 'pm_score': 2, 'selected': False, 'text': '<p>Try <a href="http://njbartlett.name/files/osgibook_preview_20091217.pdf" rel="nofollow">http://njbartlett.name/files/osgibook_preview_20091217.pdf</a> </p>\n\n<p>OR </p>\n\n<p><a href="http://www.manning.com/hall/" rel="nofollow">http://www.manning.com/hall/</a></p>\n\n<p>The second is not a book i have read myself but I have heard good things about it. </p>\n\n<p>The first was very useful for me. He takes you through the architecture initially and then it\'s hands on OSGi. </p>\n'}]
['https://Stackoverflow.com/questions/16140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/765/']