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: