Code Monkey home page Code Monkey logo

Comments (1)

drejc avatar drejc commented on May 20, 2024

The difference between the Future<> and "normal" execution is in the way vert.x is thread pool is used.

In the "normal" way vert.x is called:

context -> context.vertx().executeBlocking(
			// code execution,
			false, // not ordered
			// produce response)

As per vertx documentation:

By default, if executeBlocking is called several times from the same context (e.g. the same verticle instance) then the different executeBlocking are executed serially (i.e. one after another).
If you don’t care about ordering you can call executeBlocking specifying false as the argument to ordered. In this case any executeBlocking may be executed in parallel on the worker pool.
An alternative way to run blocking code is to use a worker verticle
A worker verticle is always executed with a thread from the worker pool.
By default blocking code is executed on the Vert.x worker pool, configured with setWorkerPoolSize.

in the "future" way vert.x is called:

return context -> {		
				Future result = method.invoke(toInvoke, args);
					result.setHandler(handler -> {
// produce response
})

Meaning you have control over the thread pool execution part and executor used.

So you can manually create an execution pool of your own ... for instance:

WorkerExecutor executor = vertx.createSharedWorkerExecutor("myExecutor", 10);   

and in your service you use the executor:

@GET
	@Path("executor")
	@Produces(MediaType.APPLICATION_JSON)
	public Future<Dummy> executor(@Context Vertx vertx) throws InterruptedException {
		Future<Dummy> res = Future.future();
		service.asyncExecutor(executor, res);
		System.out.println("Rest finished");
		return res;
	}

Where:

public void asyncExecutor(WorkerExecutor executor, Future<Dummy> value) {
		executor.executeBlocking(fut -> {
			                         System.out.println("Process started!");
			                        // do something
			                         fut.complete();
		                         },
		                         false,
		                         fut -> {
                                              System.out.println("Process finished!");
		                         });
	}

In this case console output would be:

Rest finished
Process started!
Process finished!

It might be useful to create a wrapper to hide the Future<> implementation and provide a way to create a thread execution pool via annotations ... not sure if this is a good idea.

To get back to your question ... in my book I would avoid using the Future<> type implementation and configure vert.x pool to handle the appropriate load.
If you really need control over the WorkerPool then go with the Future<> type

Hope this gives a clearer picture ... will try to improve the documentation on this part as it is a little vague.

PS: I'm in the process of implementing an Event type system to send messages to the vert.x EventBus for long operations or other non critical async operations. This is very useful in case you need to do something but don't want to block the REST thread ... for instance count how many times an API has been invoked, log something ... or just start a long operation and immediately return a response, ... so stay tuned for that.

from rest.vertx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.