简体   繁体   中英

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

I've installed the mongo driver in my running Karaf server:

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. I'm using the mvn bundle plugin.

I created a database under the alias 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.

This works when I don't use Karaf. The only error I get is 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);
    }
  }


}

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. So you get some more information what is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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