方法一:实现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. 【转】_CrtSetBreakAlloc 内存泄漏

    声明:本内容转载自 https://my.oschina.net/gp8wLHNUeKd2C/blog/110707 在使用VS开发过程中,(今日)遇到内存泄漏的情况.具体表现为: 在输出窗口中,显示 ...

  2. noip2011初赛提高组 试题详解

    转载自:https://blog.csdn.net/Eirlys_North/article/details/52889993 一.单项选择题(共20题,每题1.5分,共计30分,每题有且仅有一个正确 ...

  3. 通过wifi上网,桥接模式下virtualBox虚拟机无法连上网的解决办法

    https://jingyan.baidu.com/article/948f59242e601dd80ff5f929.html

  4. asp.net url传值,弹窗

    一,<a>标签链接式传值 1, <a href="News_list.aspx?ClassID=<%#((DataRowView)Container.DataItem ...

  5. Java乱码解决

    简述 乱码是JAVA开发时经常遇到的问题.主要出现在四种情况: 1.         系统接口之间 2.         POST提交数据 3.         GET提交数据和URL路径 4.    ...

  6. CentOS下的yum upgrade和yum update区别,没事别乱用,和Ubuntu的update不一样!

    说明:生产环境对软件版本和内核版本要求非常精确,别没事有事随便的进行yum update操作!!!!!!!!! yum update:升级所有包同时也升级软件和系统内核 yum upgrade:只升级 ...

  7. X11 FRAMEBUFFER QT

    之前对X11 FRAMEBUFFER理解的不够,现在总结一下Qt Embedded是挪威Trolletch公司的图形化界面开发工具Qt的嵌入式版本,它通过QtAPI与LinuxI/O以及Framebu ...

  8. Appium+python自动化11-adb必知必会的几个指令

    前言 学android测试,adb是必学的,有几个常用的指令需要熟练掌握 一.检查设备 1.如何检查手机(或模拟器)是连上电脑的,在cmd输入: >adb devices

  9. 一个 forceLayout() 和 requestLayout() 的测试

    两个view: 一个是系统默认的FrameLayout,  A 一个是自己自定义的MyView extends View,重载了onMeasure函数(): B @Override protected ...

  10. Android 4.4 KitKat升级率已经接近18%(2014-07-09 07:29)

    腾讯数码讯(编 译:张秀梅)按照惯例, 每个月的第一个星期的星期一谷歌都会发布最新一期Android版本分布图.从去年十月末谷歌发布Android 4.4 KitKat以来,截止到目前为止Androi ...