通过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. FIB数列

    斐波那契级数除以N会出现循环,此周期称为皮萨诺周期. 下面给出证明 必然会出现循环 这是基于下面事实: 1. R(n+2)=F(n+2) mod P=(F(n+1)+F(n)) mod P=(F(n+ ...

  2. linux 多线程编程笔记

    一, 线程基础知识 1,线程的概念 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行 中必不可少的资源(如程序计 ...

  3. SVN同步出现故障

    1.错误描写叙述    同步SVNStatusSubscribe时报告了错误,1中的0个资源已经同步    同步/frame时错误发生:Error getting status for resourc ...

  4. NSDate conversion utilities

    // Gets UTC NSDate from DateTime(.Net/WCF). + (NSDate *)fromDateTime:(NSString *)dateTime { NSDate * ...

  5. input autocomplete 下拉提示+支持中文

    js 代码: $.getJSON("/Foreign/Getforeign_routeEndPoint", function (data) {            $(" ...

  6. 调用 sphinx-build生成HTML文件

    安装 Sphinx $ easy_install sphinx Searching for sphinx Reading http://pypi.python.org/simple/sphinx/ R ...

  7. beta分布

    http://blog.csdn.net/sweetrryy/article/details/6436358

  8. 如何正确地在手机上显示图片——QImage与QPixmap完全解析

    引言 用Qt程序在手机上显示一幅图片对编程人员来说是再基础不过的一件事情了.那么先让大家看两段代码: //dangerous should not be used, cannot display ea ...

  9. Android创建启动画面

    每一个Android应用启动之后都会出现一个Splash启动界面,显示产品的LOGO.公司的LOGO或者开发人员信息.假设应用程序启动时间比較长,那么启动界面就是一个非常好的东西,能够让用户耐心等待这 ...

  10. Android事件机制全然解析

    android事件是一级一级传递的,假设父控件不拦截.就传给子控件,假设父控件想要消费事件也就是拦截事件的话,须要重写这种方法 public boolean onInterceptTouchEvent ...