无法从另一个捆绑包获取服务引用

问题描述 投票:0回答:1

我在onos升级工作。其中一项升级是我替换了 org.apache.felix.scr。 org.osgi.service.component.annotations。 这是一些捆绑包 onos-api、onos-core-*、onos-app-tunnel-api、onos-app-tunnel 这是我使用bundle:list后卡拉夫指挥官的状态

    195 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-api
    196 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-app-tunnel-api

    210 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-core-common
    211 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-core-dist
    212 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-core-net
    213 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-core-persistence
    214 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-core-primitives
    215 │ Active  │  80 │ 3.0.0.SNAPSHOT     │ onos-core-serializers
    
    224 │ Waiting │  80 │ 3.0.0.SNAPSHOT     │ onos-app-tunnel

karaf.log 中的一些有用信息

2024-11-22T10:11:44,947 | INFO  | SCR Component Actor | DeviceManager                    | 212 - org.onosproject.onos-core-net - 3.0.0.SNAPSHOT | DeviceManager ContextClassLoader:jdk.internal.loader.ClassLoaders$AppClassLoader@31b7dea0

2024-11-22T10:11:44,948 | INFO  | SCR Component Actor | DeviceManager                    | 212 - org.onosproject.onos-core-net - 3.0.0.SNAPSHOT | DeviceManager activate eventDispatcher:org.onosproject.event.impl.CoreEventDispatcher@3fce3b41

2024-11-22T10:11:45,734 | INFO  | SCR Component Actor | LinkManager                      | 212 - org.onosproject.onos-core-net - 3.0.0.SNAPSHOT | LinkManager activate eventDispatcher:org.onosproject.event.impl.CoreEventDispatcher@3fce3b41

karaf.log 错误:

 2024-11-22T10:11:46,670 | INFO  | SCR Component Actor | LabelResourceManager             | 224 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | LabelResourceManager activate eventDispatcher:*null*
2024-11-22T10:11:46,671 | ERROR | SCR Component Actor | LabelResourceManager             | 224 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | bundle org.onosproject.onos-app-tunnel:3.0.0.SNAPSHOT (224)[org.onosproject.incubator.net.resource.label.impl.LabelResourceManager(126)] : The activate method has thrown an exception
java.lang.NullPointerException: null
        at org.onosproject.incubator.net.resource.label.impl.LabelResourceManager.activate(LabelResourceManager.java:79) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
        at org.apache.felix.scr.impl.inject.methods.BaseMethod.invokeMethod(BaseMethod.java:244) ~[?:?]
        at org.apache.felix.scr.impl.inject.methods.BaseMethod.access$500(BaseMethod.java:41) ~[?:?]

        ......
        at org.apache.felix.scr.impl.manager.RegistrationManager.changeRegistration(RegistrationManager.java:133) ~[?:?]
        at org.apache.felix.scr.impl.manager.AbstractComponentManager.registerService(AbstractComponentManager.java:984) ~[?:?]
        at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:752) ~[?:?]
        at org.apache.felix.scr.impl.manager.AbstractComponentManager.enableInternal(AbstractComponentManager.java:674) ~[?:?]
        at org.apache.felix.scr.impl.manager.AbstractComponentManager$1.run(AbstractComponentManager.java:461) ~[?:?]
        at org.apache.felix.scr.impl.ComponentActorThread.run(ComponentActorThread.java:114) ~[?:?]
        at java.lang.Thread.run(Thread.java:834) ~[?:?]


        2024-11-22T10:11:46,748 | INFO  | SCR Component Actor | TunnelManager                    | 224 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | TunnelManager ContextClassLoader:jdk.internal.loader.ClassLoaders$AppClassLoader@31b7dea0

2024-11-22T10:11:46,749 | INFO  | SCR Component Actor | TunnelManager                    | 224 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | TunnelManager activate eventDispatcher:null
2024-11-22T10:11:46,749 | INFO  | SCR Component Actor | TunnelManager                    | 224 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | EventDeliveryService: jar:bundle://523d00ed-aa23-45cb-bad2-ff7996d8a72e_195.0:0/!/

org.onosproject.net.device.impl 中的DeviceManager.java 和 org.onosproject.net.link.impl 包中的LinkManager.java 位于bundle onos-core-net中,其服务接口org.onosproject.net.device中的DeviceService.java和org.onosproject.net.link中的LinkService.java位于bundle onos-api中

