方法一:实现ServiceListener接口:

package org.riawork.demo.web;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException; public class Activator implements BundleActivator, ServiceListener { private BundleContext ctx;
private ServiceReference ref; public void start(BundleContext context) throws Exception {
System.out.println("Hello World-Web!!");
this.ctx = context;
context.addServiceListener(this, "(&(objectClass=" + HttpService.class.getName() + "))");
} public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World-Web!!");
} @Override
public void serviceChanged(ServiceEvent event) {
ref = ctx.getServiceReference(HttpService.class.getName());
HttpService http = (HttpService) ctx.getService(ref); switch (event.getType()) {
case ServiceEvent.REGISTERED:
try {
http.registerResources("/demo/page", "page", null);
System.out.println("请通过/demo/page/login.htm访问");
} catch (NamespaceException e) {
e.printStackTrace();
}
break; case ServiceEvent.UNREGISTERING:
http.unregister("/demo/page");
System.out.println("已卸载web模块!");
break;
}
}
}

方式二:继承ServiceTracker基类:

package org.riawork.demo.web;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer; public class Activator implements BundleActivator, ServiceListener { private BundleContext ctx;
private ServiceReference ref;
private LocalTracker tracker; public void start(BundleContext context) throws Exception {
System.out.println("Hello World-Web!!");
this.ctx = context;
this.tracker = new LocalTracker(context);
this.tracker.open();
context.addServiceListener(this, "(&(objectClass=" + HttpService.class.getName() + "))");
} public void stop(BundleContext context) throws Exception {
this.tracker.close();
this.tracker = null;
System.out.println("Goodbye World-Web!!");
} private class LocalTracker extends ServiceTracker {
public LocalTracker(BundleContext context) {
super(context, HttpService.class.getName(), null);
} public Object addingService(ServiceReference ref) {
HttpService http = (HttpService) context.getService(ref);
try {
http.registerResources("/demo/page", "page", null);
System.out.println("请通过/demo/page/login.htm访问");
} catch (NamespaceException e) {
e.printStackTrace();
}
return http;
} public void removedService(ServiceReference ref, Object service) {
HttpService http = (HttpService) service;
http.unregister("/demo/page");
super.removedService(ref, service);
System.out.println("已卸载web模块!");
}
}
}

监听OSGi服务的更多相关文章

  1. LINUX启动ORACLE监听和服务

    可通过secureCRT或者telnet直接连 启动监听命令:lsnrctl start 成功启动后:sqlplus /nolog 回车 conn / as sysdba 回车 startup 回车 ...

  2. windows 启动关闭Oracle监听和服务

    经常要用数据库,让他自己启动的话,开机太慢,所以用命令启动方便点.    1.开启:     在运行中输入cmd,进入控制台,lsnrctl start回车,提示启动监听成功后 net start O ...

  3. PLSQL连接Oracle64监听和服务的配置!

    前言: 这里不会涉及到太多关于版本问题的解决,只是简单提一下基本的监听和服务配置问题的解决,让你可以快速的用PLSQL连接上你自己创建的Oracle数据库(这里示例数据库名为ORCL); 版本问题: ...

  4. oracle_windows下命令启动oracle监听和服务

    1.检查监听器状态 C:\Users\Administrator>lsnrctl status 2.启动监听程序 C:\Users\Administrator>lsnrctl start ...

  5. asp.net core 多端口监听&日志服务

    1 配置多个端口监听 HostingAbstractionsWebHostBuilderExtensions. public static IWebHostBuilder UseUrls(this I ...

  6. Oracle 删除监听程服务

    1.开始->运行->输入regidit ->->->->->红框内的右键删除 2.开始->运行->输入regidit ->->-> ...

  7. laravel进阶系列--通过事件和事件监听实现服务解耦

    简介 Laravel 事件提供了简单的观察着模式实现,允许你订阅和监听应用中的事件.事件类通常存放在 app/Events 目录. 监听器存放在 app/Listeners. 如果你在应用中没有看到这 ...

  8. OSGi 系列(七)之服务的监听、跟踪、声明等

    OSGi 系列(七)之服务的监听.跟踪.声明等 1. OSGi 服务的事件监听 和 bundle 的事件监听类似,服务的事件监听是在服务注册.注销,属性被修改的时候,OSGi 框架会发出各种不同的事件 ...

  9. (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...

随机推荐

  1. DP练习 巡逻

     国庆这天五一大道上人头攒动,这是因为大家都准备从五一广场上那个大屏幕观看新中国60周年的国庆阅兵式!这虽然是一件很喜庆的事情,可却让CS市的警察局长伤透了脑筋的,因为人潮拥挤是很容易发生安全事故的. ...

  2. 51nod 1035 最长的循环节 数学

    1035 最长的循环节 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1035 Description 正整 ...

  3. iOS开发系列--视频处理

    MPMoviePlayerController 在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频 ...

  4. mysql 存储过程案列一个。

    -- 设置分隔符 DELIMITER // /*初始化*/ DROP PROCEDURE IF EXISTS useCursor // /*建立 存储过程 create */ CREATE PROCE ...

  5. BusyBox 简化嵌入式 Linux 系统

    BusyBox 是很多标准 Linux® 工具的一个单个可执行实现.BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大.更复杂的工具,例如 grep.find.moun ...

  6. Linux 静态库与动态库搜索路径设置详解

    转载:http://blog.chinaunix.net/uid-29025972-id-3855495.html 1. 连接和运行时库文件搜索路径的设置 库文件在连接(静态库和共享库)和运行(仅限于 ...

  7. 配置Android-Annotation (github20大开源:http://www.eoeandroid.com/thread-278980-1-1.html)

    1. 把androidannotations-X.X.X-api.jar 放在libs文件夹 2. 把androidannotations-X.X.X.jar 放在文件夹compile-libs,1与 ...

  8. OpenCV Harris 角点检测子

    Harris 角点检测子 目标 本教程中我们将涉及: 有哪些特征?它们有什么用? 使用函数 cornerHarris 通过 Harris-Stephens方法检测角点. 理论 有哪些特征? 在计算机视 ...

  9. 循环链表的创建、插入、删除、逆序、显示(C++实现)

    对于单链表,因为每一个结点仅仅存储了向后的指针.到了尾标志就停止了向后链的操作,这样,其中某一结点就无法找到它的前驱结点了. 对于单链表的操作大家能够看我的这篇博客http://blog.csdn.n ...

  10. iOS: 向Github的README.md里添加图片

    我们将项目上传到Github上开源供大家使用,可是,有时只是在READEME.md中做一些文字说明并不直观,如果能给上演示的截图是不是更能把功能展示的一目了然呢. 不费话了,直接上步骤: 第一步:首先 ...