方法一:实现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. BeanFactoryPostProcessor和BeanPostProcessor

    1. BeanFactoryPostProcessor调用(见AbstractApplicationContext.refresh): >> 创建DefaultListableBeanFa ...

  2. April Fools Day Contest 2016 E. Out of Controls

    E. Out of Controls 题目连接: http://www.codeforces.com/contest/656/problem/E Description You are given a ...

  3. UESTCACM 每周算法讲堂 延迟标记+bfs dfs搜索入门

    http://www.bilibili.com/video/av4163472/ 地址在上面~

  4. 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

    秋实大哥の恋爱物语 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...

  5. 《python学习手册》第35章 异常的设计

    嵌套异常处理器 其实我们主要需要搞清楚的问题应该是这样的,当异常发生的时候,无论是简单的异常处理还是复杂的异常处理,我们都应该能够清楚的了解到异常运行到哪里,被谁捕获了,现在控制权到了哪里了,下面我们 ...

  6. mybatis源码分析(6)-----核心调度对象StatmentHandler

    写在前面 通过上一偏文章,我们知道:mybatis 的插件开发,主要是集中在Executor(执行器),ParameterHandler(参数处理器),ResultSetHandler(结果集处理器) ...

  7. IDA64 Fatal error before kernel init

    http://www.tuicool.com/articles/7FZVZna 第一次看到这个错误还以为是修改文件导致的,但是觉得又不大像,因为在Win7底下是完全正常的.搜索了一下才发现是由于插件导 ...

  8. Digital Adjustment of DC-DC Converter Output Voltage 电阻选择

  9. PHP 输出数据库中文是问号

  10. 《Android Studio有用指南》4.27 使用演示模式

    本文节选自<Android Studio有用指南> 第4章第27节 作者: 毕小朋 眼下本书已上传到百度阅读, 在百度中搜索[Anroid Studio有用指南]便能够找到本书. 什么是演 ...