OSGI:在重新启动捆绑包后,Activate和Bind方法的调用顺序发生变化。

huangapple go评论82阅读模式
英文:

OSGI: Activate and Bind methods changes starting order after restarting the bundle

问题

在我的项目中,我有一个OSGI bundle。在这个bundle中,我有一个Activated方法和一个bind(to ConfigurationAdmin)方法。

当我第一次运行项目时,Activated方法首先被调用,因此我可以初始化所有我需要的东西,但是如果我停止bundle,然后再次启动它,bind方法会首先被调用,然后我会遇到一个空指针异常(因为在Activated中的初始化尚未被调用)。

bind方法中的引用为"cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC"

为什么第二次启动时顺序会改变?

@Component(configurationPid = "ConsulService", immediate = true, service = ConsulService.class)
public class ConsulServiceImpl implements ConsulService {

    private ConfigurationAdmin configurationAdmin;
    private BundleContext context;
    private Consul consul;

    @Override
    public AgentClient agentClient() {
        return consul.agentClient();
    }

    @Override
    public KeyValueClient keyValueAgent() {
        return consul.keyValueClient();
    }

    @Activate
    public void activate(BundleContext bundleContext) {
        //这会在停止和重新启动bundle后导致空指针异常
        //因为此方法未被调用,"consul"为null
        this.consul = Consul.builder().build();
        this.context = bundleContext;
    }

    ...

    @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, unbind = "unbindConfigurationAdmin")
    public void bindConfigurationAdmin(final ConfigurationAdmin configurationAdmin) {
        this.configurationAdmin = configurationAdmin;
        //在这里我遇到空指针异常,因为consul未初始化
        KeyValueClient keyValueAgent = keyValueAgent();
        ...
    }
}
英文:

in my project I have an OSGI bundle. In this bundle I have an Activated method and a bind(to ConfigurationAdmin) method.

When I run the project for the first time the Activated method is called first, so i can initialize all the things I need, but if I stop the bundle and then start it again the bind method is called first and I have a nullpointer(because the initializations in Activate are not called yet).

The reference at the bind method is "cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC"

Why when started for the second time the order changes?

@Component(configurationPid = "ConsulService", immediate = true, service = ConsulService.class)
public class ConsulServiceImpl implements ConsulService {

private ConfigurationAdmin configurationAdmin;
private BundleContext context;
private Consul consul;

@Override
public AgentClient agentClient() {
	return consul.agentClient();
}

@Override
public KeyValueClient keyValueAgent() {
	return consul.keyValueClient();
}

@Activate
public void activate(BundleContext bundleContext) {
//this cause the nullpointer after the stop and the restarting of this bundle
//since this method is not called "consul" is null
	this.consul = Consul.builder().build();
	this.context = bundleContext;
}

...

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, unbind = "unbindConfigurationAdmin")
public void bindConfigurationAdmin(final ConfigurationAdmin configurationAdmin) {
	this.configurationAdmin = configurationAdmin;
	// Here I have nullpointer because consul is not initializated 
	KeyValueClient keyValueAgent = keyValueAgent();
    ...
}

答案1

得分: 1

绑定方法可以在激活方法之前调用。实际上,绑定方法必须在激活方法之前调用,以供静态引用使用。将激活方法视为后构造方法。如果您希望将激活方法作为构造函数使用,则可以使用在DS 1.4中支持的构造函数注入。

英文:

Bind methods can be called before the activate method. In fact, bind methods must be called before the activate method for static references. Think of an activate method as a post-construct method. If you require your activate method to be your constructor, then use constructor injection which is supported in DS 1.4.

答案2

得分: 0

似乎在服务可用时调用了绑定方法。
在第一次启动时,当调用Activated方法时,configurationAdmin尚不可用,但是当我停止捆绑包,然后再次重新启动时,configurationAdmin就可用了,并且在激活之前调用了绑定。

英文:

It seems that the binding method is called when the service is available.
At the first start when the Activated method is called the configurationAdmin is not available yet, but when I stop the bundle and then restart it again the configurationAdmin is available and the bind is called before activated.

huangapple
  • 本文由 发表于 2020年4月8日 16:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61096938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定