gpt4 book ai didi

java - 当方法有参数时,OSGi @Activate 中断。错误 : activate method not found; Component will fail

转载 作者:行者123 更新时间:2023-11-30 06:46:21 25 4
gpt4 key购买 nike

我正在尝试为我的基于 OSGi 的应用程序实现一个正常的关闭 Hook 。我在一个包中实现它,因为我无法直接访问启动器源。

这是类:

/**
* Shutdown Hook for OSGi
* Based On: https://stackoverflow.com/a/32216407/5284104
*/
@Component()
public class ShutdownHookActivator{

@Activate
public void start(ComponentContext cc, BundleContext bc, Map<String,Object> config) {
Thread hook = new Thread() {
@Override
public void run() {
System.out.println("Stopping OSGi Framework.");
try {
Framework systemBundle = bc.getBundle(0).adapt(Framework.class);
systemBundle.stop();
System.out.println("Waiting up to 2s for OSGi shutdown to complete...");
systemBundle.waitForStop(2000);
} catch (Exception e) {
System.err.println("Failed to cleanly shutdown OSGi Framework: " + e.getMessage());
e.printStackTrace();
}
}
};

System.out.println("Installing shutdown hook.");
Runtime.getRuntime().addShutdownHook(hook);
}
}

但是每当我启动它时,它都会返回一个错误:

ERROR: [ShutdownHookActivator(8)] activate method [start] not found; Component will fail

但是当我从 start 方法中删除参数时,一切正常。所以现在我很困惑,我看到了一些示例,其中 @Activate 方法具有由 OSGi 框架( http://enroute.osgi.org/services/org.osgi.service.component.html )自动注入(inject)的参数,而其他的只是空的,所以据我所知这应该有效。 (感觉在 OSGi 中全靠运气)

通过使用 DS 注释和获取 Framework BundleContext 来实现它的正确方法是什么?有可能吗?

编辑:OSGi环境:BndTools(aQute 启动器?)Apache Felix SCR 2.0.12(OSGi DS 注释 - 6.0.1)来源可以在这里看到:https://github.com/Jafre13/ISS-Product

编辑 2:自动生成的 ShutdownHookActivator.xml:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="dk.sdu.sso.sred.utils.ShutdownHookActivator" activate="start">
<implementation class="dk.sdu.sso.sred.utils.ShutdownHookActivator"/>
</scr:component>

list :

Manifest-Version: 1.0
Bnd-LastModified: 1512410080758
Bundle-ManifestVersion: 2
Bundle-Name: dk.sdu.sso.sred
Bundle-SymbolicName: dk.sdu.sso.sred
Bundle-Version: 0.0.0.201712041754
Created-By: 1.8.0_151 (Oracle Corporation)
Import-Package: javax.security.auth.x500,junit.framework,org.junit,org
.xml.sax,org.xml.sax.ext,org.xml.sax.helpers
Private-Package: dk.sdu.sso.sred.cmd,dk.sdu.sso.sred.lingpipe,dk.sdu.s
so.sred.api;version="1.0.0",dk.sdu.sso.sred,com.aliasi.test.unit.xml,
com.aliasi.test.unit.io,com.aliasi.suffixarray,com.aliasi.test.unit.u
til,com.aliasi.test.unit.crf,com.aliasi.matrix,com.aliasi.test.unit.s
entences,com.aliasi.test.unit,com.aliasi.tag,com.aliasi.test.unit.hmm
,com.aliasi.chunk,com.aliasi.coref,com.aliasi.spell,com.aliasi.test.u
nit.chunk,com.aliasi.test.unit.features,com.aliasi.coref.matchers,com
.aliasi.lm,com.aliasi.test.unit.coref,com.aliasi.cluster,com.aliasi.f
eatures,com.aliasi.test.unit.symbol,com.aliasi.io,com.aliasi.test.uni
t.tokenizer,com.aliasi.sentences,com.aliasi.test.unit.coref.matchers,
com.aliasi.test.unit.spell,com.aliasi.test.unit.corpus,com.aliasi.uti
l,com.aliasi.dca,com.aliasi.symbol,com.aliasi.test.unit.lm,com.aliasi
.dict,com.aliasi.test.unit.tag,com.aliasi.corpus,com.aliasi.classify,
com.aliasi.test.unit.dca,com.aliasi.tokenizer,com.aliasi.test.unit.cl
assify,com.aliasi.crf,com.aliasi.test.unit.cluster,com.aliasi.stats,c
om.aliasi.test.unit.stats,com.aliasi.test.unit.dict,com.aliasi.test.u
nit.matrix,com.aliasi.test.unit.suffixarray,com.aliasi.xml,com.aliasi
.hmm,dk.sdu.sso.sred.utils,org.apache.felix.service.command,org.osgi.
framework;version="1.8",org.osgi.service.component;version="1.3"
Provide-Capability: osgi.service;objectClass:List<String>="dk.sdu.sso.
sred.api.SRedAPI",osgi.service;objectClass:List<String>="dk.sdu.sso.s
red.cmd.ModelCommands",osgi.service;objectClass:List<String>="dk.sdu.
sso.sred.cmd.SRedCommands"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=osgi.compo
nent)(version>=1.3.0)(!(version>=2.0.0)))",osgi.ee;filter:="(&(osgi.e
e=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/dk.sdu.sso.sred.SRed.xml,OSGI-INF/dk.sdu.s
so.sred.cmd.ModelCommands.xml,OSGI-INF/dk.sdu.sso.sred.cmd.SRedComman
ds.xml,OSGI-INF/dk.sdu.sso.sred.utils.ShutdownHookActivator.xml
Tool: Bnd-3.5.0.201709291849

最佳答案

从 list 中可以很明显地看出您的问题发生的原因。您将 OSGi API 作为私有(private)包。这意味着这些类嵌入到您的 jar 中,因此它们将与框架和 DS 使用的规范类不兼容。

因此解决方案是确保您只对真正需要嵌入的内容使用私有(private)包。切勿将其用于 OSGi api 包。您需要导入它们才能工作。

关于java - 当方法有参数时,OSGi @Activate 中断。错误 : activate method not found; Component will fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com