Vencat
Vencat

Reputation: 1622

create and access MongoDB database inside vert.x verticle in Kotlin

I would like like to create and access MongoDB through vert.x. I'm following this tutorial which show how to connect and work with MongoDB from vert.x, but when I try with below simple code it throws an error.

import io.vertx.core.Vertx
import io.vertx.core.json.JsonObject
import io.vertx.ext.mongo.MongoClient

fun main() {
    val vertx = Vertx.vertx()
    val config = mapOf(Pair("db_name", "testDB"), Pair("connection_string", "mongodb://localhost:27017"))
    val mongoClient = MongoClient.create(vertx, JsonObject(config))
    val document = JsonObject().put("title", "The Hobbit")
    mongoClient.save("books", document) { res ->
        if (res.succeeded()) {
        val id: String = res.result()
        println("Saved book with id $id")
        } else {
        res.cause().printStackTrace()
        }
    }
}

above code throws below error

Jun 17, 2020 2:18:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Jun 17, 2020 2:18:48 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by com.mongodb.async.client.ClientSessionHelper$1@3315d2d7 from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out

This is my grade kotlin DSL file dependencies

dependencies {
    val vertxVersion = "3.9.0"
    implementation(kotlin("stdlib-jdk8"))
    implementation("io.vertx:vertx-lang-kotlin:$vertxVersion")
    implementation("io.vertx:vertx-mongo-client:3.9.0")
}

I have verified that MongoDB is running in my machine

enter image description here

Also, how would I create a new DB using vert.x MongoClient API?

Upvotes: 1

Views: 1026

Answers (1)

Vencat
Vencat

Reputation: 1622

Above code works when I access mongoClient inside a deployed verticle. Here is my updated working code.

import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.json.JsonObject
import io.vertx.ext.mongo.MongoClient

internal class BaseVerticle : AbstractVerticle() {
    override fun start() {
        val config = mapOf(Pair("db_name", "mnSet"), Pair("connection_string", "mongodb://localhost:27017"))
        val mongoClient: MongoClient = MongoClient.create(vertx, JsonObject(config))
        val document: JsonObject = JsonObject().put("title", "The Hobbit")
        println("BasicVerticle started")
        mongoClient.save("books", document) { res ->
            if (res.succeeded()) {
                val id: String = res.result()
                println("Saved book with id $id")
            } else {
                res.cause().printStackTrace()
            }
        }
    }
    override fun stop() {
        println("BasicVerticle stopped")
    }
}

fun main() {
    val vertx = Vertx.vertx()
    vertx.deployVerticle(BaseVerticle())
}

Upvotes: 1

Related Questions