Can OSGi components be deployed in any Servlet containers? - osgi

Can OSGi components be deployed to any Container like Tomcat/Websphere/JBoss..etc ? (just as we deploy a WAR file) or do they require any special libraries or OSGi containers to run the OSGi components ?

If you want just expose the "component" classes, you can treat the jar files as .. jar files.
If you consider the OSGi "component" as a "bundle" exposing some services and doing something, then answer is "no". You need an OSGi container (Karaf, WebSphere Liberty, Carbon, Glassfish, JBoss WildFly ...).
However - as you can see, some application servers provide OSGi support natively.

OSGi components (I guess you mean bundles) require an OSGi framework to run in. So a plain servlet container will not work. Some versions of JBoss provide support for OSGi though.

Related

Vaadin + Spring Boot + Osgi

Does some one already managed to setup a Vaadin (19 to 21) Web application using Spring Boot and Osgi (as bundle) ?
I've managed to have a Vaadin application running under Osgi container (Karaf).
I've managed to have a Spring Boot Application running under Osgi container (Karaf).
But not both at the same time.
In fact, when trying, no route are being registered and FixedVaadinServlet is never called.
Thanks for help.
I have to say that I don't exactly understand your question since Spring is not OSGi compatible (it just has no OSGi manifest) so I don't see how it may work at all.
Now I'm going to guess....
I've managed to have a Vaadin application running under Osgi container (Karaf).
So your application is built as an OSGi bundle and you have deployed it to Karaf (OSGI container) and it works.
I've managed to have a Spring Boot Application running under Osgi container (Karaf).
You have built something (either WAR or a Spring boot Jar) which is a Web application and you have deployed it to Karaf AS A WEB Application.
This feature provided by Karaf : it's not pure OSGi container. It allows to deploy WEB applications to it.
These Web Applications have no any relation to OSGi.
So you have no Spring application which works in OSGi: if you use another OSGi container (which doesn't support extra features like Web app deployment) then you won't be able to use Spring there since as I said Spring is not OSGi compatible.
So I don't see how it's possible to use Spring + OSGi at all.

Spring Boot App - Reference external code during runtime that isn't part of the bundled WAR?

I have a Spring Boot+React application that is packaged as a bundled WAR.
This works well for most cases, but we need to be able to drop-in functionality in some cases that is not part of the bundle (such as via a JAR).
I knew OSGI exists for this case, but not sure of any usage with Spring Boot. Is there another way to do this as well?
If your use case is a kind of small plugin functionality for your spring boot app then you could start an OSGi framework inside spring boot and load bundles from a separate directory. You plugins could then offer their service via an interface that is provided by the spring boot app and an OSGi service with that interface.
You need some good OSGi knowledge for this to work.

Can I deploy a fab bundle on any OSGI-compliant container/application server?

I am currently trying to deploy my camel app as a FAB bundle in the Fuse ESB container. I understand that I can install Fab(Fabric-bundle) as a feature on OSGI containers like Karaf or felix and deploy the bundle. I want to know if this feature is available on all OSGI containers as well as other application servers like IBM Websphere(Websphere supports OSGI) or will this tie me up to the Fuse ecosystem.
Will deploying as a WAR or an OSGI bundle be a better approach as the goal is to support as many ESB Containers/Application Servers as possible.
No, this is a specialty of Fuse, for this it'll only work on the FUSE containers. Even Vanilla Karaf doesn't understand FAB (unless it's a standard Karaf feature, with just another name to it)
For usage of Karaf Featurs outside of Karaf you'd need to add the feature resolver and services of Karaf to the other OSGi containers first. Never tried that but might be possible.

difference between WAB and WAR

I am a newbie to this and read about WABs , but wish to clear the basic difference -
I mean using osgi embedded in tomcat and making a WAR vs making a WAB ?
When should one consider each option ?
1) OSGI embedded in tomcat
2) tomcat in OSGI
3) using a WAB
OSGi embedded in a container (not only Tomcat!) is likely the only option when you are forced to a traditional JavaEE WAR deployment model, i.e. an IT department operates the container and you can only deloy WAR files to it. This bootstraps a whole OSGi framework within the web application and allows modular development within the web application. The web application is then composed as a set of OSGi bundles. It can also be used to migrate/transfer an existing legacy web application into OSGi modules. However, this will be challenging.
I'd like to call the second approach (Tomcat in OSGi) as a pure OSGi approach. Tomcat or any other Servlet container (eg., Jetty) can be deployed as a bundle (or a set of bundles) in an OSGi framework. The OSGi framework is the container. You don't have the full separation of a web application anymore. The can intersect. Some bundles/modules may implement web functionality and others may not. Core functionality (core bundles) can be reused by other web bundles.
The third option is a result of new spec work in OSGi. Basically, it's a web application with an OSGi bundle manifest. Thus, the whole web application can be deployed as a single OSGi bundle on any framework with WAB support. Technically, the bundle may be deployed as a web application to a Servlet container. But it gets access to a BundleContext. This allows the web application to inter-operate with other bundles or web applications running in the same framework.

OSGi bundles and wars

Is it possible to have standard war deployment, which can be deployed on tomcat and also can be build as OSGi bundle and deployed with other bundles in OSGi container tomcat(i think virgo)?
Yes, there's a good deal of interoperability between WARs and WABs. Apache Aries and WebSphere Application Server will convert WARs to WABs on deployment. This is a good way to get up and running, but it's a better practice to use proper WABs in which you build in the OSGi metadata yourself. The extra OSGi headers won't interfere with the deployment in a normal Tomcat container, so the WAB has the greatest flexibility.
For your build, you have a number of options. For example, the maven bundle plugin can be configured to build WABs, or you can use Eclipse PDE's tooling support for OSGi metadata.
Pax-Url-War provides this functionality to containers like Apache Karaf. In brief, this allows you to import an URL like war:file:///path/to/myapp.war and Pax will wrap it as an OSGi bundle, optionally changing the URL root and other parameters on the fly.

Resources