Osgi practical learning path: 6. Service-1, osgiservice-1
What is Service?
It is a java object registered to osgi.
Service Registration:
Through BundleContext: registerService (java. lang. String [] clazzes, java. lang. Object service, java. util. Dictionary properties)
Service search and use:
Through BundleContext: getServiceReference (java. lang. String clazz), return ServiceReference through BundleContext: getService (ServiceReference reference), return the registered service object
Service Release:
Use BundleContext: ungetService (ServiceReference reference)
LADP:
Lightweight Directory Access Protocol)
One Service corresponds to the registration and use demo of an implementation class:
Service provider:
Student-manage/IStudentManage. java
package com.demo.service;public interface IStudentManage {void add();}
Student-manage/Activator. java
Package com. demo. service; import java. util. dictionary; import java. util. hashMap; import java. util. hashtable; import java. util. map; import org. osgi. framework. bundleActivator; import org. osgi. framework. bundleContext; import com. demo. service. impl. studentManage; public class Activator implements BundleActivator {public void start (BundleContext context) throws Exception {System. out. println ("registration service starts .... "); context. registerService (IStudentManage. class. getName (), new StudentManage (), null); System. out. println ("registration service ends .... ");} public void stop (BundleContext context) throws Exception {}}
Service User:
Student-action/Activator. java
Package com. demo. action; import org. osgi. framework. bundleActivator; import org. osgi. framework. bundleContext; import org. osgi. framework. serviceReference; import com. demo. service. IStudentManage; public class Activator implements BundleActivator {public void start (BundleContext context) throws Exception {System. out. println ("action begin... "); ServiceReference sf = null; try {// search Servicesf = context. getServiceReference (IStudentManage. class. getName (); // call the service IStudentManage studentManage = (IStudentManage) context. getService (sf); studentManage. add ();} finally {context. ungetService (sf);} System. out. println ("action end... ");} public void stop (BundleContext context) throws Exception {}}
Deploy to karaf and view the result:
One Service corresponds to multiple implementations (based on LDAP) demo2
Service Provider
Student-manage/Activator. java
Package com. demo. service; import java. util. dictionary; import java. util. hashMap; import java. util. hashtable; import java. util. map; import org. osgi. framework. bundleActivator; import org. osgi. framework. bundleContext; import com. demo. service. impl. studentManageA; import com. demo. service. impl. studentManageB; public class Activator implements BundleActivator {public void start (BundleContext context) throws Exception {System. out. println ("registration service starts .... "); // register AHashtable <String, String> dict = new Hashtable <String, String> (); dict. put ("name", "a"); context. registerService (IStudentManage. class. getName (), new StudentManageA (), dict); // register Bdict = new Hashtable <String, String> (); dict. put ("name", "B"); context. registerService (IStudentManage. class. getName (), new StudentManageB (), dict); System. out. println ("registration service ends .... ");} public void stop (BundleContext context) throws Exception {}}
Service User
Student-action/Activator. java
Package com. demo. action; import org. osgi. framework. bundleActivator; import org. osgi. framework. bundleContext; import org. osgi. framework. serviceFactory; import org. osgi. framework. serviceReference; import com. demo. service. IStudentManage; public class Activator implements BundleActivator {public void start (BundleContext context) throws Exception {System. out. println ("action begin... "); ServiceReference [] references = null; try {System. out. println ("service call ----------------"); // search ServiceString filter = "(name = B)"; references = context. getServiceReferences (IStudentManage. class. getName (), filter); // call the service if (references. length = 1) {IStudentManage studentManage = (IStudentManage) context. getService (references [0]); studentManage. add ();} else {throw new IllegalArgumentException ("multiple implementation classes found in IStudentManage") ;}} finally {for (ServiceReference sf: references) {context. ungetService (sf) ;}} System. out. println ("action end... ");} public void stop (BundleContext context) throws Exception {}}
Deploy to karaf and view the result:
Operating system version: Windows 7 (Service Pack 1 Builder 617601) [QVOD installation directory]: C: \ QvodPlayer \ [File
Uninstall the existing version and install the new version. Do not press it on the system disk.
Add the parameter-Dorgosgiservicehttpport = 8080 on the VM arguments to start the program. Will the program automatically end?
The eclipse method is not-D, but vmargs. It is recommended to check the help of eclipse.