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的实现方式有很多,可以通 ...
随机推荐
- JAVA GUI学习 - 窗口【x】按钮关闭事件触发器:重写processWindowEvent(WindowEvent e)方法
public class WindowListenerKnow extends JFrame { public WindowListenerKnow() { this.setBounds(300, 1 ...
- Libev学习笔记3
设置完需要监听的事件之后,就开始event loop了.在Libev中,该工作由ev_run函数完成.它的大致流程如下: int ev_run (EV_P_ int flags) { do { /* ...
- 七、cocos2dx之粒子系统
本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=66 一.介绍 粒子系统指的是一个使用大量很小的精灵或者其他的一些绘制对象模拟一些模糊效果.使用传统的 ...
- 20140603 对error.c 用于分析源代码
20140603 对error.c 用于分析源代码 继续看error.c该功能 买家现在将自己的代码和数据汇编例如,下面的: 1.#include <stdio.h> 2 #inc ...
- runtime的概念,message send如果寻找不到相应的对象,如何进行后续处理
运行时刻是指一个程序在运行(或者在被执行)的状态.也就是说,当你打开一个程序使它在电脑上运行的时候,那个程序就是处于运行时刻.在一些编程语言中,把某些可以重用的程序或者实例打包或者重建成为“运行库”. ...
- 在mac本上折腾android 开发环境
众所周知的原因,google的很多网站在国内无法访问,苦逼了一堆天朝程序员,下是在mac本上折腾android 开发环境的过程: 一.先下载android sdk for mac 给二个靠谱的网址: ...
- mongoDB初接触
首先去官网http://www.mongodb.org/ 下载mongoDB 下载后解压D:\mongodb 创建两个新文件夹data以及log 新建一个文件命名为mongodb.cfg dbpath ...
- C++堆和栈的比较(7个区别)
基础知识: 堆 栈是一种简单的数据结构,是一种只允许在其一端进行插入或删除的线性表.允许插入或删除操作的一端称为栈顶,另一端称为栈底,对堆栈的插入和删除操作被称 为入栈和出栈.有一组CPU指令可以实现 ...
- 异步和同步http请求超时机制
异步超时设置: 例子: Example: do a simple HTTP GET request for http://www.nethype.de/ and print the response ...
- Sicily-1024
一. 题意: 有n个节点,n-1条边,并且任意两个节点都连通.模拟一下,实际上是一棵树的便利,求从特定根节点出发最长路径的值.这里用了广搜. 二. 每个节点只有两条邻接边,每个节点用一个vector来 ...