Osgi practical learning path: ServiceTracker of Service-3, osgiservice-3

Source: Internet
Author: User

Osgi practical learning path: ServiceTracker of Service-3, osgiservice-3
ServiceTracker can be used to expand the Service to be searched.

The following demo introduces the decorator mode to expand the Service logs.

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); // enables managerTracker. open (); // get the service IStudentManage service = (IStudentManage) managerTracker. getService (); service. add (); System. out. println ("action start end... ");} public void stop (BundleContext context) throws Exception {// disable 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...");}}


Result:





Add the parameter-Dorgosgiservicehttpport = 8080 on the VM arguments to start the program. Will the program automatically end?

The eclipse method is not-D, but vmargs. It is recommended to check the help of eclipse.
 
How to Use eclipse Equinox for OSGI and webService integration? What methods do you have to talk about?

If you create a web service, you can directly use its built-in components. If you generate a client based on WSDL, you can also create a web service client based on its components, enter the WSDL address, and then click Next.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.