繁体   English   中英

通过服务器线程调用OSGi服务

[英]call OSGi service through a server thread

我已经在OSGi中使用套接字完成了客户端服务器模型。 我在服务器端有一个捆绑包,其中我的激活器类调用一个线程,该线程创建一个套接字并从客户端获取String数据。 现在,我想从服务器端调用服务,以便可以发送此字符串进行某些处理。 我该怎么办?

这是我在服务器端的Activator类

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

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

如果我没看错,那么您仍然在服务器端的OSGi环境中,以及使用在同一容器(如Karaf)中运行的服务的方式。 使用激活器,您可以获取上下文,您是否尝试过?

另一种使用Bnd注释的方法也需要在容器中安装声明性服务。 然后使用Bnd Annotations,您可以对类似这样的类进行注释,其中“ @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非常方便。

您可以使用getService()获取服务引用,并将其作为新线程的构造参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM