osgi实战学习之路:6. Service-1
什么是Service?
它是注冊到osgi的一个java对象
Service注冊:
通过BundleContext::registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)
Service查找及使用:
通过BundleContext::getServiceReference(java.lang.String clazz),返回ServiceReference
通过BundleContext::getService(ServiceReference reference) ,返回注冊的服务对象
Service释放:
通过BundleContext::ungetService(ServiceReference reference)
LADP:
轻量级文件夹訪问协议(Lightweight Directory Access Protocol)
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
1个Service相应一个实现类的注冊与使用demo:
服务提供者:
student-manage/IStudentManage.java
package com.demo.service; public interface IStudentManage {
void add();
}
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("注冊服务開始....");
context.registerService(IStudentManage.class.getName(),
new StudentManage(), null);
System.out.println("注冊服务结束....");
} public void stop(BundleContext context) throws Exception { } }服务使用者:
student-action/Activator.java
package com.demo.action; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference; import com.demo.service.IStudentManage; public class Activator implements BundleActivator{
public void start(BundleContext context) throws Exception {
System.out.println("action begin...");
ServiceReference sf=null;
try {
//查找Service
sf=context.getServiceReference(IStudentManage.class.getName());
//调用服务
IStudentManage studentManage = (IStudentManage)context.getService(sf);
studentManage.add();
} finally {
context.ungetService(sf);
}
System.out.println("action end...");
} public void stop(BundleContext context) throws Exception {
} }部署至karaf并查看结果:
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
一个Service相应多个实现(基于LDAP)demo2
服务提供者
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.StudentManageA;
import com.demo.service.impl.StudentManageB; public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception {
System.out.println("注冊服务開始....");
//注冊A
Hashtable<String, String> dict=new Hashtable<String, String>();
dict.put("name", "a");
context.registerService(IStudentManage.class.getName(),
new StudentManageA(), dict);
//注冊B
dict=new Hashtable<String, String>();
dict.put("name", "b");
context.registerService(IStudentManage.class.getName(),
new StudentManageB(), dict);
System.out.println("注冊服务结束....");
} public void stop(BundleContext context) throws Exception { } }服务使用者
student-action/Activator.java
package com.demo.action; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceReference; import com.demo.service.IStudentManage; public class Activator implements BundleActivator{
public void start(BundleContext context) throws Exception {
System.out.println("action begin...");
ServiceReference[] references=null;
try {
System.out.println("服务调用------------------");
//查找Service
String filter="(name=b)";
references=context.getServiceReferences(IStudentManage.class.getName(), filter);
//调用服务
if(references.length==1){
IStudentManage studentManage = (IStudentManage)context.getService(references[0]);
studentManage.add();
}else {
throw new IllegalArgumentException("IStudentManage查找到多个实现类");
} } finally {
for(ServiceReference sf:references){
context.ungetService(sf);
} }
System.out.println("action end...");
} public void stop(BundleContext context) throws Exception {
} }部署到karaf及查看结果:
osgi实战学习之路:6. Service-1的更多相关文章
- osgi实战学习之路:8. Service-3之ServiceTracker
通过ServiceTracker能够对查找的Service进行扩展 以下的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activa ...
- 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的实现方式有很多,可以通 ...
随机推荐
- MS SQL 中判断 数据库, 存储过程,表,临时表,视图,函数,用户,用户创建对象 等是否存在 SQL脚本
摘自: http://www.111cn.net/database/mssqlserver/39107.htm sql判断存储过程是否存在 判断数据库教程是否存在 Sql代码 if exists (s ...
- DRP——JDBC中的Batch
在jdbc2.0里添加了批量处理的功能(batch),其同意将多个sql语句作为一个单元送至数据库去运行,这样做能够提高操作效率.在操作大量的数据时, ORM框架实现批量是非常慢的.我们能够使用jdb ...
- Voice Commands (VCD) Cortana 微软小娜示例
Cortana 样品 您可以创建自定义功能Cortana使用Cortana技能装备或遗留的声音命令(VCD)平台. 在这里,你可以找到相关的样品: Cortana技能装备 目前Cortana技巧是建立 ...
- [React] Preventing extra re-rendering with function component by using React.memo and useCallback
Got the idea form this lesson. Not sure whether it is the ncessary, no othere better way to handle i ...
- MFC获得主窗体和父窗体指针
MFC编程中经常遇到子窗体向父窗体传递參数的情况,这就须要获得父窗体的指针. 例:主对话框CMyMainDlg通过buttonButtonA进入对话框CMyParentDlg.CMyParentDlg ...
- MP3文件头格式
MP3文件结构及编解码流程 http://blog.sina.com.cn/s/blog_67b7cb7b01018i2l.html http://blog.csdn.net/liuyan4794/a ...
- iOS tabbar 图片,最佳大小方式
iOS tabbar 图片,最佳大小方式 文档大小 30 *30 retaina 60 *60 最佳大小 48 *32 参考:http://stackoverflow.com/questions/15 ...
- vue - 添加sass(less)处理
1. 添加less.sass处理 1.1如果是sass,首先在当前目录安装处理插件(sass): npm i -D node-sass sass-loader 1.2如果是less,首先在当前目录安装 ...
- FLV视频播放:对未缓冲进度条实现拖动
FLV视频播放:对未缓冲进度条实现拖动 流媒体开发 Add comments 八282010 一.文件准备 1.转码:ffmpeg 2.添加元数据:yamdi 二.网页播放器:jw player 使 ...
- IOS客户端Coding项目记录(二)
9:第三方插件整理 JSON转实体:jsonModel https://github.com/icanzilb/JSONModel/ 美化按键:BButton https://github.com/m ...