![Page 1: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/1.jpg)
1
Inter module communication in DX 7.2 using OSGiWednesday 22th, 2017
![Page 2: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/2.jpg)
2
Introduction
What will you see?Some slides about OSGi Some code of DX bundlesPeople talking
There are 29 slides
![Page 3: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/3.jpg)
3
Use Services to communicate between
modules.
1
![Page 4: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/4.jpg)
4
1 What kind of service?
- Spring services
- OSGi services
![Page 5: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/5.jpg)
5
1 What are OSGi Services?
Definition:An Interface (service interface) with its implementation registered in the OSGi service registry
An OSGi service can be:- Started- Stopped- Configured- Called from anywhere
![Page 6: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/6.jpg)
6
Get a service from DX.
2
![Page 7: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/7.jpg)
7
2 How services are exposed by DX
- Spring beans- OSGi services (since
7.2)
![Page 8: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/8.jpg)
8
2 Sample of available services
- Site service- Publication service- Categories service- Mail Service
![Page 9: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/9.jpg)
9
2 How to get Services
Spring injection
<bean id="myBean" class="org.jahia.modules.Myclass" > <property name="jcrStoreService" ref="JCRStoreService"/></bean>
![Page 10: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/10.jpg)
10
2 How to get Services
Blueprint
<osgi:reference id="myService" interface="org.jahia.modules.MyServiceInterface"
/>
<bean id="myBean" class="org.jahia.modules.Myclass" > <property name="myService" ref="myService"/></bean>
![Page 11: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/11.jpg)
11
2 How to get Services
OSGi Annotations @Component(...)public class MyClass implements MyInterface {...@Reference(service = MyService.class)protected void bindMyService(MyService myService) { this.myService = myService;}private MyService myService;
![Page 12: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/12.jpg)
12
2 How to get Services
DEMO
A DX module that uses the mail service to send a mail on a submitted form
![Page 13: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/13.jpg)
13
Create and expose Services
3
![Page 14: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/14.jpg)
14
3 Create and Expose service
Using blueprint
<osgi:service interface="org.jahia.myService"> <osgi:service-properties> <entry key="Service Description" value="my OSGi Service"/> <entry key="Service Vendor" value="Jahia Solutions Group SA"/> </osgi:service-properties> <bean class="org.jahia.myServiceImpl"/></osgi:service>
![Page 15: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/15.jpg)
15
3 Expose service
Using OSGi annotation
@Component(name = "org.jahia.myService", service = MyService.class, property = { Constants.SERVICE_PID + "=org.jahia.myService", Constants.SERVICE_DESCRIPTION + "=my OSGi Service", Constants.SERVICE_VENDOR + "=" + Jahia.VENDOR_NAME}, immediate = true)public class MyServiceImpl implements MyService{
![Page 16: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/16.jpg)
16
3 Expose service
DEMOModule that exposes a Mail Template service. This service provides methods to wrap the mail content before sending it.
![Page 17: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/17.jpg)
17
4Consume an OSGi service from another module
![Page 18: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/18.jpg)
18
Consume OSGi service
Same as DX service, using OGSi reference.
Can be done using blueprint or annotations
4
![Page 19: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/19.jpg)
19
Consume OSGi service
Handle import export.
- Export the Interface package to expose the service
- Import is done by the maven-bundle-plugin while building the module.
4
![Page 20: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/20.jpg)
20
Consume OSGi service
Demo
Use the Template Mail service in the send mail module instead of DX mail service.
4
![Page 21: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/21.jpg)
21
5Advanced concepts
![Page 22: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/22.jpg)
22
List of services that match an interface
OSGi list, is a blueprint mechanism.
- Provides a dynamic list of services that match an interface.
- The list is automatically updated on service availability
DEMO
5
![Page 23: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/23.jpg)
23
Services listeners
Service tracker:Can be set to perform operations on service add /
remove or modified on a filtered list of services.
Service listener:All events related to services can be catch to
execute custom code
![Page 24: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/24.jpg)
24
Other way of communication
Activators:Atcs at bundle startup, can register listener that will
react on other bundle activation.
OSGi Listeners:This allows to react on bundle states change
5
![Page 25: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/25.jpg)
25
Tools
- OSGi web console
- Karaf shell
5
![Page 26: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/26.jpg)
26
Conclusion
![Page 27: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/27.jpg)
27
Mix the things
Use OSGi services instead of Spring bean injection
OSGi services have been designed for that purpose.
It solves a lot of Spring injection issue:- Class loading- Module order startup and dependencies
![Page 28: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/28.jpg)
28
Mix the things
Sources of the modules are available here:https://github.com/Jahia/inter-module-communication-demo
![Page 29: Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017](https://reader035.vdocuments.net/reader035/viewer/2022070600/58e4f1bc1a28ab87378b641d/html5/thumbnails/29.jpg)
29
THE END!THANKS FOR LISTENING