【问题标题】:Export sqljdbc4 as OSGi service将 sqljdbc4 导出为 OSGi 服务
【发布时间】:2016-07-26 13:33:35
【问题描述】:

我正在尝试使用 sqlserver-database 作为端点创建骆驼路由。我开始明白在 OSGi 中集成数据源的常用方法是将它们导出为 OSGi 服务。

出于测试和开发目的,我使用了 H2 内存数据库并成功将其导出为服务。然而,当我尝试用 SQL Server 做同样的事情时,我遇到了一些问题。

我下载了 SQL Server JDBC 驱动程序并将 .jar 添加到我的本地 Maven 存储库中:

$ mvn install:install-file -Dpath=<path-to-jar> 
    -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0.2206
    -Dpackaging=jar

然后我继续通过创建 pom.xml 将 .jar 包装在 osgi-bundle 中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.microsoft</groupId>
    <artifactId>sqljdbc4-osgi-bundle</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SQL Server JDBC Driver</name>
    <packaging>bundle</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId> 
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version> 
                <extensions>true</extensions> 
                <configuration> 
                    <instructions> 
                        <Embed-Dependency>*</Embed-Dependency>
                        <_exportcontents>
                            com.microsoft.sqlserver.jdbc.*
                            , microsoft.sql.*
                        </_exportcontents>
                    </instructions>
                </configuration> 
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0.2206</version>
        </dependency>
    </dependencies>
</project>

我能够在 Karaf 中成功安装捆绑包,因此我继续创建我的服务蓝图:

<?xml version="1.0" encoding="utf-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/1.0.0">
    <bean id="dataSource" class="com.microsoft.sqlserver.jdbc.SQLServerDataSource">
        <property name="URL" value=<db-connectionString> />
        <property name="user" value=<username> />
        <property name="password" value=<pw> />
    </bean>
    <service id="dataSourceService" ref="dataSource" interface="javax.sql.DataSource">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/myDb" />
        </service-properties>
    </service>
</blueprint>

现在,当我将蓝图复制到 /deploy 时,没有任何反应,没有安装任何包,日志中也没有任何内容。当我为 H2 数据源做同样的事情时,安装了一个包。

我正在使用 JBoss Fuse 6.2.1、camel-version 2.15.1 和 Karaf 2.4.0

我将非常感谢您对此事的任何帮助。

【问题讨论】:

  • 您是否将 blueprint.xml 视为 karaf 中的一个包?
  • @ChristianSchneider,对于 H2 数据源,是的,但不是 sql server 一。我想我可能搞砸了正确包装驱动程序,但我不确定。
  • 尝试使用wrap:mvn:协议安装原驱动
  • @ChristianSchneider,我尝试使用“osgi:install -s wrap:mvn:com.microsoft.sqlserver/sqljdbc4/4.0.2206”,它显示为一个捆绑包,但在复制我的数据源服务到 /deploy
  • 您可以尝试将 blueprint.xml 放入包中并使用 install -s 进行部署。这样 karaf 可能会为您提供更多信息。

标签: apache-camel osgi apache-karaf jbossfuse blueprint-osgi


【解决方案1】:

在让它按原样工作后,我建议你看看 pax-jdbc。

https://ops4j1.jira.com/wiki/display/PAXJDBC/Create+DataSource+from+config https://ops4j1.jira.com/wiki/display/PAXJDBC/Pooling+and+XA+support+for+DataSourceFactory

它使用 OSGi 规范中的 DataSourceFactory 以通用方式访问数据库。还有一个用于适配 mssql 的 karaf 功能,称为 pax-jdbc-mssql。

基本上它允许只使用一个配置文件为 mssql 创建一个数据源。您不必为它创建蓝图。

【讨论】:

    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2011-02-16
    • 2014-04-24
    相关资源
    最近更新 更多