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的实现方式有很多,可以通 ...
随机推荐
- perl lwp关闭ssl校验
use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Encode; use File:: ...
- Linux Centos 系统上安装BT客户端 Transmission
Linux Centos 系统上安装BT客户端 Transmission Transmission是一种BitTorrent客户端,特点是一个跨平台的后端和其上的简洁的用户界面,以MIT许可证和G ...
- 关于QT中evaluateJavaScript()函数返回值的处理问题
关于QT中evaluateJavaScript()函数返回值的处理问题 - 寒风问雪的专栏 - 博客频道 - CSDN.NET 关于QT中evaluateJavaScript()函数返回值的处理问题 ...
- [置顶] 浅谈大型web系统架构
转载原文:http://blog.csdn.net/dinglang_2009/article/details/6863697 分类: 大规模Web 2.0架构 2011-10-11 18:27 12 ...
- BCS--使用SharePoint Designer创建外部内容类型
使用SharePoint Designer创建外部列表(也可以在浏览器中创建列表) http://www.cnblogs.com/haogj/archive/2011/05/01/2033845.ht ...
- MVC表单提交加JS验证
做一个普通表单提交,但是要加前端验证,如下: 1. Action public ActionResult Add(ProductModelproductID) { //operate... } ...
- STL之map和multimap(关联容器)
map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.自动建立Key - value的对应,对于迭代器来说,可以修改实值,而不能修改key. ...
- jacksonall的使用,解析json
转自:http://www.cnblogs.com/lee0oo0/archive/2012/08/23/2652751.html , Jackson可以轻松的将Java对象转换成json对象和xml ...
- [LeetCode]题解(python):029-Divide Two Integers
题目来源: https://leetcode.com/problems/divide-two-integers/ 题意分析: 不用乘法,除法和mod运算来实现一个除法.如果数值超过了int类型那么返回 ...
- Nginx阅读笔记(三)之proxy_pass用法
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走,如果没有/,则会把匹配的路径部分也给代理走. 假设访问 ...