org.onosproject.incubator.net.resource.label.impl 中的LabelResourceManager.java 和 org.onosproject.incubator.net.tunnel.impl 中的 TunnelManager.java 位于捆绑包 onos-app-tunnel 中 org.onosproject.incubator.net.resource.label 中的 LabelResourceService.java 和 org.onosproject.incubator.net.tunnel 中的 TunnelService 位于包 onos-app-tunnel-api 中

@Component(immediate = true, service = {TunnelService.class, TunnelAdminService.class, TunnelProviderRegistry.class})
public class TunnelManager
        extends AbstractListenerProviderRegistry<TunnelEvent, TunnelListener,
                                                 TunnelProvider, TunnelProviderService>
        implements TunnelService, TunnelAdminService, TunnelProviderRegistry {
    private static final String TUNNNEL_ID_NULL = "Tunnel ID cannot be null";
    private static final String TUNNNEL_NULL = "Tunnel cannot be null";

    private final Logger log = getLogger(getClass());

    private final TunnelStoreDelegate delegate = new InternalStoreDelegate();

    @Reference(cardinality = ReferenceCardinality.MANDATORY)
    protected TunnelStore store;


    @Activate
    public void activate(ComponentContext context) {
        store.setDelegate(delegate);
        log.info("TunnelManager ContextClassLoader:{}",Thread.currentThread().getContextClassLoader());
        log.info("TunnelManager ClassLoader:{}",getClass().getClassLoader());
        log.info("TunnelManager activate eventDispatcher:{}", eventDispatcher);
        log.info("EventDeliveryService: {}", EventDeliveryService.class.getProtectionDomain().getCodeSource().getLocation());
        eventDispatcher.addSink(TunnelEvent.class, listenerRegistry);
        log.info("Started");
    }

AbstractListenerProviderRegistry.java 位于 onos-api 包中的 org.onosproject.net.provider 包中 有一个由 TunnelManager 继承的服务引用

@Reference(cardinality = ReferenceCardinality.MANDATORY)
    protected EventDeliveryService eventDispatcher;

但是在 karaf.log 中该服务为空 所有捆绑包的版本是

<version>3.0.0-SNAPSHOT</version>

在捆绑 onos-api jar 文件中,

Bundle-SymbolicName: org.onosproject.onos-api
Bundle-Version: 3.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package:
org.onosproject.event;version="3.0.0",
...
 org.onosproject.store.primitives;uses:="com.google.common.collect,org.o
 nlab.util,org.onosproject.cluster,org.onosproject.core,org.onosproject.
 event,org.onosproject.store.service";version="3.0.0",
 org.onosproject.store;uses:="org.onosproject.event";version="3.0.0",
 org.onosproject.store.service;uses:="com.google.common.collect,org.onla
 b.util,org.onosproject.cluster,org.onosproject.core,org.onosproject.eve
 nt,org.onosproject.store,org.onosproject.store.primitives";version="3.0
 .0",
 org.onosproject.store.cluster.messaging;uses:="org.onlab.packet,org.ono
 sproject.cluster";version="3.0.0",
...
org.onosproject.net.provider;uses:="org.onosproject.event,org.onosproje
 ct.net";version="3.0.0",
Bundle-SymbolicName: org.onosproject.onos-app-tunnel
Bundle-Version: 3.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
...

Import-Package:
 com.fasterxml.jackson.databind.node;version="[2.11,3)",
 com.google.common.base;version="[22.0,23)",
 com.google.common.collect;version="[22.0,23)",
 org.apache.karaf.shell.api.action;version="[4.3,5)",
 org.apache.karaf.shell.api.action.lifecycle;version="[4.3,5)",
 org.onlab.packet;version="[3.0,4)",
 org.onlab.util;version="[3.0,4)",
 org.onosproject.cli;version="[3.0,4)",
 org.onosproject.cluster;version="[3.0,4)",
 org.onosproject.core;version="[3.0,4)",
 org.onosproject.event;version="[3.0,4)",
 org.onosproject.incubator.net.domain;version="[3.0,4)",
 org.onosproject.incubator.net.resource.label;version="[3.0,4)",
 org.onosproject.incubator.net.tunnel;version="[3.0,4)",
 org.onosproject.mastership;version="[3.0,4)",
 org.onosproject.net;version="[3.0,4)",
 org.onosproject.net.device;version="[3.0,4)",
 org.onosproject.net.provider;version="[3.0,4)",
 org.onosproject.store;version="[3.0,4)",
 org.onosproject.store.cluster.messaging;version="[3.0,4)",
 org.onosproject.store.primitives;version="[3.0,4)",
 org.onosproject.store.serializers;version="[3.0,4)",
 org.onosproject.store.service;version="[3.0,4)",
 org.onosproject.ui;version="[3.0,4)",
 org.onosproject.ui.table;version="[3.0,4)",
 org.onosproject.ui.table.cell;version="[3.0,4)",
 org.osgi.service.component;version="[1.4,2)",
 org.slf4j;version="[1.7,2)"

我不知道为什么我无法获得服务引用,尽管它们具有相同的类加载器

有人可以给点建议吗? 先谢谢你了

这是我尝试过的 当我直接使用@Reference服务时,服务为空,但是当我使用bundleContext时它可以获取服务,这是代码


@Activate
    public void activate(ComponentContext context) {  BundleContext blContext = context.getBundleContext();
        ServiceReference<EventDeliveryService> sr = blContext.getServiceReference(EventDeliveryService.class);if(sr != null) {
                eventDispatcher = blContext.getService(sr);
                log.info("TunnelManager  eventDispatcher from context:{}", eventDispatcher);
        }
...
}

日志是


2024-11-27T16:35:03,126 | INFO  | SCR Component Actor | TunnelManager                    | 225 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | TunnelManager ServiceReference: [org.onosproject.event.EventDeliveryService]
2024-11-27T16:35:03,127 | INFO  | SCR Component Actor | TunnelManager                    | 225 - org.onosproject.onos-app-tunnel - 3.0.0.SNAPSHOT | TunnelManager  eventDispatcher from context:org.onosproject.event.impl.CoreEventDispatcher@5c78b416 

同时


2024-11-27T16:35:04,622 | INFO  | SCR Component Actor | DeviceManager                    | 213 - org.onosproject.onos-core-net - 3.0.0.SNAPSHOT | DeviceManager activate eventDispatcher:org.onosproject.event.impl.CoreEventDispatcher@5c78b416

reference annotations osgi apache-karaf onos
1个回答
0
投票

它可以从其他bundle中的组件获取服务引用,但无法获取从其他bundle中的基类继承的服务引用。

这是这个网站的解决方案 OSGI @Component 注解不包含扩展现有 OSGI 服务时基类所需的引用

在 pom.xml 中添加一些配置之前 编译后的目录 target/classes/OSGI-INF/org.onosproject.incubator.net.tunnel.impl.TunnelManager.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.onosproject.incubator.net.tunnel.impl.TunnelManager" immediate="true" activate="activate" deactivate="deactivate">
  <implementation class="org.onosproject.incubator.net.tunnel.impl.TunnelManager"/>
  <service>
    <provide interface="org.onosproject.incubator.net.tunnel.TunnelService"/>
    <provide interface="org.onosproject.incubator.net.tunnel.TunnelAdminService"/>
    <provide interface="org.onosproject.incubator.net.tunnel.TunnelProviderRegistry"/>
  </service>
  <reference name="store" cardinality="1..1" interface="org.onosproject.incubator.net.tunnel.TunnelStore" field="store"/>
</scr:component>

在一些 pom.xml 中添加插件后,我从 TunnelManager.xml 中的基类中获取由 @Reference 注释的引用

<plugins>
    <plugin>
        <groupId>biz.aQute.bnd</groupId>
        <artifactId>bnd-maven-plugin</artifactId>
        <version>3.5.0</version>
        <executions>
            <execution>
                <id>run-bnd</id>
                <goals>
                    <goal>bnd-process</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <bnd><![CDATA[-dsannotations-options: inherit]]></bnd>
        </configuration>
    </plugin>
</plugins>

    <?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.onosproject.incubator.net.tunnel.impl.TunnelManager" immediate="true" activate="activate" deactivate="deactivate">
  <implementation class="org.onosproject.incubator.net.tunnel.impl.TunnelManager"/>
  <service>
    <provide interface="org.onosproject.incubator.net.tunnel.TunnelService"/>
    <provide interface="org.onosproject.incubator.net.tunnel.TunnelAdminService"/>
    <provide interface="org.onosproject.incubator.net.tunnel.TunnelProviderRegistry"/>
  </service>
  <reference name="eventDispatcher" cardinality="1..1" interface="org.onosproject.event.EventDeliveryService" field="eventDispatcher"/>
  <reference name="store" cardinality="1..1" interface="org.onosproject.incubator.net.tunnel.TunnelStore" field="store"/>
</scr:component>

谢谢这个网站,除非你使用正确的搜索关键字,否则它有答案

© www.soinside.com 2019 - 2024. All rights reserved.