什么是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. 在低带宽或不可靠的网络环境中安装 Visual Studio 2017

    在低带宽或不可靠的网络环境中安装 Visual Studio 2017 2017-4-141 分钟阅读时长 作者  https://docs.microsoft.com/zh-cn/visualstu ...

  2. itext测试地址

    http://demo.itextsupport.com/xmlworker/?snippetChooser=headers

  3. 算法笔记_133:最大连续乘积子数组(Java)

    目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 动态规划法   1 问题描述 给定一个浮点数组,任意取出数组中的若干个连续的数相乘,请找出其中乘积最大的子数组. 2 解决方案 2.1 蛮力法 ...

  4. js 实现图片的无缝滚动

      js 实现图片的无缝滚动 CreateTime--2018年3月7日17:18:34 Author:Marydon 测试成功 <!DOCTYPE html> <html> ...

  5. JavaWeb 绝对路径与相对路径

      JavaWeb 绝对路径与相对路径 CreateTime--2018年4月14日16:51:19 Author:Marydon 8.1 绝对路径 web项目不可能使用绝对路径,因为: 以文件的引用 ...

  6. 〖Linux〗(2013.08.02)VIM74b+YouCompleteMe,VIM代码编辑器补全能手

    1. 编译和安装vim74b(参考:http://t.cn/zQa8R7h ) sudo apt-get install -y hgsvn libncurses5-dev libgnome2-dev ...

  7. 基于spring-boot的社区社交微信小程序,适合做脚手架、二次开发

    基于spring-boot的社区社交微信小程序,适合做脚手架.二次开发 代码地址如下:http://www.demodashi.com/demo/13867.html 1 概述 笔者做的一个后端基于s ...

  8. dom与jquery互相转换

    /*取得<input>标签中的value属性的内容[dom对象->jquery对象] var inputElement = document.getElementById(" ...

  9. java基础学习总结——GUI编程(二) 未学习

    一.事件监听

  10. Linux命令-压缩解压命令:zip、unzip

    zip [选项] [压缩后文件名] [压缩前的文件或者目录名称] -r表示压缩目录(recursion 递归) rm -rf * 删除当前目录下面的所有文件,也包括目录和子目录ls cp /etc/s ...