• osgi实战学习之路:6. Service-1


    什么是Service?


    它是注冊到osgi的一个java对象


    Service注冊:


    通过BundleContext::registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties) 
    


    Service查找及使用:


    通过BundleContext::getServiceReference(java.lang.String clazz),返回ServiceReference
    通过BundleContext::getService(ServiceReference reference) ,返回注冊的服务对象


    Service释放:


    通过BundleContext::ungetService(ServiceReference reference) 



    LADP:


     轻量级文件夹訪问协议(Lightweight Directory Access Protocol)



    1个Service相应一个实现类的注冊与使用demo:


    服务提供者:

    student-manage/IStudentManage.java

    package com.demo.service;
    
    public interface IStudentManage {
    	void add();
    }
    


    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("注冊服务開始....");
    		context.registerService(IStudentManage.class.getName(),
    				new StudentManage(), null);
    		System.out.println("注冊服务结束....");
    	}
    
    	public void stop(BundleContext context) throws Exception {
    
    	}
    
    }
    

    服务使用者:

    student-action/Activator.java

    package com.demo.action;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceReference;
    
    import com.demo.service.IStudentManage;
    
    
    public class Activator implements BundleActivator{
    	public void start(BundleContext context) throws Exception {
    		System.out.println("action  begin...");
    		ServiceReference sf=null;
    		try {
    			//查找Service
    			sf=context.getServiceReference(IStudentManage.class.getName());
    			//调用服务
    			IStudentManage studentManage = (IStudentManage)context.getService(sf);
    			studentManage.add();
    		} finally {
    			context.ungetService(sf);
    		}
    		System.out.println("action  end...");
    	}
    
    	public void stop(BundleContext context) throws Exception {
    	}
    
    }
    

    部署至karaf并查看结果:




    一个Service相应多个实现(基于LDAP)demo2


    服务提供者

    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.StudentManageA;
    import com.demo.service.impl.StudentManageB;
    
    public class Activator implements BundleActivator {
    
    	public void start(BundleContext context) throws Exception {
    		System.out.println("注冊服务開始....");
    		//注冊A
    		Hashtable<String, String> dict=new Hashtable<String, String>();
    		dict.put("name", "a");
    		context.registerService(IStudentManage.class.getName(),
    				new StudentManageA(), dict);
    		//注冊B
    		dict=new Hashtable<String, String>();
    		dict.put("name", "b");
    		context.registerService(IStudentManage.class.getName(),
    				new StudentManageB(), dict);
    		System.out.println("注冊服务结束....");
    	}
    
    	public void stop(BundleContext context) throws Exception {
    
    	}
    
    }
    

    服务使用者

    student-action/Activator.java

    package com.demo.action;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceFactory;
    import org.osgi.framework.ServiceReference;
    
    import com.demo.service.IStudentManage;
    
    
    public class Activator implements BundleActivator{
    	public void start(BundleContext context) throws Exception {
    		System.out.println("action  begin...");
    		ServiceReference[] references=null;
    		try {
    			System.out.println("服务调用------------------");
    			//查找Service
    			String filter="(name=b)";
    			references=context.getServiceReferences(IStudentManage.class.getName(), filter);
    			//调用服务
    			if(references.length==1){
    				IStudentManage studentManage = (IStudentManage)context.getService(references[0]);
    				studentManage.add();
    			}else {
    				throw new IllegalArgumentException("IStudentManage查找到多个实现类");
    			}
    			
    		} finally {
    			for(ServiceReference sf:references){
    				context.ungetService(sf);
    			}
    			
    		}
    		System.out.println("action  end...");
    	}
    
    	public void stop(BundleContext context) throws Exception {
    	}
    
    }
    

    部署到karaf及查看结果:




  • 相关阅读:
    题目1202:排序------注意每个数字的后面都有空格
    题目1178:复数集合------------结构体的的比较,cmp()函数的错误让我WA了多次
    题目1041:Simple Sorting-------注意最后一个数字的处理
    题目1034:寻找大富翁---用了sort()函数,注意头文件;
    题目1415:不一样的循环队列------注意细节小地方循环队列注意%M;还有为什么M要加一!!!!!!!!!!!!!
    题目1342:寻找最长合法括号序列II---注意:不要求就近匹配,只要求( 在 )前面的任一个位置即可
    题目1398:移动次数-----最少移动的次数,那么相同的最大值越靠后越好,相同的最小值越靠前越好
    题目1375:陈博的完美主义(25分)----------------题目中对输入数字的长度有要求,所以输入字符串,用atoi()转换函数
    题目1363:欢乐斗地主------用数组计数的应用,注意1和2得排在最后,表示最大
    题目1174:查找第K小数-------qsort()可以直接对数组用;还有用unique(a,a+n);的,服!
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6734965.html
Copyright © 2020-2023  润新知