Skip to content

Instantly share code, notes, and snippets.

@Narigo
Last active December 17, 2015 03:58
Show Gist options
  • Select an option

  • Save Narigo/5546664 to your computer and use it in GitHub Desktop.

Select an option

Save Narigo/5546664 to your computer and use it in GitHub Desktop.
This is working now.. with another ExecutionContext
[19:20:14] <purplefox> Narigo: ok found the problem
[19:20:24] <purplefox> Narigo: the problem is you're playing with threads in your verticle
[19:20:38] <purplefox> Narigo: you're using a fork join
[19:20:49] <purplefox> Narigo: and calling deployVerticle on the result of that
[19:20:56] <purplefox> Narigo: which is not the vert.x thread
[19:21:16] <purplefox> Narigo: so vert.x doesn't know what verticle you're in
[19:21:28] <purplefox> Narigo: basically - don't use other threads in verticles
[19:21:38] <purplefox> Narigo: you're also opening yourself up to race conditions
[19:22:15] <purplefox> Narigo: I'll add some better exceptions in vert.x so you'll get a better message next time you do this
[19:22:37] <purplefox> Narigo: "deploVerticle called from a non Vert.x thread. You are a very naughty boy!!"
[19:22:50] <purplefox> anyway - i need to dinnerise now
[19:23:15] <purplefox> btw - to run the result on the vertx thread do
[19:23:27] <purplefox> vertx.getContext().runOnContext( { handler } )
[19:23:45] <purplefox> do the getContext before the fork join
package com.campudus.vertxmoduleregistry
import scala.concurrent.{ Future, Promise }
import org.vertx.java.core.AsyncResult
import org.vertx.java.core.json.JsonObject
import org.vertx.java.platform.Verticle
import com.campudus.vertx.helpers.VertxScalaHelpers
import com.campudus.vertxmoduleregistry.database.Database
import com.campudus.vertxmoduleregistry.security.Authentication
import org.vertx.java.core.AsyncResultHandler
import org.vertx.java.core.Handler
class ModuleRegistryStarter extends Verticle with VertxScalaHelpers {
val mongoPersistorModName = "io.vertx~mod-mongo-persistor~2.0.0-SNAPSHOT"
val authManagerModName = "io.vertx~mod-auth-mgr~2.0.0-SNAPSHOT"
val unzipModName = "io.vertx~mod-unzip~1.0.0-SNAPSHOT"
val unzipAddress = "io.vertx.unzipper"
val serverVerticle = "com.campudus.vertxmoduleregistry.ModuleRegistryServer"
override def start() {
lazy val logger = container.logger()
implicit val ec = new scala.concurrent.ExecutionContext() {
def execute(runnable: Runnable): Unit = runnable.run()
def reportFailure(t: Throwable): Unit = logger.error("problem: " + t)
}
logger.error("Starting module registry ...")
val config = Option(container.config()).getOrElse(new JsonObject)
val configDb = config.getObject("database", new JsonObject)
.putString("address", Database.dbAddress)
val configAuth = config.getObject("auth", new JsonObject)
.putString("persistor_address", Database.dbAddress)
.putString("address", Authentication.authAddress)
val configUnzip = config.getObject("unzip", new JsonObject)
.putString("address", unzipAddress)
println("deploying all modules with " + config)
deployModule(mongoPersistorModName, configDb)
.map(id => println("deployed mongo persistor with id: " + id))
.flatMap(_ => deployModule(authManagerModName, configAuth))
.map(id => println("deployed auth manager with id: " + id))
.flatMap(_ => deployModule(unzipModName, configUnzip))
.map(id => println("deployed unzip module with id: " + id))
.flatMap(_ => deployVerticle(serverVerticle, config))
.map(id => println("deployed verticle with id: " + id))
println("Modules should deploy async now.")
}
override def stop() {
println("Module registry stopped.")
}
private def deployVerticle(name: String, config: JsonObject): Future[String] = {
val p = Promise[String]
container.deployVerticle(name, config, new AsyncResultHandler[String]() {
def handle(deployResult: AsyncResult[String]) = {
if (deployResult.succeeded()) {
println("started " + name + " with config " + config)
p.success(deployResult.result())
} else {
println("failed to start " + name + " because of " + deployResult.cause())
deployResult.cause().printStackTrace()
p.failure(deployResult.cause())
}
}
})
p.future
}
private def deployModule(name: String, config: JsonObject): Future[String] = {
val p = Promise[String]
container.deployModule(name, config, new AsyncResultHandler[String]() {
def handle(deployResult: AsyncResult[String]) = {
if (deployResult.succeeded()) {
println("started " + name + " with config " + config)
p.success(deployResult.result())
} else {
println("failed to start " + name + " because of " + deployResult.cause())
p.failure(deployResult.cause())
}
}
})
p.future
}
}
Starting module registry ...
deploying all modules with {}
Modules should deploy async now.
started io.vertx~mod-mongo-persistor~2.0.0-SNAPSHOT with config {"address":"registry.database"}
deployed mongo persistor with id: deployment-0e3decd7-fbee-4aa7-8787-e5dba69c8159
started io.vertx~mod-auth-mgr~2.0.0-SNAPSHOT with config {"persistor_address":"registry.database","address":"registry.auth"}
deployed auth manager with id: deployment-c1a293e7-4c48-404e-b06f-be3214c2f5eb
started io.vertx~mod-unzip~1.0.0-SNAPSHOT with config {"address":"io.vertx.unzipper"}
deployed unzip module with id: deployment-be2d1643-b0f1-46c6-ad0c-3e0d0b7b51e4
failed to start com.campudus.vertxmoduleregistry.ModuleRegistryServer because of java.lang.NullPointerException
java.lang.NullPointerException
at sun.misc.URLClassPath.<init>(URLClassPath.java:103)
at sun.misc.URLClassPath.<init>(URLClassPath.java:113)
at java.net.URLClassLoader.<init>(URLClassLoader.java:146)
at org.vertx.java.platform.impl.ModuleClassLoader.<init>(ModuleClassLoader.java:49)
at org.vertx.java.platform.impl.DefaultPlatformManager.doDeployVerticle(DefaultPlatformManager.java:494)
at org.vertx.java.platform.impl.DefaultPlatformManager.access$1300(DefaultPlatformManager.java:52)
at org.vertx.java.platform.impl.DefaultPlatformManager$10.run(DefaultPlatformManager.java:346)
at org.vertx.java.platform.impl.DefaultPlatformManager$9.run(DefaultPlatformManager.java:320)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment