我有一个数据源功能,可以将数据源导出为OSGi服务:
> services -p 2038
OPS4J Pax JDBC Config (2038) provides:
--------------------------------------
objectClass = [org.osgi.service.cm.ManagedServiceFactory]
service.bundleid = 2038
service.id = 211
service.pid = org.ops4j.datasource
service.scope = singleton
----
databaseName = foobar
dataSourceName = fooDatasource
felix.fileinstall.filename = file:/home/foousr/apache-karaf-4.0.6/etc/org.ops4j.datasource-foo.cfg
objectClass = [javax.sql.DataSource]
osgi.jndi.service.name = fooDatasource
service.bundleid = 2038
service.factoryPid = org.ops4j.datasource
service.id = 251
service.pid = org.ops4j.datasource.b3020619-71b9-4876-94c3-477f3e4a503d
service.scope = singleton
url = jdbc:oracle:thin:@dbserver:99999/foo
user = FOOUSR
作为创建和注册此数据源服务的ds-feature的一部分,它还包含一个可用于测试数据源的ping-ds软件包:
> jdbc:ping-ds fooDatasource
Ping from localhost(127.0.0.1) as FOOUSR to schema FOOUSR on dbserver/foo
我有一个使用该数据源的蓝图包:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/blueprint"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint
http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0
http://svn.apache.org/repos/asf/aries/trunk/blueprint/blueprint-cm/src/main/resources/org/apache/aries/blueprint/compendium/cm/blueprint-cm-1.1.0.xsd">
<reference id="ds" interface="javax.sql.DataSource" filter="(dataSourceName=fooDatasource)"/>
<camelContext id="fooDatasourceTestContext" trace="true" xmlns="http://camel.apache.org/schema/blueprint">
<route id="fooDatasourceTest">
<from uri="timer:/fooDatasourceTest?fixedRate=true&repeatCount=1"/>
<setBody>
<simple>
select * from FOOUSR.FOOTABLE
</simple>
</setBody>
<to uri="jdbc:ds" />
<to uri="log:fooDatasourceTest?showBody=true"/>
</route>
</camelContext>
</blueprint>
当我做一个功能时:安装foo-datasource-test-feature我得到一个错误抱怨无法找到数据源服务 – 但它在那里我可以使用我的ping-ds cmd访问它.
Error executing command: Unable to resolve root: missing requirement [root] osgi.identity;
osgi.identity=foo-datasource-test-feature; type=karaf.feature; version="[0.0.1.SNAPSHOT,0.0.1.SNAPSHOT]";
filter:="(&(osgi.identity=foo-datasource-test-feature)(type=karaf.feature)(version>=0.0.1.SNAPSHOT)(version<=0.0.1.SNAPSHOT))"
[caused by: Unable to resolve foo-datasource-test-feature/0.0.1.SNAPSHOT: missing requirement
[foo-datasource-test-feature/0.0.1.SNAPSHOT] osgi.identity; osgi.identity=com.company.project.foo-datasource-test;
type=osgi.bundle; version="[0.0.1.SNAPSHOT,0.0.1.SNAPSHOT]"; resolution:=mandatory [caused by: Unable to resolve
com.company.project.foo-datasource-test/0.0.1.SNAPSHOT: missing requirement
[com.company.project.foo-datasource-test/0.0.1.SNAPSHOT] osgi.service; effective:=active;
filter:="(&(objectClass=javax.sql.DataSource)(dataSourceName=fooDatasource))"]]
似乎抱怨它无法找到已安装的OSGi服务的数据源:
osgi.service; effective:=active;
filter:="(&(objectClass=javax.sql.DataSource)(dataSourceName=fooDatasource))"]]
有什么奇怪的,除了我编写的ping-ds命令工作得很好的事实,如果我只是安装它所抱怨的功能中的测试包,它就可以了.这意味着这是该功能的某种问题:安装过程本身.
在foo-datasource-test-feature功能中,我包含一个引用ds-feature的foo-core-feature:
FOO – 数据源 – 测试 – feature.xml的:
<?xml version="1.0" encoding="utf-8"?>
<features name="foo-datasource-test" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<feature name="foo-datasource-test-feature" version="${project.version}">
<feature>foo-core-feature</feature>
<bundle>mvn:com.company.project/foo-datasource-test/${project.version}</bundle>
</feature>
</features
FOO-核心feature.xml的:
<?xml version="1.0" encoding="utf-8"?>
<features name="foo-core" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<feature name="foo-core-feature" version="${project.version}">
<feature>ds-feature</feature>
...
</feature>
</features>
DS-features.xml:
<?xml version="1.0" encoding="UTF-8"?>
<features name="ds-features" xmlns="http://karaf.apache.org/xmlns/features/v1.0.0">
<feature name="ds-feature" version="${project.version}" >
<feature>pax-jdbc-config</feature>
...
<bundle start-level="86">mvn:com.company.commons/foo-datasource/${project.version}</bundle>
</feature>
<feature name="ds-ping-datasource" version="${project.version}" >
<bundle start-level="80">mvn:com.company.commons/foo-ping-datasource/${project.version}</bundle>
<feature>pax-jdbc-config</feature>
</feature>
</features>
这会导致问题吗?如果是这样,因为foo-datasource-test-feature依赖于已安装的数据源服务,那么在我的功能中描述这种依赖关系的正确方法是什么呢?
使用:
Karaf版本4.0.6
骆驼版2.16.5
UPDATE
我注释掉了对核心功能的引用,以便只有捆绑包在测试功能中,它仍然会抱怨.因此,这与功能依赖关系的功能无关.
我打算尝试karaf 4.1.0版.
UPDATE
4.1.0没有喜悦.作为旁注,由于还没有构建activemq-client 5.14.4,因此存在更多问题.
最佳答案 显然,功能:安装正在寻找不在OSGi服务注册表中的服务,而是在其他地方的MANIFEST中查找.一位同事让我看着MANIFEST文件,我注意到在foo-datasource-test中它的MANIFEST文件中有一个Import-Service头:
Import-Service: javax.sql.DataSource;multiple:=false;filter=(dataSourceName=fooDatasource)
似乎错误是因为它正在执行我的MANIFEST文件要求它做的事情 – 即导入服务.为什么它在OSGi服务注册表中找不到它,我不知道.但是在任何MANIFEST文件中都没有相应的Export-Service.但是,由于Import-Service和Export-Service明显被弃用,我的旧代码运行在我正在迁移到karaf的保险丝上没有通知;-)我决定只是找到一种方法来删除任何Import-或Export-服务标头.
在this advice之后,我补充道
<_removeheaders>Import-Service,Export-Service</_removeheaders>
在我的foo-core-feature pom.xml中的maven-bundle-plugin的指令部分(回想一下foo-datasource-test-feature依赖于foo-core-feature):
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.2.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
. . .
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Import-Package>*,org.apache.camel.core.osgi</Import-Package>
<_removeheaders>Import-Service,Export-Service</_removeheaders>
</instructions>
</configuration>
</plugin>
<plugins>
</build>
在mvn clean部署之后,功能:安装工作,它运行得很好(找到数据源,使用它并返回sql结果集).
像往常一样,在寻找很长一段时间之后,它是简单或基本的东西.我不确定为什么功能:安装不会只检查OSGi注册表,但是可能有一个很好的理由(我希望)因为它不依赖于它并寻找一个MANIFEST头.不确定Provide-Capability标头在这种情况下是否运行得更好,但可能会尝试一下.