方法一:实现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. [转]Android学习:EditText的使用方法

        EditText是在Android开发中经常被使用到的控件,主要用来获取用户的输入内容.   1.EditText常用属性   EditText继承自TextView,所以EditText也拥 ...

  2. MAMP PRO 下安装 memcache

    本人PHP用的是 5.5.10,编译 memcache 模块需要用到 php 源码,mamp 不自带,到 php 官网下一个 php-5.5.11.tar.gz, 解压后,生成 zend_config ...

  3. HDFS API基本操作

    对HDFS API基本操作都是通过 org.apache.hadoop.fs.FileSystem类进行的,以下是一些常见的操作: package HdfsAPI; import java.io.Bu ...

  4. Spring使用@Required注解依赖检查

    Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种情况,你需要 @Required ...

  5. MySql清空表的方法介绍 : truncate table 表名

    清空某个mysql表中所有内容 delete from 表名; truncate table 表名; 不带where参数的delete语句可以删除mysql表中所有内容,使用truncate tabl ...

  6. Unable to load DLL 'opencv_core290'

    问题: In my winforms application I need to use some Emgu.CV libraries (I have installed Emgu 2.9). Pro ...

  7. java开发常用的Linux命令

    原文:https://www.cnblogs.com/not-alone/p/8505925.html 1.查找文件 find / -name filename.txt 根据名称查找/目录下的file ...

  8. 微信开发之如何使用开发工具--weixin-java-tools

    一.前沿 微信公众平台由于没有提供针对语言的开发包,只公布了一个基于Http协议的接口和加解密的算法sdk,这样给微信公众号的开发者带来很多工作量,除了实现业务逻辑外,还需要自己处理底层的接口协议细节 ...

  9. 跨域策略文件crossdomain.xml

    Web站点通过crossdomain.xml文件(放于站点根目录)配置提供允许的域跨域访问本域内容的权限 以土豆的为例: <cross-domain-policy> <allow-a ...

  10. js复制兼容:ZeroClipboard复制到剪切板(支持IE、FF、Chrome)

    注意:ZeroClipboard在本地测试无法直接使用,必须在服务器上测试,如http://localhost... 准备:ZeroClipboard.swf 和 ZeroClipboard.js 小 ...