什么是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. C#好书盘点

    C#是学习asp.net和.NET Winform程序的基础,所以一定要学好.从00年C#出道到现在,7年过去了,C#的书出了许多,有不少好书. 1.<21天学通C# > 作者:作者:(美 ...

  2. FrameWork数据权限浅析3之基于角色的配置表实现行级数据安全

    带着上一次笔记的疑问和些许欢喜来到了混混沌沌的下午,程序员的脑子一直在不停的思索着,而多思考总是没错的,盼望着盼望着事情就有了转机,现在我们就来说一说基于角色级别的中间表机制实现行级数据安全. 由于本 ...

  3. token 机制

  4. 如何用 js 递归输出树型

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)

    翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...

  6. 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。一些服务自动停止,如果他们没有什么可做的 .

    一.问题 本地计算机上的OracleOraDb10g_home1TNSListener 服务启动又停止了 二.问题描述 Oracle:本地计算机上的OracleOraDb10g_home1TNSLis ...

  7. u32、u16、u8 数据类型

    #define U32 unsigned int #define U16 unsigned short #define S32 int #define S16 short int #define U8 ...

  8. Python 创建包含列表的元组

    “可变”的tuple前面我们看到了tuple一旦创建就不能修改.现在,我们来看一个“可变”的tuple:>>> t = ('a', 'b', ['A', 'B'])注意到 t 有 3 ...

  9. 2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数

    NPOI教程:http://www.cnblogs.com/atao/archive/2009/11/15/1603528.html 之所有说NPOI强大,是因为常用的Excel操作她都可以通过编程的 ...

  10. ie6/7 bug大全

     1. ie6/7下ul高度变高  li 加 vertical-align:bottom; 2.ie6不支持min-height a:height:auto !important; height:10 ...