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)
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 {//查找Servicesf=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并查看结果:
一个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("注冊服务開始....");//注冊AHashtable<String, String> dict=new Hashtable<String, String>();dict.put("name", "a");context.registerService(IStudentManage.class.getName(),new StudentManageA(), dict);//注冊Bdict=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("服务调用------------------");//查找ServiceString 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及查看结果:
转载于:https://www.cnblogs.com/wzjhoutai/p/6734965.html
osgi实战学习之路:6. Service-1相关推荐
- osgi实战学习之路:8. Service-3之ServiceTracker
通过ServiceTracker能够对查找的Service进行扩展 以下的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activa ...
- osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld
开发环境分为三个部份 osgi_provider: bundle开发环境,对外提供服务 osgi_consumer: 引用其它bundle osgi_main: 运行测试 项目主要内容 : commo ...
- 大数据学习之路(转载)
#大数据学习之路(转载) 博文地址https://blog.csdn.net/zys_1997/article/details/78358992 看到一个博主写的大数据学习路线,看了比较心动,想着自己 ...
- 送你九年经验,我的Java学习之路你也可以复制
这篇文章写的非常认真,足足花了两周时间,不是简单的资料聚合,我将多年的工作和学习经验写下来了,相信看完后你能有一种豁然开朗的感觉,这就是我要达到的目的,希望不要被打脸. 最近身边很多人在问:Java ...
- SpringCloud学习之路
SpringCloud学习之路 1.使用IDEA搭建Eureka服务中心Server端启动 1.1.创建和配置注册中心Eureka 1.2.使用Eureka案例 1.3.负载均衡器Ribbon 1.4 ...
- Rasa 3.X 智能对话机器人案例开发硬核实战高手之路 (7大项目Expert版本)
课程标题:Rasa 3.X 智能对话机器人案例开发硬核实战高手之路(7大项目Expert版本) 课程关键字:Rasa Application.Debugging.E-commerce.Retail.C ...
- 拿下斯坦福和剑桥双offer,00后的算法学习之路
董文馨,00后,精通英语,西班牙语.斯坦福大学计算机系和剑桥大学双Offer,秋季将进入斯坦福大学学习. 10岁开始在国外上学:12岁学Scratch: 13岁学HTML & CSS: 14岁 ...
- JavaWeb学习之路——SSM框架之Spring(五)
前情提要请看JavaWeb学习之路--SSM框架之Spring(四) 整合Spring和Mybatis框架 1.在项目的 ...
- JavaWeb学习之路——SSM框架之Mybatis(三)
数据库配置和相关类创建看上篇:JavaWeb学习之路--SSM框架之Mybatis(二) https://blog.csdn.net/kuishao1314aa/article/details/832 ...
最新文章
- 日志级别 debug info warn eirror fatal
- 服务器内存延迟,内存带宽、延迟性能测试
- MYSQL存储过程中 表名 使用变量
- Android的三种网络联接方式(URL / HttpURLConnection | HttpClient | InetAddress )
- jQuery用正则查找元素:jQuery选择器使用
- 【转载】js数组的操作
- FastDFS学习总结(1)--FastDFS安装和部署
- 王小毛是懒惰了,还是堕落了?
- Oracle RAC -常见CRS命令
- Note8 android 9 root,三星Note8 国行 root N9500 9.0 root N9500ZCS6DTC1 root
- exePath must be specified when not running inside a stand alone exe
- [python][project][爬虫] 时光网抓取信息
- Android曲线水波纹动画,Android水波纹显示进度效果,很炫
- python爬虫—爬取taptap游戏的评论信息(通过fiddler抓包)
- 2015款Mac笔记本安装Windows10系统到外置移动硬盘教程
- 解决你关于域名过期且没有备案的问题
- uva 1645 count
- 欧文计算机科学排名,加州大学欧文分校计算机科学世界排名2019年最新排名第55(THE世界排名)...
- 微信公众平台开发[3] —— 微信公众号支付功能(PHP)
- 【关联分析实战篇】为什么 BI 软件都搞不定关联分析