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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

osgi实战学习之路:8. Service-3之ServiceTracker

發(fā)布時間:2025/6/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osgi实战学习之路:8. Service-3之ServiceTracker 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
通過ServiceTracker能夠?qū)Σ檎业腟ervice進(jìn)行擴(kuò)展

以下的demo引入裝飾器模式對Service進(jìn)行日志的擴(kuò)展

demo:

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.StudentManage;public class Activator implements BundleActivator {public void start(BundleContext context) throws Exception {System.out.println("register service start...");Dictionary<String, String> prop=new Hashtable<String, String>();prop.put("action", "student_action");context.registerService(IStudentManage.class.getName(), new StudentManage(), prop);System.out.println("register service end...");}public void stop(BundleContext context) throws Exception {}}

Consumer

student-action/Activator.java

package com.demo.action;import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext;import com.demo.action.log.LogStudentManager; import com.demo.action.tracker.StudentManagerTracker; import com.demo.service.IStudentManage;public class Activator implements BundleActivator{StudentManagerTracker managerTracker ;public void start(BundleContext context) throws Exception {System.out.println("action start begin...");managerTracker=new StudentManagerTracker(context);//開啟managerTracker.open();//獲取服務(wù)IStudentManage service=(IStudentManage)managerTracker.getService();service.add();System.out.println("action start end...");}public void stop(BundleContext context) throws Exception {//關(guān)閉managerTracker.close();}}

student-action/StudentManagerTracker.java

package com.demo.action.tracker;import org.omg.PortableInterceptor.INACTIVE; import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer;import com.demo.action.log.LogStudentManager; import com.demo.service.IStudentManage;public class StudentManagerTracker extends ServiceTracker {public StudentManagerTracker(BundleContext context) {super(context, IStudentManage.class.getName(), null);}@Overridepublic Object addingService(ServiceReference reference) {IStudentManage manage=new LogStudentManager(context, reference);return manage;}@Overridepublic void open() {super.open();}}

student-action/LogStudentManager.java

package com.demo.action.log;import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference;import com.demo.service.IStudentManage;public class LogStudentManager implements IStudentManage {IStudentManage studentManage;BundleContext context;ServiceReference reference;public LogStudentManager(BundleContext context, ServiceReference reference) {this.context = context;this.reference = reference;}public void add() {studentManage=(IStudentManage) context.getService(reference);System.out.println("log start...");studentManage.add();System.out.println("log end...");}}


結(jié)果:




轉(zhuǎn)載于:https://www.cnblogs.com/bhlsheji/p/4255364.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。