簡體   English   中英

當方法具有參數時,OSGi @Activate中斷。 錯誤:找不到激活方法; 組件將失敗

[英]OSGi @Activate breaks when method has parameters. Error: activate method not found; Component will fail

我正在嘗試對基於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方法中刪除參數時,一切正常。 所以現在我很困惑,我看到了@Activate方法具有由OSGi框架自動注入的參數的示例( http://enroute.osgi.org/services/org.osgi.service.component.html )以及其他僅空的內容,因此從我的理解來看,這應該可行。 (就像在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>

表現:

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作為私有軟件包。 這意味着這些類已嵌入到您的jar中,因此它們將與框架和DS使用的規范類不兼容。

因此,解決方案是確保僅對真正需要嵌入的內容使用私有軟件包。 切勿將其用於OSGi api軟件包。 您需要導入才能使它們工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM