什么是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. 文本相似性计算总结(余弦定理,simhash)及代码

    最近在工作中要处理好多文本文档,要求找出和每个文档的相识的文档.通过查找资料总结如下几个计算方法: 1.余弦相似性 我举一个例子来说明,什么是"余弦相似性". 为了简单起见,我们先 ...

  2. iis服务器站点配置

    iis网站正常移动到一个新的服务器上 需要配置如下: 启用父路径 不启用父路径会报服务器500错误

  3. JSP2.0自定义标签

    JSP1.0中可以通过继承TagSupport或者BodyTagSupport来实现自定义的tag处理方法. JSP2.0中也支持另外一种更为简单的自定tag的方法,那就是直接讲JSP代码保存成*.t ...

  4. Simple TCP/IP Echo Server & Client Application in C#

    1. TCP Server The server’s job is to set up an endpoint for clients to connect to and passively wait ...

  5. 定义JQuery插件

    http://cavalry800528.iteye.com/blog/1953917 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$( ...

  6. BerkeleyDB java的简单使用

    关于BerkeleyDB的有点和优点,列在以下 JE offers the following major features: Large database support. JE databases ...

  7. javascript 自定义动画函数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  8. java获取unicode码

    public String toUNICODE(String s)     {         StringBuilder sb=new StringBuilder();         for(in ...

  9. web站点,同一个浏览器只能登陆一个用户的原因(cookie不能跨浏览器)

    我的web站点,比如  http://ip/testsite/default.aspx, 当我在我的机器上,用chrome打开,用账号user1登陆,那么当我再新开个tab,再打开这个web站点,这时 ...

  10. 转:configure/make/make install的作用 linux 安装 卸载 make uninstall

    这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure 是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GC ...