|
| 1 | +--- |
| 2 | +title: Vaadin Executor |
| 3 | +page-title: How to configure and use Vaadin built-in Executor |
| 4 | +description: Configuring and using Vaadin Executor to run background tasks. |
| 5 | +meta-description: Discover how to configure and use Vaadin Executor to run background tasks. |
| 6 | +order: 177 |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | += Vaadin Executor |
| 11 | + |
| 12 | +The Vaadin Executor provides a centralized mechanism for managing asynchronous tasks in Vaadin applications. |
| 13 | +It offers a configurable thread pool that can be used for executing background operations without blocking the UI thread. |
| 14 | + |
| 15 | +This feature is particularly useful for performing time-consuming operations asynchronously, executing background tasks |
| 16 | +that should not block the UI, managing concurrent operations efficiently and integrating with framework-specific task execution mechanisms, |
| 17 | +like Spring [classname]`TaskExecutor` or CDI [classname]`ManagedExecutor`. |
| 18 | + |
| 19 | +By default, Vaadin creates a thread pool executor with the following configuration: |
| 20 | + |
| 21 | +* Core pool size: 8 threads |
| 22 | +* Maximum pool size: Unbounded (Integer.MAX_VALUE) |
| 23 | +* Keep-alive time: 60 seconds for idle threads |
| 24 | +* Custom thread factory that creates daemon threads |
| 25 | +* Core threads are allowed to time out when idle |
| 26 | + |
| 27 | +This default configuration is suitable for most applications but can be customized as needed. |
| 28 | + |
| 29 | +== Accessing and Using the Executor |
| 30 | + |
| 31 | +You can access the executor service through the [classname]`VaadinService` instance: |
| 32 | + |
| 33 | +[source,java] |
| 34 | +---- |
| 35 | +VaadinService service = VaadinService.getCurrent(); |
| 36 | +Executor executor = service.getExecutor(); |
| 37 | +
|
| 38 | +// Execute a task asynchronously |
| 39 | +executor.execute(() -> { |
| 40 | + // Your background task here |
| 41 | + service.longRunningTask(); |
| 42 | +}); |
| 43 | +
|
| 44 | +// Execute a task asynchronously using CompletableFeature |
| 45 | +CompletableFuture.supplyAsync(service::longRunningTask, executor); |
| 46 | + .thenAccept(this::computeResult); |
| 47 | +---- |
| 48 | + |
| 49 | +You can use the executor service in your UI components to perform background operations and update the UI when complete. |
| 50 | + |
| 51 | +[source,java] |
| 52 | +---- |
| 53 | +Button button = new Button("Start Background Task"); |
| 54 | +button.addClickListener(event -> { |
| 55 | + UI ui = UI.getCurrent(); |
| 56 | + VaadinService.getCurrent().getExecutor().execute(() -> { |
| 57 | +
|
| 58 | + // Perform time-consuming operation |
| 59 | + service.longRunningTask(); |
| 60 | +
|
| 61 | + // Update UI from background thread |
| 62 | + ui.access(() -> { |
| 63 | + Notification.show("Background task completed!"); |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
| 67 | +---- |
| 68 | + |
| 69 | +Remember that code running in the executor is not automatically thread-safe: |
| 70 | + |
| 71 | +* Use `UI.access()` to update the UI from background threads. |
| 72 | +* Be careful with shared state and consider using thread-safe collections or synchronization. |
| 73 | +* Avoid long-running tasks that might block the thread pool. |
| 74 | + |
| 75 | +== Configuring a Custom Executor |
| 76 | + |
| 77 | +In standard Java applications, you can customize the executor by registering a <<service-init-listener#,VaadinServiceInitListener>> and providing your own executor implementation: |
| 78 | + |
| 79 | +.`CustomExecutorServiceInitListener.java` |
| 80 | +[source,java] |
| 81 | +---- |
| 82 | +package com.example; |
| 83 | +import java.util.concurrent.LinkedBlockingQueue; |
| 84 | +import java.util.concurrent.ThreadPoolExecutor; |
| 85 | +import java.util.concurrent.TimeUnit; |
| 86 | +
|
| 87 | +import com.vaadin.flow.server.ServiceInitEvent; |
| 88 | +import com.vaadin.flow.server.VaadinServiceInitListener; |
| 89 | +
|
| 90 | +public class CustomExecutorServiceInitListener implements VaadinServiceInitListener { |
| 91 | + @Override |
| 92 | + public void serviceInit(ServiceInitEvent event) { |
| 93 | + ThreadPoolExecutor customExecutor = new ThreadPoolExecutor( |
| 94 | + 16, // Core pool size |
| 95 | + 32, // Maximum pool size |
| 96 | + 120, // Keep-alive time |
| 97 | + TimeUnit.SECONDS, |
| 98 | + new LinkedBlockingQueue<>(), |
| 99 | + r -> { |
| 100 | + Thread thread = new Thread(r, "CustomVaadinExecutor-" + r.hashCode()); |
| 101 | + thread.setDaemon(true); |
| 102 | + return thread; |
| 103 | + }); |
| 104 | +
|
| 105 | + // Allow core threads to time out |
| 106 | + customExecutor.allowCoreThreadTimeOut(true); |
| 107 | +
|
| 108 | + // Set the custom executor |
| 109 | + event.setExecutor(customExecutor); |
| 110 | + } |
| 111 | +} |
| 112 | +---- |
| 113 | + |
| 114 | +Register your listener using Java's `ServiceLoader` mechanism by creating a file at |
| 115 | +`META-INF/services/com.vaadin.flow.server.VaadinServiceInitListener`. |
| 116 | +The file should contain the fully qualified name of your implementation class: |
| 117 | +`com.example.CustomExecutorServiceInitListener` |
| 118 | + |
| 119 | +When configuring a custom Thread Pool, consider the following best practices: |
| 120 | + |
| 121 | +* *Core Pool Size*: Set based on the number of concurrent tasks your application typically handles. A good starting point is the number of CPU cores. |
| 122 | +* *Maximum Pool Size*: Set to a reasonable upper limit to prevent resource exhaustion. Consider your server's memory and CPU constraints. |
| 123 | +* *Queue Capacity*: Use a bounded queue to prevent memory issues when task submission rate exceeds execution rate. |
| 124 | +* Use descriptive thread names to make debugging easier. |
| 125 | +* Make threads daemon threads (`Thread.setDaemon(true)`) to prevent them from blocking JVM shutdown. |
| 126 | +* Allow core threads to time out if your application has periods of inactivity |
| 127 | + |
| 128 | +For framework integrations, consult the specific documentation pages for <<../integrations/spring/configuration#configure-custom-vaadin-executor,Spring>>, <<../integrations/cdi/service-beans#configure-custom-vaadin-executor,CDI>> and <<../integrations/quarkus#configure-custom-vaadin-executor,Quarkus>>. |
| 129 | + |
| 130 | + |
| 131 | +[discussion-id]`2EF7B593-E426-479E-89D7-E62667D316C2` |
0 commit comments