A little question about the declarative service during osgi

Source: Internet
Author: User

 

Put my interfaces and implementations in a bundle

Interface code:

Public interface myservice {

Public String gethello ();

}

 

Implementation Code:

Import org. osgi. Service. component. componentcontext;

Import osgi. Service. myservice;

 

Public class myserviceimpl implements myservice {

Protected void activate (componentcontext context ){

System. Out

. Println ("myservice component active, within the bundle lifecircle .");

}

 

Public void deactivate (componentcontext context) throws exception {

System. Out

. Println ("myservice component deactive, within the bundle lifecircle .");

}

 

@ Override

Public String gethello (){

// Todo auto-generated method stub

Return "no spring osgi ";

}

 

}

 

Call implementation is another bundle

Import org. Eclipse. osgi. Framework. Console. commandprovider;

Import osgi. Service. myservice;

 

Public class testosgi implements commandprovider {

Myservice MS;

Public synchronized void setrunnable (myservice R ){

MS = R;

System. Out. println (Ms. gethello ());

System. Out. println ("this is testosgi declarative service .");

}

Public synchronized void unsetrunnable (myservice R ){

MS = NULL;

}

@ Override

Public String gethelp (){

// Todo auto-generated method stub

Return NULL;

}

}

The problem lies in the implementation of the call. I must implement an interface (such as commandprovider), which is embodied in the XML file as the provider interface of the service. At this time, you can see the output. If I do not implement the interface, after running it, you can see that the output is active through the command SS, but there is no output, that is to say, testosgi does not execute the setrunnable method and myserviceimpl does not execute the activate method.

 

Finally, we know that the interface must be implemented as a Service. Therefore, testosgi is not used as a Service. Therefore, you can delete a service segment in the bundle XML, but use it as a common component.

 

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.