我正在尝试对基于OSGI的应用程序实现优美的关闭挂钩。我正在以捆绑范围实现它,因为我无法直接访问启动器源。
这是类:
/**
* 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);
}
}
但是,每当我启动它时,它都会返回错误:
错误:[shutdownhookactivator(8)]未找到激活方法[start]; 组件将失败
但是,当我从 start> start 方法中删除参数时,一切都起作用。所以现在我很困惑,我已经看到了@Activate方法具有由OSGI框架自动注射的参数(http://enroute.osgi.org/services/org.osgi.osgi.service.service.service.component.html),还有其他只是空的,因此据我了解,这应该有效。(感觉就像在Osgi有关)
使用DS注释并获取框架BundleContext,实现此目的的正确方法是什么?有可能吗?
编辑:OSGI环境:BNDTOOLS(aqute Launcher?)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>
清单:
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
从清单起,很明显您的问题会发生。您将OSGI API作为私人包。这意味着这些类嵌入了您的罐子中,因此它们与框架和DS使用的规格类别不兼容。
因此,解决方案是确保仅将私有软件包用于真正需要嵌入的东西。切勿将其用于OSGI API软件包。您需要有进口才能工作。