Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017

Post on 11-Apr-2017

52 views 3 download

Transcript of Communication Inter-Module with OSGi (DX 7.2) - Developers Meetup - February 2017

1

Inter module communication in DX 7.2 using OSGiWednesday 22th, 2017

2

Introduction

What will you see?Some slides about OSGi Some code of DX bundlesPeople talking

There are 29 slides

3

Use Services to communicate between

modules.

1

4

1 What kind of service?

- Spring services

- OSGi services

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

6

Get a service from DX.

2

7

2 How services are exposed by DX

- Spring beans- OSGi services (since

7.2)

8

2 Sample of available services

- Site service- Publication service- Categories service- Mail Service

9

2 How to get Services

Spring injection

<bean id="myBean" class="org.jahia.modules.Myclass" > <property name="jcrStoreService" ref="JCRStoreService"/></bean>

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>

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;

12

2 How to get Services

DEMO

A DX module that uses the mail service to send a mail on a submitted form

13

Create and expose Services

3

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>

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{

16

3 Expose service

DEMOModule that exposes a Mail Template service. This service provides methods to wrap the mail content before sending it.

17

4Consume an OSGi service from another module

18

Consume OSGi service

Same as DX service, using OGSi reference.

Can be done using blueprint or annotations

4

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

20

Consume OSGi service

Demo

Use the Template Mail service in the send mail module instead of DX mail service.

4

21

5Advanced concepts

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

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

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

25

Tools

- OSGi web console

- Karaf shell

5

26

Conclusion

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

28

Mix the things

Sources of the modules are available here:https://github.com/Jahia/inter-module-communication-demo

29

THE END!THANKS FOR LISTENING