|
| 1 | +/* |
| 2 | + * Copyright © 2026 ObjectBox Ltd. <https://objectbox.io> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.objectbox; |
| 18 | + |
| 19 | +import java.util.concurrent.BlockingQueue; |
| 20 | +import java.util.concurrent.RejectedExecutionHandler; |
| 21 | +import java.util.concurrent.ThreadFactory; |
| 22 | +import java.util.concurrent.ThreadPoolExecutor; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +/** |
| 26 | + * A {@link ThreadPoolExecutor} that automatically releases thread-local ObjectBox resources after each task execution |
| 27 | + * by calling {@link BoxStore#closeThreadResources()}. |
| 28 | + * <p> |
| 29 | + * This is useful when using a thread pool with ObjectBox to ensure that thread-local resources (currently readers only) |
| 30 | + * are properly cleaned up after each task completes. |
| 31 | + * <p> |
| 32 | + * <b>Recommended:</b> Use the factory methods {@link BoxStore#newFixedThreadPoolExecutor(int)} or |
| 33 | + * {@link BoxStore#newCachedThreadPoolExecutor()} to create instances of this executor. |
| 34 | + * <p> |
| 35 | + * Example usage: |
| 36 | + * <pre> |
| 37 | + * BoxStore boxStore = MyObjectBox.builder().build(); |
| 38 | + * |
| 39 | + * // Recommended: Use BoxStore factory methods |
| 40 | + * ObjectBoxThreadPoolExecutor executor = boxStore.newFixedThreadPoolExecutor(4); |
| 41 | + * |
| 42 | + * // Or for a cached thread pool |
| 43 | + * ObjectBoxThreadPoolExecutor cachedExecutor = boxStore.newCachedThreadPoolExecutor(); |
| 44 | + * |
| 45 | + * // Advanced: Direct construction for custom configuration |
| 46 | + * ObjectBoxThreadPoolExecutor customExecutor = new ObjectBoxThreadPoolExecutor( |
| 47 | + * boxStore, |
| 48 | + * 4, // core pool size |
| 49 | + * 8, // maximum pool size |
| 50 | + * 60L, TimeUnit.SECONDS, // keep-alive time |
| 51 | + * new LinkedBlockingQueue<>() |
| 52 | + * ); |
| 53 | + * </pre> |
| 54 | + */ |
| 55 | +public class ObjectBoxThreadPoolExecutor extends ThreadPoolExecutor { |
| 56 | + |
| 57 | + private final BoxStore boxStore; |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates a new ObjectBoxThreadPoolExecutor with the given parameters. |
| 61 | + * <p> |
| 62 | + * See {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)} for parameter |
| 63 | + * details. |
| 64 | + * |
| 65 | + * @param boxStore the BoxStore instance for which to close thread resources |
| 66 | + */ |
| 67 | + public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, |
| 68 | + long keepAliveTime, TimeUnit unit, |
| 69 | + BlockingQueue<Runnable> workQueue) { |
| 70 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); |
| 71 | + this.boxStore = boxStore; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Creates a new ObjectBoxThreadPoolExecutor with the given parameters. |
| 76 | + * <p> |
| 77 | + * See {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, ThreadFactory)} for |
| 78 | + * parameter details. |
| 79 | + * |
| 80 | + * @param boxStore the BoxStore instance for which to close thread resources |
| 81 | + */ |
| 82 | + public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, |
| 83 | + long keepAliveTime, TimeUnit unit, |
| 84 | + BlockingQueue<Runnable> workQueue, |
| 85 | + ThreadFactory threadFactory) { |
| 86 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); |
| 87 | + this.boxStore = boxStore; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates a new ObjectBoxThreadPoolExecutor with the given parameters. |
| 92 | + * <p> |
| 93 | + * See |
| 94 | + * {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, RejectedExecutionHandler)} |
| 95 | + * for parameter details. |
| 96 | + * |
| 97 | + * @param boxStore the BoxStore instance for which to close thread resources |
| 98 | + */ |
| 99 | + public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, |
| 100 | + long keepAliveTime, TimeUnit unit, |
| 101 | + BlockingQueue<Runnable> workQueue, |
| 102 | + RejectedExecutionHandler handler) { |
| 103 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); |
| 104 | + this.boxStore = boxStore; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Creates a new ObjectBoxThreadPoolExecutor with the given parameters. |
| 109 | + * <p> |
| 110 | + * See |
| 111 | + * {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, ThreadFactory, |
| 112 | + * RejectedExecutionHandler)} for parameter details. |
| 113 | + * |
| 114 | + * @param boxStore the BoxStore instance for which to close thread resources |
| 115 | + */ |
| 116 | + public ObjectBoxThreadPoolExecutor(BoxStore boxStore, int corePoolSize, int maximumPoolSize, |
| 117 | + long keepAliveTime, TimeUnit unit, |
| 118 | + BlockingQueue<Runnable> workQueue, |
| 119 | + ThreadFactory threadFactory, |
| 120 | + RejectedExecutionHandler handler) { |
| 121 | + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); |
| 122 | + this.boxStore = boxStore; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Releases thread-local ObjectBox resources after each task execution. |
| 127 | + */ |
| 128 | + @Override |
| 129 | + protected void afterExecute(Runnable runnable, Throwable throwable) { |
| 130 | + super.afterExecute(runnable, throwable); |
| 131 | + boxStore.closeThreadResources(); |
| 132 | + } |
| 133 | +} |
0 commit comments