Liferay 7.1 CE GA1 OSGi Service / Portlet / Maven sample

Hi everybody,

With the version 7.X we have all started to play with OSGi and for some projects it's sometimes hard to have a clean start with the correct build tool; despite the blade sample which are definitely useful.

I wanted to share through this blog, a sample with an OSGi service and a portlet which are configured to be deployed in a Liferay 7.1.

The code given can be compiled with maven : https://www.dropbox.com/sh/2ohrojsmtuzyev0/AAAqsroA_25aUYx3LEJbHsYsa?dl=0

A repository has to be added into the settings.xml : https://repository.liferay.com/nexus/content/repositories/liferay-public-releases/ to be able to find the version 3.0.0 of the liferay kernel.

 

Let's see some details on the sample given :

1 - OSGi service module

In this module we have an interface called OsgiService and his implementation OsgiServiceImpl.

The OsgiService is into the package xxx.api which is declared as export package into the bnd.

The MANIFEST.MF generated once the compilation is done :

Manifest-Version: 1.0
Bnd-LastModified: 1535096462818
Bundle-ManifestVersion: 2
Bundle-Name: osgi-simple-module
Bundle-SymbolicName: com.fr.nixial.osgi-simple-module
Bundle-Version: 0.0.1.201808240741
Created-By: 1.8.0_111 (Oracle Corporation)
Export-Package: com.fr.nixial.osgi.simple.api;version="0.0.1"
Import-Package: com.fr.nixial.osgi.simple.api
Private-Package: com.fr.nixial.osgi.simple.impl
Provide-Capability: osgi.service;objectClass:List<String>="com.fr.nixi
 al.osgi.simple.api.OsgiService"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/com.fr.nixial.osgi.simple.impl.OsgiService
 Impl.xml
Tool: Bnd-3.2.0.201605172007

 

When the OSGi service module is started in liferay, you will be able to see the service is correctly exposed (felix webconsole view) :

With the Service ID 9245, you can see the type of the service exposed and the implementation below.

 

2 - OSGi portlet

The portlet is a MVCPortlet with jsp where we inject the OsgiService and we called the log method on the portlet default render.

Let's see the MANIFEST.MF of the portlet :

Manifest-Version: 1.0
Bnd-LastModified: 1535097259074
Bundle-ManifestVersion: 2
Bundle-Name: osgi-simple-portlet
Bundle-SymbolicName: com.fr.nixial.osgi-simple-portlet
Bundle-Version: 0.0.1.201808240754
Created-By: 1.8.0_111 (Oracle Corporation)
Import-Package: com.liferay.portal.kernel.portlet.bridges.mvc;version=
 "[1.5,2)",javax.portlet;version="[2.0,3)",com.fr.nixial.osgi.simple.a
 pi;version="[0.0,1)",com.liferay.portal.kernel.log;version="[7.0,8)",
 javax.servlet;version="[3.0,4)",javax.servlet.http;version="[3.0,4)"
Private-Package: com.fr.nixial.environment.web,com.fr.nixial.environme
 nt.web.model,content
Provide-Capability: osgi.service;objectClass:List<String>="javax.portl
 et.Portlet",liferay.resource.bundle;bundle.symbolic.name="com.fr.nixi
 al.osgi-simple-portlet";resource.bundle.base.name="content.Language"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=jsp.taglib
 )(uri=http://java.sun.com/portlet_2_0))",osgi.extender;filter:="(&(os
 gi.extender=jsp.taglib)(uri=http://liferay.com/tld/aui))",osgi.extend
 er;filter:="(&(osgi.extender=jsp.taglib)(uri=http://liferay.com/tld/p
 ortlet))",osgi.extender;filter:="(&(osgi.extender=jsp.taglib)(uri=htt
 p://liferay.com/tld/theme))",osgi.extender;filter:="(&(osgi.extender=
 jsp.taglib)(uri=http://liferay.com/tld/ui))",osgi.extender;filter:="(
 &(osgi.extender=osgi.component)(version>=1.3.0)(!(version>=2.0.0)))",
 osgi.service;filter:="(objectClass=com.fr.nixial.osgi.simple.api.Osgi
 Service)";effective:=active,osgi.ee;filter:="(&(osgi.ee=JavaSE)(versi
 on=1.8))"
Service-Component: OSGI-INF/com.fr.nixial.environment.web.OsgiSimplePo
 rtlet.xml
Tool: Bnd-3.2.0.201605172007

 

In the Apache Felix webconsole, we can see the OsgiService is a service used by the portlet and satisfy by the osgi simple module (service #9245) :

 

I hope this quick tutorial will help some of you to your next development on Liferay 7.X.

Feel free to add comments.

 

Best regards,

David.

Blogs

I am not familiar with maven and I don't see how you connect both projects and why you didn't put them into one repository? Could you be so kind and give me some more insights.

Hi Jan, The goal of this sample is to show how to expose an osgi service which can be deployed as standalone component and expose a list of method and a consumer like the portlet given. This way through osgi, you could build your system with the idea of microservices and consume them from outside. Does it gives you sufficient information ? Regards, David.

Hi, I just have a question. 

I have  an OSGI based Portlet, which is using an other OSGI Service, if I change something in the Service or in the Portlet Eclipse redeploy the changed component, but the portlet gets unusable. (undeployed) so I have to restart the whole server.  What can be wrong with the server or the OSGI projects. It was not the first project with that issue, I have also a rest OSGI Module, which also 

gets unavailable for calls.

 

So must be something in the setup.