Osgi practice: 6. Service-1

Source: Internet
Author: User
What is service?

It is a Java object registered to osgi.


Service Registration:

Through bundlecontext: registerservice (Java. Lang. String [] clazzes, java. Lang. Object Service, java. util. dictionary properties)


Service search and use:

Through bundlecontext: getservicereference (Java. Lang. String clazz), return servicereference through bundlecontext: getservice (servicereference reference), return the registered service object


Service Release:

Use bundlecontext: ungetservice (servicereference reference)



Ladp:

Lightweight Directory Access Protocol)



One Service corresponds to the registration and use demo of an implementation class:

Service provider:

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 ("registration service starts .... "); context. registerservice (istudentmanage. class. getname (), new studentmanage (), null); system. out. println ("registration service ends .... ");} public void stop (bundlecontext context) throws exception {}}

Service User:

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 {// search servicesf = context. getservicereference (istudentmanage. class. getname (); // call the service istudentmanage studentmanage = (istudentmanage) context. getservice (SF); studentmanage. add ();} finally {context. ungetservice (SF);} system. out. println ("action end... ");} public void stop (bundlecontext context) throws exception {}}

Deploy to Karaf and view the result:




One Service corresponds to multiple implementations (based on LDAP) demo2

Service 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 ahashtable <string, string> dict = new hashtable <string, string> (); dict. put ("name", "A"); context. registerservice (istudentmanage. class. getname (), new studentmanagea (), dict); // register bdict = new hashtable <string, string> (); dict. put ("name", "B"); context. registerservice (istudentmanage. class. getname (), new studentmanageb (), dict); system. out. println ("registration service ends .... ");} public void stop (bundlecontext context) throws exception {}}

Service User

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 call ----------------"); // search servicestring filter = "(name = B)"; References = context. getservicereferences (istudentmanage. class. getname (), filter); // call the service if (references. length = 1) {istudentmanage studentmanage = (istudentmanage) context. getservice (references [0]); studentmanage. add ();} else {Throw new illegalargumentexception ("multiple implementation classes found in istudentmanage") ;}} finally {for (servicereference SF: References) {context. ungetservice (SF) ;}} system. out. println ("action end... ");} public void stop (bundlecontext context) throws exception {}}

Deploy to Karaf and view the result:




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.