Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Promise variants of start and stop methods #18

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<properties>
<!--Dependency versions-->
<guice.version>4.1.0</guice.version>
<vertx.version>3.3.0</vertx.version>
<vertx.version>3.8.3</vertx.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,41 @@ public void init(Vertx vertx, Context context) {
* This is useful if your verticle deploys other verticles or modules and you don't want this verticle to
* be considered started until the other modules and verticles have been started.
*
* @deprecated Use {@link #start(Promise)} instead
* @param startedResult When you are happy your verticle is started set the result
* @throws Exception
*/
@Deprecated
@Override
public void start(Future<Void> startedResult) throws Exception {
// Start the real verticle
realVerticle.start(startedResult);
}

/**
* Start the verticle instance.
* <p>
* Vert.x calls this method when deploying the instance. You do not call it yourself.
* <p>
* A promise is passed into the method, and when deployment is complete the verticle should either call
* {@link io.vertx.core.Promise#complete} or {@link io.vertx.core.Promise#fail} the future.
*
* @param startPromise the future
*/
@Override
public void start(Promise<Void> startPromise) throws Exception {
// Start the real verticle
realVerticle.start(startPromise);
}

/**
* Vert.x calls the stop method when the verticle is undeployed.
* Put any cleanup code for your verticle in here
*
* @deprecated Use {@link #stop(Promise)} instead
* @throws Exception
*/
@Deprecated
@Override
public void stop(Future<Void> stopFuture) throws Exception {
// Stop the real verticle
Expand All @@ -111,6 +131,25 @@ public void stop(Future<Void> stopFuture) throws Exception {
}
}

/**
* Stop the verticle instance.
* <p>
* Vert.x calls this method when un-deploying the instance. You do not call it yourself.
* <p>
* A promise is passed into the method, and when un-deployment is complete the verticle should either call
* {@link io.vertx.core.Promise#complete} or {@link io.vertx.core.Promise#fail} the future.
*
* @param stopPromise the future
*/
@Override
public void stop(Promise<Void> stopPromise) throws Exception {
// Stop the real verticle
if (realVerticle != null) {
realVerticle.stop(stopPromise);
realVerticle = null;
}
}

public String getVerticleName() {
return verticleName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.englishtown.vertx.guice.integration;

import com.englishtown.vertx.guice.MyDependency;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;

import javax.inject.Inject;

import static org.junit.Assert.assertNotNull;

public class DependencyInjectionVerticle3 extends AbstractVerticle {

public static final String EB_ADDRESS = "et.address";
private final MyDependency myDependency;

@Inject
public DependencyInjectionVerticle3(MyDependency myDependency) {
this.myDependency = myDependency;
assertNotNull(myDependency);
}

@Override
public void start(Promise<Void> startPromise) throws Exception {
vertx.eventBus()
.<Void>consumer(EB_ADDRESS)
.handler(msg -> msg.reply(myDependency.getClass().getName()));

startPromise.complete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,38 @@ protected void configure() {
await();
}


@Test
public void testDependencyInjection_PromiseStartMethod() throws Exception {

Injector parent = Guice.createInjector(new GuiceVertxBinder(vertx), new AbstractModule() {
@Override
protected void configure() {
bind(MyDependency.class).to(CustomMyDependency.class).in(Singleton.class);
}
});

getFactory().setInjector(parent);

DeploymentOptions options = new DeploymentOptions()
.setConfig(new JsonObject()
.put(GuiceVerticleLoader.CONFIG_BOOTSTRAP_BINDER_NAME, NOPBinder.class.getName()));

deployVerticle(DependencyInjectionVerticle3.class, options).get();

vertx.eventBus()
.<String>send(DependencyInjectionVerticle3.EB_ADDRESS, null, result -> {
if (result.succeeded()) {
assertEquals(CustomMyDependency.class.getName(), result.result().body());
testComplete();
} else {
fail(result.cause());
}
});

await();
}

@Test
public void testDependencyInjection_Uncompiled() throws Exception {
deployVerticle("UncompiledDIVerticle.java").get();
Expand Down