Osgi practical learning path: 7. ServiceListener of Service-2, osgiservice-2

Source: Internet
Author: User

Osgi practical learning path: 7. ServiceListener of Service-2, osgiservice-2
Three statuses of ServiceListener:

ServiceEvent.REGISTEREDServiceEvent.MODIFIEDServiceEvent.UNREGISTERING



Service search demo Based on ServiceListener

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. studentManageA; import com. demo. service. impl. studentManageB; public class Activator implements BundleActivator {public void start (BundleContext context) throws Exception {System. out. println ("registration service starts .... "); // register BSystem. out. println ("Register Service B .... "); Hashtable <String, String> dict = new Hashtable <String, String> (); dict. put ("name", "B"); context. registerService (IStudentManage. class. getName (), new StudentManageB (), dict); // register ASystem. out. println ("Register Service .... "); dict = new Hashtable <String, String> (); dict. put ("name", "a"); context. registerService (IStudentManage. class. getName (), new StudentManageA (), dict); System. out. println ("registration service ends .... ");} public void stop (BundleContext context) throws Exception {}}


Consumer

Student-action/Activator. java

Package com. demo. action; import java. util. sortedSet; import java. util. treeSet; import org. osgi. framework. bundleActivator; import org. osgi. framework. bundleContext; import org. osgi. framework. constants; import org. osgi. framework. serviceEvent; import org. osgi. framework. serviceFactory; import org. osgi. framework. serviceListener; import org. osgi. framework. serviceReference; import org. osgi. service. log. logService; imp Ort com. demo. service. IStudentManage; public class Activator implements BundleActivator {BundleContext context; UserManagerListener listener; public void start (BundleContext context) throws Exception {System. out. println ("action listener begin... "); this. context = context; listener = new UserManagerListener (); System. out. println ("service call ----------------"); synchronized (listener) {String filter = "(" + Constant S. OBJECTCLASS + "=" + IStudentManage. class. getName () + ")"; context. addServiceListener (listener, filter); ServiceReference [] refs = context. getServiceReferences (null, filter); if (refs! = Null) {for (ServiceReference r: refs) {listener. serviceChanged (new ServiceEvent (ServiceEvent. REGISTERED, r) ;}}// search for ServiceIStudentManage studentManage = listener. getStudentManageService (); // call the service studentManage. add (); System. out. println ("action listener end... ");} public void stop (BundleContext context) throws Exception {}// listener class UserManagerListener implements ServiceListener {SortedSet <ServiceReference> studentManageRefs = new TreeSet <ServiceReference> (); public synchronized void serviceChanged (ServiceEvent event) {switch (event. getType () {case ServiceEvent. REGISTERED: studentManageRefs. add (event. getServiceReference (); break; case ServiceEvent. MODIFIED: break; case ServiceEvent. UNREGISTERING: studentManageRefs. remove (event. getServiceReference (); break; default: break;}/** Search Service * @ return IStudentManage */public synchronized IStudentManage getStudentManageService () {if (studentManageRefs. size ()> 0) {System. out. println ("size:" + studentManageRefs. size (); return (IStudentManage) context. getService (studentManageRefs. last () ;}return null ;}}}


Result:





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.

After I upgrade Windows 7 Service pack 1 to Service pack 2, the system will be restored to Service pack 1 and cannot be upgraded to Service pack 2.

Microsoft does not have SP2 on its official website. I do not know where your SP2 came from,

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.