日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

osgi实战学习之路:6. Service-1

發布時間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osgi实战学习之路:6. Service-1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是Service?


它是注冊到osgi的一個java對象


Service注冊:


通過BundleContext::registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)?

Service查找及使用:


通過BundleContext::getServiceReference(java.lang.String clazz),返回ServiceReference 通過BundleContext::getService(ServiceReference reference) ,返回注冊的服務對象

Service釋放:


通過BundleContext::ungetService(ServiceReference reference)


LADP:


?輕量級文件夾訪問協議(Lightweight Directory Access Protocol)



1個Service相應一個實現類的注冊與使用demo:


服務提供者:

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("注冊服務開始....");context.registerService(IStudentManage.class.getName(),new StudentManage(), null);System.out.println("注冊服務結束....");}public void stop(BundleContext context) throws Exception {}}
服務使用者:

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 {//查找Servicesf=context.getServiceReference(IStudentManage.class.getName());//調用服務IStudentManage studentManage = (IStudentManage)context.getService(sf);studentManage.add();} finally {context.ungetService(sf);}System.out.println("action end...");}public void stop(BundleContext context) throws Exception {}}
部署至karaf并查看結果:




一個Service相應多個實現(基于LDAP)demo2


服務提供者

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("注冊服務開始....");//注冊AHashtable<String, String> dict=new Hashtable<String, String>();dict.put("name", "a");context.registerService(IStudentManage.class.getName(),new StudentManageA(), dict);//注冊Bdict=new Hashtable<String, String>();dict.put("name", "b");context.registerService(IStudentManage.class.getName(),new StudentManageB(), dict);System.out.println("注冊服務結束....");}public void stop(BundleContext context) throws Exception {}}
服務使用者

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("服務調用------------------");//查找ServiceString filter="(name=b)";references=context.getServiceReferences(IStudentManage.class.getName(), filter);//調用服務if(references.length==1){IStudentManage studentManage = (IStudentManage)context.getService(references[0]);studentManage.add();}else {throw new IllegalArgumentException("IStudentManage查找到多個實現類");}} finally {for(ServiceReference sf:references){context.ungetService(sf);}}System.out.println("action end...");}public void stop(BundleContext context) throws Exception {}}
部署到karaf及查看結果:




轉載于:https://www.cnblogs.com/wzjhoutai/p/6734965.html

總結

以上是生活随笔為你收集整理的osgi实战学习之路:6. Service-1的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。