osgi实战学习之路:8. Service-3之ServiceTracker
通过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的更多相关文章
- osgi实战学习之路:6. Service-1
什么是Service? 它是注冊到osgi的一个java对象 Service注冊: 通过BundleContext::registerService(java.lang.String[] clazze ...
- osgi实战学习之路:3. osgi分层概念及相互合作demo
源码下载 分层: modual: 主要作用于包级管理与共享代码 lifecycle: 主要作用于执行期间的模块管理与訪问osgi底层框架 service: 主要作用于多模块之间的相互通信 demo: ...
- osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo
生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在执行期间,为应用程序提供操作osgi框架的方法 Bundle 代 ...
- 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 ...
- osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld
开发环境分为三个部份 osgi_provider: bundle开发环境,对外提供服务 osgi_consumer: 引用其他bundle osgi_main: 执行測试 项目主要内容 : commo ...
- osgi实战学习之路:4.Bundle
</pre></h1><h1 style="margin:0 0 0 40px; border:none; padding:0px"><p ...
- Salesforce学习之路(十三)Aura案例实战分析
Aura相关知识整合: Salesforce学习之路(十)Aura组件工作原理 Salesforce学习之路(十一)Aura组件属性<aura:attribute /> Salesforc ...
- GitHub标星8k,字节跳动高工熬夜半月整理的“组件化实战学习手册”,全是精髓!
前言 什么是组件化? 最初的目的是代码重用,功能相对单一或者独立.在整个系统的代码层次上位于最底层,被其他代码所依赖,所以说组件化是纵向分层. 为什么要使用组件化? 当我们的项目越做越大的时候,有时间 ...
- RPC远程过程调用学习之路(一):用最原始代码还原PRC框架
RPC: Remote Procedure Call 远程过程调用,即业务的具体实现不是在自己系统中,需要从其他系统中进行调用实现,所以在系统间进行数据交互时经常使用. rpc的实现方式有很多,可以通 ...
随机推荐
- Libev学习笔记2
这一节根据官方文档给出的简单示例,深入代码内部,了解其实现机制.示例代码如下: int main (void) { struct ev_loop *loop = EV_DEFAULT; ev_io_i ...
- hdoj 1269 迷宫城堡(强连通分量)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269 思路分析:该问题要求判断是否每两个房间都可以相互到达,即求该有向图中的所有点是否只构成一个强连通 ...
- GCD自己做的一些简单总结
GCD总结 GCD Grand Central Dispatch 牛逼的中枢调度器 GCD中各种队列的执行效果 想看线程 必须是异步函数 并且不是主队列 注意:使用sync函数往当前串行队列添 ...
- 分析php获取客户端ip
用php能获取客户端ip,这个大家都知道,代码如下: /** * 获取客户端ip * @param number $type * @return string */ function getClien ...
- poj 2411 Mondriaan's Dream 轮廓线dp
题目链接: http://poj.org/problem?id=2411 题目意思: 给一个n*m的矩形区域,将1*2和2*1的小矩形填满方格,问一共有多少种填法. 解题思路: 用轮廓线可以过. 对每 ...
- css优先级及权重值
优先级: 外部样式表 内部样式表(位于<head>标签内部 内联样式(在HTML元素内部)优先权最高 内联样式>内部样式=外部样式(看具体引入位置,解析的先后) 权重值: 第一等:内 ...
- 自动分组+合并完整的sql脚本
BEGIN#前提:指定字符串长度为8字符定长#逻辑:循环8次,比对2个字符串相同索引位置下的数值大小,并取结果最大值.#示例:merge1(输入参数source1,输入参数source2,输出结果re ...
- Angular JS 学习笔记(一)
1. 菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 2. Angular JS中文网:http://www.apjs.net ...
- 读取jar包里面的文件
一.最近做项目的时候,师兄要求读取jar包里面的java文件.在网上查了各种文件以后,终于完成了,在这里和各位朋友分享一下. (一)找到jar包所在的位置. String path="XXX ...
- SAE部署Java应用
链接地址:http://blog.csdn.net/shuixin536/article/details/9031335 SAE为开发者提供了非常宽松的开发环境,你甚至不用做任何特别定制就能将各种Ja ...