Os gi introduction made by Ly MInh Phuong-SOC team
-
Upload
thuydang -
Category
Technology
-
view
704 -
download
0
Transcript of Os gi introduction made by Ly MInh Phuong-SOC team
![Page 1: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/1.jpg)
OSGi Introduction
Ly Minh Phuong – Social Team
![Page 2: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/2.jpg)
2
Objective
OSGi architecture overview Bundles Services Lifecycle
DemoQ&A
OSGi framework Motivation
![Page 3: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/3.jpg)
3
Why OSGi?
Java not support natively dynamic module system.(@2011)
JAR dependences management is missing.
No way to restrict using class in JAR file.
![Page 4: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/4.jpg)
4
Why OSGi?
Java not support natively dynamic module system.(@2011)
![Page 5: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/5.jpg)
5
Why OSGi?
JAR dependences management is missing.
What happened when two jar file have duplicate packages and classes ?
What happened when your classes only work with a library which version < 2.3.0 ?
Can it be prevent in run time ? No way to restrict using class in JAR file.
When writing a lib/component can we control what user can import or not ?
![Page 6: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/6.jpg)
6
Why OSGi?
Resolve in OSGi way.
![Page 7: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/7.jpg)
7
What is OSGi
OSGi alliance
Open Service Gateway initiative
Founded 1999
Specification first released in 2000
Newest version of specification is 4.3 (April 2011)
Target: embedded, desktop, enterprise application.
Widely adopted: Eclipse Equinox, Apache Felix, Glassfish, Knopflerfish, ProSyst, Hitachi SuperJ Engine, …
![Page 8: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/8.jpg)
8
Basic architect
![Page 9: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/9.jpg)
9
Bundle
Bundle
Basic deployment entity ( app, component, library)
Version convention ( major.minor.micro.qualifier )
Specify the dependence, export package, activator
Package like JAR file and added the bundle meta info
Bundle-ManifestVersion: 2Bundle-Name: Greeting ClientBundle-SymbolicName: org.foo.hello.clientBundle-Version: 1.2.1.SNAPSHOT
![Page 10: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/10.jpg)
10
Bundle Lifecycle
![Page 11: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/11.jpg)
11
Bundle dependency resolution The framework prefer already resolved bundle
If both are resolved the framework prefer highest matching version.
If the version is equal it's prefer the older one.
![Page 12: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/12.jpg)
12
Bundle Activator Bundle Activator
Define by Bundle-Activator header
Handle when bundle start/stop
public final class Activator implements BundleActivator{ public void start( BundleContext bc ) throws Exception{ .... } public void stop( BundleContext bc ) throws Exception{ .... }}
![Page 13: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/13.jpg)
13
Bundle Mainifest example
Bundle-ManifestVersion: 2Bundle-Name: Greeting ClientBundle-SymbolicName: org.foo.hello.clientBundle-Version: 1.2.1.SNAPSHOTBundle-Activator: org.foo.hello.client.Activator Import-Package: org.foo.hello;version="[1.0,2.0)"Export-Package: org.org.foo.hello.api;version="1.0"Bundle-ClassPath: .,WEB-INF/classes,WEB-INF/lib/a.jar
![Page 14: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/14.jpg)
14
Service
Service
Bundle can register/unregister service using bundle activator.
One service name can have multi provider
Service can have properties to describe the service info
![Page 15: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/15.jpg)
15
Registering a service
Programmatically using Activator
Dictionary properties = new Properties();properties.setProperty("BookStorageType", "MySQL");
bundleContext.registerService(BookStorage.class.getName(), new BookStorageImpl(), properties );
![Page 16: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/16.jpg)
16
Consuming a service
Programatically using bundleContext.getSerivce()ServiceReference bookStorageRef = bc.getServiceReference(BookStorage.class.getName());if(bookStorageRef != null){
bookStorage = (BookStorage) bc.getService(bookStorageRef);System.out.println("Got BookStorage Service");
}
Use Service ListenerActivator.javaString filter = "(" + Constants.OBJECTCLASS + "=" + BookStorage.class.getName() + ")";context.addServiceListener(new BookStorageListener(), filter);
BookStorageListener.javaclass BookStorageListener implements ServiceListener { public void serviceChanged(ServiceEvent event) { .... }}
![Page 17: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/17.jpg)
17
Service consuming
Use Service Tracker
public class XyzServiceTracker extends ServiceTracker { public XyzServiceTracker(BundleContext context) { super(context, ServiceToTrack.class.getName(), null); ... } public Object addingService(ServiceReference reference) { ... } public void removedService(ServiceReference reference, Object service) { context.ungetService(reference); ... }}
![Page 18: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/18.jpg)
18
Problems about OSGi
OSGi look great but tool and development process not widely public yet.
Not every lib is bundle.
Version management can be nightmare.
Runtime dynamic can result untested situation.
![Page 19: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/19.jpg)
19
DEMO
![Page 20: Os gi introduction made by Ly MInh Phuong-SOC team](https://reader035.fdocuments.us/reader035/viewer/2022070315/554f4ff5b4c905423f8b517b/html5/thumbnails/20.jpg)
20
Q&A