无法使用 Java、OSGI、Karaf 连接到 mongo 数据库

Unable to connect to mongo database using Java, OSGI, Karaf

我已经在我的 运行 Karaf 服务器中安装了 mongo 驱动程序:

bundle:install -s wrap:mvn:org.mongodb/mongo-java-driver/3.6.3

我只是想连接到数据库并记录我拥有的数据库。当前 运行 开箱即用的本地实例。下面是我在 OSGI/Karaf 中编写的演示代码。我正在使用 mvn bundle 插件。

我在别名 osgiDatabase

下创建了一个数据库

我是 运行 我的调试器,失败发生在 MongoClient() 的实例化过程中,但我不明白我可能做错了什么。

这在我不使用 Karaf 时有效。我得到的唯一错误是 Activator start error in bundle

POM

<?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.qa</groupId>
  <artifactId>board</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>bundle</packaging>

  <dependencies>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>3.6.3</version>
    </dependency>
    <dependency>
      <groupId>org.osgi</groupId>
      <artifactId>org.osgi.core</artifactId>
      <version>6.0.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Import-Package>com.mongodb, org.osgi.framework</Import-Package>
            <Bundle-Activator>Connection.Activator</Bundle-Activator>
            <Export-Package>*</Export-Package>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>



</project>

DBUtil

package Connection;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import java.util.List;

public class DBUtil {

  MongoClient client;
  MongoDatabase database;

  public DBUtil() {
  }

  public DBUtil(String databaseName) {
    if (client == null) {
      client = new MongoClient();
      database = client.getDatabase(databaseName);
    }
  }

  /**
   * Allows you to reveal all databases under the current connection
   */
  public void showDatabases() {
    if (client == null) {
      throw new NullPointerException();
    }

    List<String> databases = client.getDatabaseNames();
    for (String db : databases) {
      System.out.println("The name of the database is: " + db);
    }
  }


}

激活剂

package Connection;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

  public void start(BundleContext bundleContext) throws Exception {
    DBUtil util = new DBUtil("osgiDatabase");
//    util.showDatabases();
    System.out.println("Working");
  }

  public void stop(BundleContext bundleContext) throws Exception {
    System.out.println("Bundle disabled");
  }
}

您的 Import-Package 配置看起来不对。如果你像这样明确地配置它,你会关闭对所需包的自动检测。所以很可能你遗漏了一些你的代码需要的包。

而是尝试只配置激活器并将其余部分保留为默认值。

为了获得更好的日志,您应该在 Activator 中使用 try catch,并使用 slf4j 记录异常。所以你会得到更多的信息是什么出了问题。