OSGI声明服务不打印任何内容

时间:2014-08-20 10:11:02

标签: declarative-services

亲爱的有才华的程序员! 我一直在使用OSGI框架,并试图在我的程序中应用Declarative Service,但它在屏幕上没有打印任何内容。 我的程序很简单,只有一个接口,一个类实现它,一个类使用接口作为客户端。

- 界面为:

package test.osdids.date;

public interface IDateService {
String getDate();
                              }

- 该类实现接口,因为服务是:

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;
                        }

                                                  }

注册服务的XML是:

<?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>

该服务的清单文件为:

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

- 现在,这是将服务作为客户端并打印出结果的类(此类位于其他插件项目中,与之前存储的插件项目不同接口和上面的实现类):

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!");
}

}

- 这是使用和绑定消费者类服务的XML文件:

<?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>

- 这是清单文件:

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

- 这是屏幕上的结果:

osgi> 

(什么都没发生)

  • 但是当我使用jUnit测试来测试类DateConsumer中的方法activate()时,它工作正常。但是,当我尝试通过OSGI框架运行插件项目时,没有任何反应。

我希望有人知道这个问题并帮助我。 非常感谢提前!

1 个答案:

答案 0 :(得分:0)

我得到了答案,只需通过Eclipse框架(产品)启动捆绑使用者,而不是通过OSGI框架启动捆绑使用者,然后捆绑将顺利运行