通过ServiceTracker能够对查找的Service进行扩展

以下的demo引入装饰器模式对Service进行日志的扩展

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();//获取服务IStudentManage service=(IStudentManage)managerTracker.getService();service.add();System.out.println("action start end...");}public void stop(BundleContext context) throws Exception {//关闭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...");}}

结果:

转载于:https://www.cnblogs.com/bhlsheji/p/4255364.html

osgi实战学习之路:8. Service-3之ServiceTracker相关推荐

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

    什么是Service? 它是注冊到osgi的一个java对象 Service注冊: 通过BundleContext::registerService(java.lang.String[] clazze ...

  2. osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld

    开发环境分为三个部份 osgi_provider: bundle开发环境,对外提供服务 osgi_consumer: 引用其它bundle osgi_main: 运行测试 项目主要内容 : commo ...

  3. 大数据学习之路(转载)

    #大数据学习之路(转载) 博文地址https://blog.csdn.net/zys_1997/article/details/78358992 看到一个博主写的大数据学习路线,看了比较心动,想着自己 ...

  4. 送你九年经验,我的Java学习之路你也可以复制

    这篇文章写的非常认真,足足花了两周时间,不是简单的资料聚合,我将多年的工作和学习经验写下来了,相信看完后你能有一种豁然开朗的感觉,这就是我要达到的目的,希望不要被打脸. 最近身边很多人在问:Java ...

  5. SpringCloud学习之路

    SpringCloud学习之路 1.使用IDEA搭建Eureka服务中心Server端启动 1.1.创建和配置注册中心Eureka 1.2.使用Eureka案例 1.3.负载均衡器Ribbon 1.4 ...

  6. Rasa 3.X 智能对话机器人案例开发硬核实战高手之路 (7大项目Expert版本)

    课程标题:Rasa 3.X 智能对话机器人案例开发硬核实战高手之路(7大项目Expert版本) 课程关键字:Rasa Application.Debugging.E-commerce.Retail.C ...

  7. 拿下斯坦福和剑桥双offer,00后的算法学习之路

    董文馨,00后,精通英语,西班牙语.斯坦福大学计算机系和剑桥大学双Offer,秋季将进入斯坦福大学学习. 10岁开始在国外上学:12岁学Scratch: 13岁学HTML & CSS: 14岁 ...

  8. JavaWeb学习之路——SSM框架之Spring(五)

    前情提要请看JavaWeb学习之路--SSM框架之Spring(四)                                         整合Spring和Mybatis框架 1.在项目的 ...

  9. JavaWeb学习之路——SSM框架之Mybatis(三)

    数据库配置和相关类创建看上篇:JavaWeb学习之路--SSM框架之Mybatis(二) https://blog.csdn.net/kuishao1314aa/article/details/832 ...

最新文章

  1. [毕业生的商业软件开发之路]尽早暴露错误原则
  2. WebStorm 2017.1增加对Vue.js的支持
  3. 【转】.net异步性能测试(包括ASP.NET MVC WebAPI异步方法)
  4. String特殊值的判断方式
  5. Python3.6 所有内置函数
  6. FISCO BCOS源码(2)启动顺序
  7. 计算机内加减法的溢出处理
  8. matlab二重积分有奇异点,用MATLAB计算某些区域上二重积分.pdf
  9. BDF 字体文件格式
  10. 软考信息系统项目管理师考试难度大概是怎样的?
  11. 光驱位改装固态硬盘并装win 和linux双系统
  12. oracle remap schema,impdp的remap_schema选项的另一个schema是否要重建
  13. mac+微信打开连接到服务器,MAC OS系统 ,微信接收到的文件,打开wo… - Apple 社区...
  14. 获取淘宝订单的解决方案
  15. 阿里云服务器ECS 搭建网站
  16. 国内外常用公共NTP网络时间同步服务器地址
  17. 服务器定时任务是通过什么样的方式实现的
  18. 手机拍照中的防抖功能
  19. 编程小白和大神都想要的百元级物理外挂(装逼利器)----KeyPad++编程键盘
  20. 昆仑通泰(MCGS)官方资料

热门文章

  1. csharp:Chart
  2. [ 墨者学院 ] 命令执行——Bash漏洞分析溯源
  3. Thinking in work
  4. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
  5. 实验6 在应用程序中播放音频和视频
  6. SharePoint 2013 商城
  7. 写代码如坐禅:你是哪一类程序员?
  8. 使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录
  9. 世界上最经典的25句话
  10. Docker Cgroups