2013-11-09 33 views
0

我已经在OSGi中使用套接字完成了一个客户端服务器模型。我在服务器端有一个bundle,我的activator类调用一个创建套接字并从客户端获取String数据的线程。现在我想从服务器端调用服务,以便我可以发送该字符串进行一些处理。我如何去做这件事?通过服务器线程调用OSGi服务

这是在服务器端

int serverport=5000; 
    Thread t; 
    public void start(BundleContext bundleContext) throws Exception { 
     Activator.context = bundleContext; 
     t = new StdServer(serverport,this); 
     t.start(); 

的StdServer类我Activator类将扩展处理该套接字创建一个线程。我想在激活器的启动功能中调用一个服务。任何帮助深表谢意。

+0

看看远程服务http://r-osgi.sourceforge.net/ – Mirco

回答

0

您可以通过getService()获得服务参考,并将其作为新线程的构造函数参数。

+0

感谢Areo。我发送了一个Activator对象给线程的构造函数,通过这个,我可以获得服务引用和服务对象。 – sdwaraki

1

如果我正在读你,那么你仍然在服务器端的OSGi环境中,以及在同一个容器中运行的服务(如Karaf)。用你的激活器,你可以在上下文中得到它,你试过吗?

另一种使用Bnd Annotations的方法也需要声明性服务安装在您的容器中。然后使用BND注解,你可以注释一个类像这样在“@Reference”将让你从容器中需要的服务:

import java.util.Map; 

import org.osgi.framework.BundleContext; 

import aQute.bnd.annotation.component.Activate; 
import aQute.bnd.annotation.component.Component; 
import aQute.bnd.annotation.component.Deactivate; 
import aQute.bnd.annotation.component.Reference; 

//Doesn't have to be called Activator 
@Component 
public class Activator { 
    private BundleContext context; 
    private TheServiceINeed theServiceINeed; 

@Activate 
public void Activate(BundleContext context, Map<String, Object> props) { 
this.context = context; 

} 

@Deactivate 
public void Deactivate() { 
    this.context = null; 
} 

public TheServiceINeed getTheServiceINeed() { 
    return theServiceINeed; 
} 

    //The Service to process my String 
@Reference 
public void setTheServiceINeed(TheServiceINeed theServiceINeed) { 
    this.theServiceINeed = theServiceINeed; 
    } 

} 

是否使用BndTools与做你的工作?如果你问我,对于OSGi Development来说非常方便。

+0

非常感谢Tony的回复。我使用getContext函数来获取上下文并通过它获得服务引用对象。 – sdwaraki

+0

YOu先生。我很高兴它为你解决。 – Tony