方法一:实现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. wampserver -- 解决Exception Exception in module wampmanager.exe at 000F15A0

    Learn from: http://hi.baidu.com/spt_form/item/4b4533476c3b92a6de2a9f78 系统:windows2003 32bit wampserv ...

  2. CAShapeLayer实现音量大小动态改变

    我是效果图 实现如图这效果一般会直接通过多张图进行切换进行完成.但这样的处理,会浪费App的资源存储空间,而且效率也不高.那么今天我们用CAShapeLayer实现以下吧. 拆分: 1.一个椭圆 2. ...

  3. Java实现-每天三道剑指Offre(2-4)

    实现一个单例模式 /** * 面试题2:实现单例模式 * * @author qiuyong 饿汉式 */ public class Singleton01 { private Singleton01 ...

  4. hdu4337 King Arthur's Knights

    King Arthur's Knights Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  5. ASCII表 基本记忆 -- C

    /* ASCII表规则记忆 我们仅仅要记住了一个字母或数字的 ASCII 码 (比如记住 A 为 65 , 0 的 ASCII 码为 48 ), 知道对应的大写和小写字母之间差 32. 0 -- 32 ...

  6. Windows Embedded Compact 7网络编程概述(下)

    11.1.1 Select I/O模型 在Windows CE中,Select模型是唯一被支持的I/O模型.Select I/O模型就是利用select函数对I/O进行管理. 函数select的功能在 ...

  7. Caffe简单入门 AI

    https://yq.aliyun.com/articles/112207?spm=5176.100239.bloglist.58.wN003U

  8. mysql数据库测试库下载

    The mysqlslap program can be helpful for simulating a high load produced by multiple clients issuing ...

  9. 接口安全--http数字签名

    原文:https://blog.csdn.net/u011521890/article/details/55506716 import java.io.UnsupportedEncodingExcep ...

  10. 玩转android自定义控件二——自定义索引栏listview

    带索引栏的listview,在android开发非常普遍,方便用户进行字母索引,就像微信通讯录这样: 今天,我们就从零到一实现这个具有索引栏的listview. 怎么实现这个控件了,我们应当梳理出一个 ...