osgi實戰學習之路:8. Service-3之ServiceTracker,osgiservice-3

來源:互聯網
上載者:User

osgi實戰學習之路:8. Service-3之ServiceTracker,osgiservice-3
通過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);}@Overridepublic Object addingService(ServiceReference reference) {IStudentManage manage=new LogStudentManager(context, reference);return manage;}@Overridepublic 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...");}}


結果:





在VM arguments上添加參數-Dorgosgiservicehttpport=8080後啟動程式,該程式自動結束?

eclipse方式不是-D,而是vmargs,建議看看eclipse的協助。
 
OSGI 整與 webService 用eclipse Equinox 怎弄 ?講講都有什 方法?

如果你建立一個web service的話,直接用他內建的組建就可以了,如果根據WSDL產生用戶端的話,也可以根據他的組建 建立一個 web service client,然後輸入WSDL地址,然後下一步下一步就可以了
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.