0
votes

Dear talented programmers! I have been working with OSGI framework and was trying to applied Declarative Service in my program, but it printed nothing on the screen. My program is simple, just have one interface, one class implements it and one class consume the interface as the client.

- The Interface is:

package test.osdids.date;

public interface IDateService {
String getDate();
                              }

- The class implements Interface as the Service is:

package test.osdids.date.service;

import java.util.Calendar;

import test.osdids.date.IDateService;

public class DateService implements IDateService {

@Override
public String getDate() {
    String date = Calendar.getInstance().getTime().toString();
    return date;
                        }

                                                  }

The XML to registered the service is:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
name="test.osdids.date.service">
<implementation class="test.osdids.date.service.DateService" />
<service>
    <provide interface="test.osdids.date.IDateService" />
</service>
</scr:component>

The Manifest file of the service is:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TESTOSDIDSDATESERVICE
Bundle-SymbolicName: TESTOSDIDSDATESERVICE
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Service-Component: OSGI-INF/DateBridge.xml
Export-Package: test.osdids.date
Bundle-ActivationPolicy: lazy

- And now, this is the class which will consume the service as the client and prints out the result ( This class was located in other plug-in project, different from the former plug-in project which stored the interface and the implementation class above):

package test.osdids.date.consumer;

import test.osdids.date.IDateService;

public class DateConsumer {
private IDateService dateService;

public synchronized void setService(IDateService dateService) {
    this.dateService = dateService;
    System.out
            .println("The Date Service has been registered successfully!");
    System.out.println("The current time is: " + dateService.getDate());

}

public synchronized void unsetService(IDateService dateService) {
    System.out
            .println("The Date Service has been unregistered successfully!");

}

public void activate() {
    System.out.println("Test again...");
    System.out.println("The current time is: " + dateService.getDate());

}

public void deactivate() {
    System.out.println("Stop the service!");
}

}

- This is the XML file to consume and bind the service of consumer class:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
 name="test.osdids.date.consumer" activate="activate"
 deactivate="deactivate" enabled="true"  immediate="true">
<implementation class="test.osdids.date.consumer.DateConsumer" />
<reference bind="setService" cardinality="1..1"
    interface="test.osdids.date.IDateService" name="IDateService" policy="static"
    unbind="unsetService" />
</scr:component>

- This is the manifest file:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TESTOSDIDSDATECONSUMER
Bundle-SymbolicName: TESTOSDIDSDATECONSUMER
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Require-Bundle: TESTOSDIDSDATESERVICE;bundle-version="1.0.0"
Service-Component: OSGI-INF/DateBridgeConsumer.xml
Import-Package: test.osdids.date
Bundle-ActivationPolicy: lazy

- And here is the result on the screen:

osgi> 

(Nothing happened)

  • But when I use jUnit test to test the method activate() in class DateConsumer, it worked. However, when I tried to run the plug-in project by OSGI framework, nothing happened.

I hope that someone who knows this problem and help me. Thanks a lot in advance!

1

1 Answers

0
votes

I got the answer, just launch the bundle consumer by Eclipse framework (product), not by OSGI framework, then the bundles will work smoothly