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

29
1 Inter module communication in DX 7.2 using OSGi Wednesday 22th, 2017

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

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

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

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

3

Use Services to communicate between

modules.

1

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

4

1 What kind of service?

- Spring services

- OSGi services

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

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

6

Get a service from DX.

2

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

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

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

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

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

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

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

13

Create and expose Services

3

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

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

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

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

17

4Consume an OSGi service from another module

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

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

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

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

21

5Advanced concepts

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

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

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

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

25

Tools

- OSGi web console

- Karaf shell

5

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

26

Conclusion

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

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

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

29

THE END!THANKS FOR LISTENING