Skip to content

Commit cc4cddc

Browse files
codingxu97DavideD
authored andcommitted
[hibernate#1001] Don't create a new Vert.x instance is one exists
DefaultVertxInstance used to create a new Vert.x instance every time, even if one was already present. This commit make it easier to use Hibernate Reactive when a Vert.x instance already exists. In particular, when used with Vert.x verticles.
1 parent d8098d1 commit cc4cddc

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/vertx/impl/DefaultVertxInstance.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.lang.invoke.MethodHandles;
99

10+
import io.vertx.core.Context;
1011
import io.vertx.core.Vertx;
1112

1213
import org.hibernate.reactive.logging.impl.Log;
@@ -29,6 +30,7 @@ public final class DefaultVertxInstance implements VertxInstance, Stoppable, Sta
2930
private static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() );
3031

3132
private Vertx vertx;
33+
private boolean vertxCreator;
3234

3335
@Override
3436
public Vertx getVertx() {
@@ -40,14 +42,18 @@ public Vertx getVertx() {
4042

4143
@Override
4244
public void stop() {
43-
if ( vertx != null ) {
45+
if ( vertxCreator && vertx != null ) {
4446
vertx.close().toCompletionStage().toCompletableFuture().join();
4547
}
4648
}
4749

4850
@Override
4951
public void start() {
50-
vertx = Vertx.vertx();
52+
final Context context = Vertx.currentContext();
53+
vertxCreator = context == null || context.owner() == null;
54+
vertx = vertxCreator
55+
? Vertx.vertx() // Create a new one
56+
: context.owner(); // Get the existing one
5157
}
5258

5359
}

0 commit comments

Comments
 (0)