通过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);
} @Override
public Object addingService(ServiceReference reference) {
IStudentManage manage=new LogStudentManager(context, reference);
return manage;
} @Override
public 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...");
} }

结果:

osgi实战学习之路:8. Service-3之ServiceTracker的更多相关文章

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

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

  2. osgi实战学习之路:3. osgi分层概念及相互合作demo

    源码下载 分层: modual: 主要作用于包级管理与共享代码 lifecycle: 主要作用于执行期间的模块管理与訪问osgi底层框架 service: 主要作用于多模块之间的相互通信 demo: ...

  3. osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo

    生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在执行期间,为应用程序提供操作osgi框架的方法 Bundle 代 ...

  4. osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld

    环境准备: jdk版本号 jdk:1.7 karaf: 版本号:apache-karaf-3.0.1 下载地址: http://pan.baidu.com/s/1qWM4Y1u http://kara ...

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

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

  6. osgi实战学习之路:4.Bundle

    </pre></h1><h1 style="margin:0 0 0 40px; border:none; padding:0px"><p ...

  7. Salesforce学习之路(十三)Aura案例实战分析

    Aura相关知识整合: Salesforce学习之路(十)Aura组件工作原理 Salesforce学习之路(十一)Aura组件属性<aura:attribute /> Salesforc ...

  8. GitHub标星8k,字节跳动高工熬夜半月整理的“组件化实战学习手册”,全是精髓!

    前言 什么是组件化? 最初的目的是代码重用,功能相对单一或者独立.在整个系统的代码层次上位于最底层,被其他代码所依赖,所以说组件化是纵向分层. 为什么要使用组件化? 当我们的项目越做越大的时候,有时间 ...

  9. RPC远程过程调用学习之路(一):用最原始代码还原PRC框架

    RPC: Remote Procedure Call 远程过程调用,即业务的具体实现不是在自己系统中,需要从其他系统中进行调用实现,所以在系统间进行数据交互时经常使用. rpc的实现方式有很多,可以通 ...

随机推荐

  1. LeapMotion 简介

    Leap Motion Overview Leap Motion是一种检测和跟踪hands, fingers and finger-like tools的设备.该设备在一个较近的环境中操作,精度高,跟 ...

  2. Hello China操作系统STM32移植指南(一)

    Hello China操作系统移植指南 首先说明一下,为了适应更多的文化背景,对Hello China操作系统的名字做了修改,修改为"Hello X",或者连接在一起,写为&quo ...

  3. jdbc操作步骤和preparedStatment相比Statment的好处

    java操纵数据库封装了一组API,通过这组API可以透明的操作各种数据库,一般来讲,操纵数据库的步骤是: 一. try{ 1.加载数据库驱动 Class.forName("数据库驱动类&q ...

  4. [学习笔记]jQuery实现一些动画效果的方法

    jQuery实现效果的方法 1,隐藏和显示:hide(),show(),toggle()  // [ˈtɑ:gl]切换 语法: $(selector).hide(speed,callback); $( ...

  5. <input type="text">文本输人框

    type类型: text  文本框 password  口令密码输人框 reset  重置或清除 buttou  命令按钮 checkbox  复选框 radio  单选框 submit  提交 fi ...

  6. 学习MVC遇到的问题

    修改电脑上的DNS配置: Opendns 首选DNS服务器和备用DNS服务器分别设置为208.67.222.222和208.67.220.220 google的8.8.8.8 首选DNS服务器和备用D ...

  7. Mac Yosemite下Android Studio环境问题集合

    1. java not found 在mac Yosemite下,因jre升级到1.8,导致Android Studio无法启动.报错:"JAVA not found". 解决方法 ...

  8. r语言之生成规则序列,规则序列函数及用法

    在生成序列时,“:”的优先级最高 (1)从1到20的整数序列: > 1:20 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 (2) ...

  9. QPushButton跑进度条(使用QSS的不同修饰来实现,其实是伪进度条)

    主要用到qlineargradient,写以下CSS样式即可实现: background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, sto ...

  10. GDKOI2016

    天若有情天亦老 月若无恨月常圆 Day1 score cardcaptor AAAAAAAATT protal WWWWWWWWWW treasurehunt AAAAWXXXXX map AAATT ...