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 reuse current eventLoop for Deployment #5353

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions vertx-core/src/main/java/io/vertx/core/ThreadingModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public enum ThreadingModel {
*/
EVENT_LOOP,

/**
* Tasks are scheduled on the current event-loop thread.
*/
CURRENT_EVENT_LOOP,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current event loop is not a threading model

/**
* Tasks are scheduled on a worker pool of platform threads managed by the vertx instance.
*/
Expand Down
1 change: 1 addition & 0 deletions vertx-core/src/main/java/io/vertx/core/impl/VertxImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ public ContextImpl createContext(ThreadingModel threadingModel,
WorkerPool wp;
switch (threadingModel) {
case EVENT_LOOP:
case CURRENT_EVENT_LOOP:
wp = workerPool != null ? workerPool : this.workerPool;
eventExecutor = eventLoopExecutor;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public Future<?> deploy(DeploymentContext deployment) {
context = vertx.createVirtualThreadContext(deployment, closeFuture, workerLoop, tccl);
}
break;
case CURRENT_EVENT_LOOP:
context = vertx.createContext(ThreadingModel.CURRENT_EVENT_LOOP, vertx.getOrCreateContext().nettyEventLoop(),closeFuture, workerPool, deployment, tccl);
break;
default:
context = vertx.createEventLoopContext(deployment, closeFuture, workerPool, tccl);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.vertx.tests.deployment;

import io.netty.channel.EventLoop;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Promise;
import io.vertx.core.ThreadingModel;
import io.vertx.core.internal.ContextInternal;
import org.junit.Test;

/**
* @author <a href="https://dreamlike-ocean.github.io/blog/">dremalike</a>
*/
public class CurrentEventLoopDeploymentTest extends AbstractVerticleTest {

@Test
public void testDeploy() throws InterruptedException {
waitFor(1);
ContextInternal currentContext = (ContextInternal) vertx.getOrCreateContext();
EventLoop targetEventLoop = currentContext.nettyEventLoop();
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() {
EventLoop currentEventLoop = ((ContextInternal) context).nettyEventLoop();
assertEquals(targetEventLoop, currentEventLoop);
assertNotSame(currentContext, context);
}

}, new DeploymentOptions().setThreadingModel(ThreadingModel.CURRENT_EVENT_LOOP))
.onSuccess(this::assertNotNull)
.onFailure(this::fail)
.onComplete(s -> testComplete());

await();
}

@Test
public void testExecuteBlocking() {
waitFor(1);
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start(Promise<Void> startPromise) throws Exception {
Thread eventLoopThread = Thread.currentThread();
vertx.executeBlocking(() -> {
assertNotSame(eventLoopThread, Thread.currentThread());
startPromise.complete();
return null;
});
}
}, new DeploymentOptions().setThreadingModel(ThreadingModel.CURRENT_EVENT_LOOP))
.onSuccess(this::assertNotNull)
.onFailure(this::fail)
.onComplete(s -> testComplete());
await();
}


}