zoukankan      html  css  js  c++  java
  • 监听OSGi服务

    方法一:实现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模块!");
            }
        }
    }
  • 相关阅读:
    接口的应用(入门级)—— 插件式开发
    什么是接口(入门篇)——使你的程序功能丰富
    什么是接口(入门篇)
    【搞定Jvm面试】 面试官:谈谈 JVM 类文件结构的认识
    【搞定Jvm面试】 JDK监控和故障处理工具揭秘
    【搞定Jvm面试】 JVM 垃圾回收揭秘附常见面试题解析
    【搞定Jvm面试】 Java 内存区域揭秘附常见面试题解析
    【真实面试经历】我和阿里面试官的一次“邂逅”(附问题详解)
    【原创!推荐!】不了解布隆过滤器?一文给你整的明明白白!
    【搞定 Java 并发面试】面试最常问的 Java 并发进阶常见面试题总结!
  • 原文地址:https://www.cnblogs.com/eastson/p/3624806.html
Copyright © 2011-2022 走看看