简体   繁体   English

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

[英]Unable to connect to mongo database using Java, OSGI, Karaf

I've installed the mongo driver in my running Karaf server: 我已经在运行的Karaf服务器中安装了mongo驱动程序:

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

I'm simply trying to connect to the DB and log the databases I have. 我只是试图连接到数据库并记录我拥有的数据库。 Currently running out of the box local instance. 当前正在运行的本地实例。 Below is the code I wrote to demo this in OSGI/Karaf. 以下是我在OSGI / Karaf中演示该代码的代码。 I'm using the mvn bundle plugin. 我正在使用mvn捆绑插件。

I created a database under the alias osgiDatabase 我在别名osgiDatabase下创建了一个数据库

I'm running my debugger and the failure happens during the instantiation of the MongoClient() but not understanding what I could be doing wrong. 我正在运行调试器,但失败发生在实例化MongoClient()但我不了解自己可能做错了什么。

This works when I don't use Karaf. 当我不使用Karaf时,此方法有效。 The only error I get is Activator start error in bundle 我得到的唯一错误是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 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);
    }
  }


}

Activator 活化剂

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");
  }
}

Your Import-Package configuration looks wrong. 您的导入包配置看起来不正确。 If you configure it explicitly like this you switch off the auto detection of needed packages. 如果像这样显式配置它,则会关闭对所需软件包的自动检测。 So it is very likely you are missing some packages your code needs. 因此,很可能您会丢失一些代码所需的软件包。

Instead try to only configure the activator and leave the rest on defaults. 而是尝试仅配置激活器,其余配置保留默认值。

To get better logs you should use a try catch in your Activator an log the exception using slf4j. 为了获得更好的日志,您应该在Activator中使用try catch,并使用slf4j记录异常。 So you get some more information what is wrong. 因此,您可以获得更多信息,这是什么地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM