• osgi实战学习之路:8. Service-3之ServiceTracker


    通过ServiceTracker能够对查找的Service进行扩展

    以下的demo引入装饰器模式对Service进行日志的扩展

    demo:

    Provider

    student-manage/Activator.java

    package com.demo.service;
    
    import java.util.Dictionary;
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.Map;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    
    import com.demo.service.impl.StudentManage;
    
    public class Activator implements BundleActivator {
    
    	public void start(BundleContext context) throws Exception {
    		System.out.println("register service start...");
    		Dictionary<String, String> prop=new Hashtable<String, String>();
    		prop.put("action", "student_action");
    		context.registerService(IStudentManage.class.getName(), new StudentManage(), prop);
    		System.out.println("register service end...");
    	}
    
    	public void stop(BundleContext context) throws Exception {
    
    	}
    
    }
    


    Consumer

    student-action/Activator.java

    package com.demo.action;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    
    import com.demo.action.log.LogStudentManager;
    import com.demo.action.tracker.StudentManagerTracker;
    import com.demo.service.IStudentManage;
    
    public class Activator implements BundleActivator{
    	StudentManagerTracker managerTracker ;
    	public void start(BundleContext context) throws Exception {
    		System.out.println("action start begin...");
    		managerTracker=new StudentManagerTracker(context);
    		//开启
    		managerTracker.open();
    		//获取服务
    		IStudentManage service=(IStudentManage)managerTracker.getService();
    		service.add();
    		System.out.println("action start end...");
    	}
    
    	public void stop(BundleContext context) throws Exception {
    		//关闭
    		managerTracker.close();
    	}
    
    }
    


    student-action/StudentManagerTracker.java

    package com.demo.action.tracker;
    
    import org.omg.PortableInterceptor.INACTIVE;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.Filter;
    import org.osgi.framework.ServiceReference;
    import org.osgi.util.tracker.ServiceTracker;
    import org.osgi.util.tracker.ServiceTrackerCustomizer;
    
    import com.demo.action.log.LogStudentManager;
    import com.demo.service.IStudentManage;
    
    public class StudentManagerTracker extends ServiceTracker {
    	
    	public StudentManagerTracker(BundleContext context) {
    		super(context, IStudentManage.class.getName(), null);
    	}
    
    	@Override
    	public Object addingService(ServiceReference reference) {
    		IStudentManage manage=new LogStudentManager(context, reference);
    		return manage;
    	}
    
    	@Override
    	public void open() {
    		super.open();
    	}
    	
    }
    


    student-action/LogStudentManager.java

    package com.demo.action.log;
    
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceReference;
    
    import com.demo.service.IStudentManage;
    
    public class LogStudentManager implements IStudentManage {
    	IStudentManage studentManage;
    	BundleContext context;
    	ServiceReference reference;
    	
    	public LogStudentManager(BundleContext context, ServiceReference reference) {
    		this.context = context;
    		this.reference = reference;
    	}
    
    	public void add() {
    		studentManage=(IStudentManage) context.getService(reference);
    		System.out.println("log start...");
    		studentManage.add();
    		System.out.println("log end...");
    	}
    
    }
    


    结果:




  • 相关阅读:
    解决“验证码代码完毕且页面代码没问题但网页无法显示验证码图片”
    关于服务器已经启动“localhost拒绝你的请求”
    解决eclipse项目在浏览器中启动时老是报404错误可是路径没错
    解决“错误: 找不到或无法加载主类 org.apache.catalina.startup.Bootstrap”
    解决“The Tomcat server configuration at ServersTomcat v8.0 Server at localhost-config is missing. Check the server for errors.”
    解决“The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path”
    解决“JSONArray cannot be resolved to a type”
    Windows10系统截图
    解决“ eclipse出现Exception in thread ‘main’ java.lang.NoClassDefFoundError”
    解决“has value‘1.7’,but 'jdk1.8' is requred”
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4255364.html
Copyright © 2020-2023  润新知