Java – OSGi / equinox, declarative services and deferred loading

I'm trying to use declarative services to create a service package to provide functionality for another package However, I hope my service provider package will not start until it is needed Let me describe my situation

@H_ 301_ 8@

There are two packages: @ h_ 301_ 8@

-com. example. serviceprovider@H_301_8 @

-com. example. serviceconsumer@H_301_8 @

The service provider bundle uses declarative services to provide services, as shown below: @ h_ 301_ 8@

@H_ 301_ 8@

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true"     immediate="true" name="samplerunnable1">
<implementation class="com.example.serviceprovider.SampleRunnable"/>
<service>
    <provide interface="java.lang.Runnable"/>
</service>

The service provided by the service consumer is as follows: @ h_ 301_ 8@

@H_ 301_ 8@

<reference name="SampleRunnable"
    interface="java.lang.Runnable"
    bind="setRunnable"
    unbind="unsetRunnable"
    cardinality="1..n"
    policy="dynamic"/>

When both bundles are "active" at startup, service consumers can communicate with the services declared by the service provider without difficulty The problem arises when I try to get the service provider to start in a lazy way@ H_ 301_ 8@

After the service provider is set to load delay, this is what I got in the OSGi console: @ h_ 301_ 8@

@H_ 301_ 8@

osgi> ss
  "Framework is launched."
  id      State       Bundle
  15      STARTING    com.example.serviceconsumer_1.0.0.X
  16      RESOLVED    com.example.serviceprovider_1.0.0.X

What I expect to see is that even if bundle 16 is only "resolved", it will at least register services However, when I call the bind command, it declares that there is no registered service@ H_ 301_ 8@

@H_ 301_ 8@

osgi> bundle 16
com.example.serviceprovider_1.0.0.X [17]
  Id=17,Status=RESOLVED    Data Root=C:\apache\apache-tomcat-.0.40\work\Catalina\localhost\examplesX\eclipse\configuration\org.eclipse.osgi\bundles\17\data
  "No registered services."
  No services in use.
  No exported packages
  Imported packages
     org.osgi.framework; version="1.7.0"<org.eclipse.osgi_3.8.0.v20120529-1548 [0]>
  No fragment bundles
  Named class space
    com.example.serivceprovider; bundle-version="1.0.0.X"[provided]
  No required bundles

Perhaps I have missed the basic concepts of lazy load bundling and service registration If the bundle is in the resolved state, should all connections be connected? (that is, there is a class loader, and the resolved import and export dependencies and services are registered.) If the service consumer attempts to access the service, should the bundle be converted to the "active" state? What did I miss here@ H_ 301_ 8@

Solution

Bundles in resolved state cannot provide services, and declarative services will ignore them You should usually start all bundles at startup, even if you need to delay loading behavior The key is to quickly activate the bundle (or free!), And only pay the initialization cost of components when needed

@H_ 301_ 8@

DS is responsible for delayed activation by default To do this, you do not need to enable or change anything In essence, DS publishes service entries in the registry, but it does not actually instantiate your components (or even load their classes) until a client tries to use the service@ H_ 301_ 8@

In addition, because DS does not load classes before it is needed, OSGi does not even need to create a classloader for the bundle, as long as your bundle does not have a bundleactivator@ H_ 301_ 8@

Again, you should not try to keep your bundle resolved Such bundles can only export static code and resources, but they can't "do" anything or participate in service registration@ H_ 301_ 8@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>