什么是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的更多相关文章

  1. osgi实战学习之路:8. Service-3之ServiceTracker

    通过ServiceTracker能够对查找的Service进行扩展 以下的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activa ...

  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. ElementUI表单验证使用

    1.设计校验方式: 我们表单验证的rules一般封装一个单独的js文件,比如我之前写的这个博客: ElementUI使用问题记录:设置路由+iconfont图标+自定义表单验证 可以修改下:公共的校验 ...

  2. Sql server2005 优化查询速度50个方法小结

    Sql server2005 优化查询速度50个方法小结   Sql server2005优化查询速度51法查询速度慢的原因很多,常见如下几种,大家可以参考下.   I/O吞吐量小,形成了瓶颈效应.  ...

  3. [Functional Programming] Async IO Functor

    We will see a peculiar example of a pure function. This function contained a side-effect, but we dub ...

  4. ejs和swig对比的问题之一

    本来想测试下两者在nodejs中得执行速度,设置了一个测试数据,如下 var testData = { title:'测试标题', description: '<p>这是一个描述,里面用很 ...

  5. 基于Struts2、Spring、Hibernate实现的包括多条件查询分页的基础Dao层帮助jar包实现

    操作数据库经常使用操作就是增删查改.每做一次就写一次这些操作太麻烦,也不是必需,特别是写多条件查询并分页时.太痛苦了,所以抽出时间写了个dao帮助jar.导入即搞定!妈妈再有不用操心我的项目了! 转载 ...

  6. 百度地图SDK for Android【检索服务】

    1搜索服务 百度地图SDK集成搜索服务包括:位置检索.周边检索.范围检索.公交检索.驾乘检索.步行检索,通过初始化MKSearch类,注册搜索结果的监听对象MKSearchListener,实现异步搜 ...

  7. 手机网站调试神器之chrome控制台

    现在真是一个信息化的时代,每个人手里都拿着一款智能机,上班下班走路坐车之余都会玩玩手机,上上网.于是作为广大网站媒体来说,争抢手机市场无疑是刻不容缓.对于我们Web前端工程师来说,了解并掌握手机编程的 ...

  8. 《学习opencv》笔记——矩阵和图像操作——cvCrossProduct and cvCvtColor

    矩阵和图像的操作 (1)cvCrossProduct函数 其结构 void cvCrossProdust(//计算两个三维向量的叉积 const CvArr* src1, const CvArr* s ...

  9. TypeScript 面向对象基础知识

    孙广东  2016.4.5 JavaScript如今到处都是.web.server(通过NodeJS).移动应用(通过各种框架).全部这些,TypeScript都能够使用,而且能够为JavaScrip ...

  10. Qt Multimedia Backends(多媒体后端)翻译

    目录 MediaService plugins 媒体服务插件 不同后端支持的媒体播放器功能: 后端支持的摄像头(相机)功能 后端支持的音频解码功能 Audio plugins 音频插件 原文地址: Q ...