diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java index 0d43e94389..d5b77cbc1b 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java @@ -21,7 +21,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArrayList; import com.github.benmanes.caffeine.cache.CacheLoader; import com.github.benmanes.caffeine.cache.Caffeine; @@ -56,10 +56,6 @@ import org.springframework.util.ObjectUtils; */ public class CaffeineCacheManager implements CacheManager { - private final ConcurrentMap cacheMap = new ConcurrentHashMap<>(16); - - private boolean dynamic = true; - private Caffeine cacheBuilder = Caffeine.newBuilder(); @Nullable @@ -67,6 +63,12 @@ public class CaffeineCacheManager implements CacheManager { private boolean allowNullValues = true; + private boolean dynamic = true; + + private final Map cacheMap = new ConcurrentHashMap<>(16); + + private final Collection customCacheNames = new CopyOnWriteArrayList<>(); + /** * Construct a dynamic CaffeineCacheManager, @@ -135,6 +137,13 @@ public class CaffeineCacheManager implements CacheManager { doSetCaffeine(Caffeine.from(cacheSpecification)); } + private void doSetCaffeine(Caffeine cacheBuilder) { + if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) { + this.cacheBuilder = cacheBuilder; + refreshCommonCaches(); + } + } + /** * Set the Caffeine CacheLoader to use for building each individual * {@link CaffeineCache} instance, turning it into a LoadingCache. @@ -145,7 +154,7 @@ public class CaffeineCacheManager implements CacheManager { public void setCacheLoader(CacheLoader cacheLoader) { if (!ObjectUtils.nullSafeEquals(this.cacheLoader, cacheLoader)) { this.cacheLoader = cacheLoader; - refreshKnownCaches(); + refreshCommonCaches(); } } @@ -158,7 +167,7 @@ public class CaffeineCacheManager implements CacheManager { public void setAllowNullValues(boolean allowNullValues) { if (this.allowNullValues != allowNullValues) { this.allowNullValues = allowNullValues; - refreshKnownCaches(); + refreshCommonCaches(); } } @@ -183,42 +192,76 @@ public class CaffeineCacheManager implements CacheManager { this.dynamic ? createCaffeineCache(cacheName) : null); } + /** - * Create a new CaffeineCache instance for the specified cache name. + * Register the given native Caffeine Cache instance with this cache manager, + * adapting it to Spring's cache API for exposure through {@link #getCache}. + * Any number of such custom caches may be registered side by side. + *

This allows for custom settings per cache (as opposed to all caches + * sharing the common settings in the cache manager's configuration) and + * is typically used with the Caffeine builder API: + * {@code registerCustomCache("myCache", Caffeine.newBuilder().maximumSize(10).build())} + *

Note that any other caches, whether statically specified through + * {@link #setCacheNames} or dynamically built on demand, still operate + * with the common settings in the cache manager's configuration. + * @param name the name of the cache + * @param cache the custom Caffeine Cache instance to register + * @since 5.2.8 + * @see #adaptCaffeineCache + */ + public void registerCustomCache(String name, com.github.benmanes.caffeine.cache.Cache cache) { + this.customCacheNames.add(name); + this.cacheMap.put(name, adaptCaffeineCache(name, cache)); + } + + /** + * Adapt the given new native Caffeine Cache instance to Spring's {@link Cache} + * abstraction for the specified cache name. + * @param name the name of the cache + * @param cache the native Caffeine Cache instance + * @return the Spring CaffeineCache adapter (or a decorator thereof) + * @since 5.2.8 + * @see CaffeineCache + * @see #isAllowNullValues() + */ + protected Cache adaptCaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache cache) { + return new CaffeineCache(name, cache, isAllowNullValues()); + } + + /** + * Build a common {@link CaffeineCache} instance for the specified cache name, + * using the common Caffeine configuration specified on this cache manager. + *

Delegates to {@link #adaptCaffeineCache} as the adaptation method to + * Spring's cache abstraction (allowing for centralized decoration etc), + * passing in a freshly built native Caffeine Cache instance. * @param name the name of the cache * @return the Spring CaffeineCache adapter (or a decorator thereof) + * @see #adaptCaffeineCache + * @see #createNativeCaffeineCache */ protected Cache createCaffeineCache(String name) { - return new CaffeineCache(name, createNativeCaffeineCache(name), isAllowNullValues()); + return adaptCaffeineCache(name, createNativeCaffeineCache(name)); } /** - * Create a native Caffeine Cache instance for the specified cache name. + * Build a common Caffeine Cache instance for the specified cache name, + * using the common Caffeine configuration specified on this cache manager. * @param name the name of the cache * @return the native Caffeine Cache instance + * @see #createCaffeineCache */ protected com.github.benmanes.caffeine.cache.Cache createNativeCaffeineCache(String name) { - if (this.cacheLoader != null) { - return this.cacheBuilder.build(this.cacheLoader); - } - else { - return this.cacheBuilder.build(); - } - } - - private void doSetCaffeine(Caffeine cacheBuilder) { - if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) { - this.cacheBuilder = cacheBuilder; - refreshKnownCaches(); - } + return (this.cacheLoader != null ? this.cacheBuilder.build(this.cacheLoader) : this.cacheBuilder.build()); } /** - * Create the known caches again with the current state of this manager. + * Recreate the common caches with the current state of this manager. */ - private void refreshKnownCaches() { + private void refreshCommonCaches() { for (Map.Entry entry : this.cacheMap.entrySet()) { - entry.setValue(createCaffeineCache(entry.getKey())); + if (!this.customCacheNames.contains(entry.getKey())) { + entry.setValue(createCaffeineCache(entry.getKey())); + } } } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java index f20fde56b1..a9adcc823d 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/commonj/WorkManagerTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,13 +58,13 @@ import org.springframework.util.concurrent.ListenableFutureTask; *

The CommonJ WorkManager will usually be retrieved from the application * server's JNDI environment, as defined in the server's management console. * - *

Note: On the upcoming EE 7 compliant versions of WebLogic and WebSphere, a + *

Note: On EE 7/8 compliant versions of WebLogic and WebSphere, a * {@link org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor} - * should be preferred, following JSR-236 support in Java EE 7. + * should be preferred, following JSR-236 support in Java EE 7/8. * * @author Juergen Hoeller * @since 2.0 - * @deprecated as of 5.1, in favor of EE 7's + * @deprecated as of 5.1, in favor of the EE 7/8 based * {@link org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor} */ @Deprecated @@ -121,6 +121,11 @@ public class WorkManagerTaskExecutor extends JndiLocatorSupport * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. * @since 4.3 */ public void setTaskDecorator(TaskDecorator taskDecorator) { diff --git a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java index 7637e10545..e90a3ece93 100644 --- a/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java +++ b/spring-context-support/src/test/java/org/springframework/cache/caffeine/CaffeineCacheManagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -125,7 +125,7 @@ public class CaffeineCacheManagerTests { Cache cache1x = cm.getCache("c1"); assertThat(cache1x != cache1).isTrue(); - cm.setCaffeine(caffeine); // Set same instance + cm.setCaffeine(caffeine); // Set same instance Cache cache1xx = cm.getCache("c1"); assertThat(cache1xx).isSameAs(cache1x); } @@ -155,12 +155,14 @@ public class CaffeineCacheManagerTests { CaffeineCacheManager cm = new CaffeineCacheManager("c1"); Cache cache1 = cm.getCache("c1"); - CacheLoader loader = mockCacheLoader(); + @SuppressWarnings("unchecked") + CacheLoader loader = mock(CacheLoader.class); + cm.setCacheLoader(loader); Cache cache1x = cm.getCache("c1"); assertThat(cache1x != cache1).isTrue(); - cm.setCacheLoader(loader); // Set same instance + cm.setCacheLoader(loader); // Set same instance Cache cache1xx = cm.getCache("c1"); assertThat(cache1xx).isSameAs(cache1x); } @@ -194,9 +196,19 @@ public class CaffeineCacheManagerTests { .withMessageContaining("I only know ping"); } - @SuppressWarnings("unchecked") - private CacheLoader mockCacheLoader() { - return mock(CacheLoader.class); + @Test + public void customCacheRegistration() { + CaffeineCacheManager cm = new CaffeineCacheManager("c1"); + com.github.benmanes.caffeine.cache.Cache nc = Caffeine.newBuilder().build(); + cm.registerCustomCache("c2", nc); + + Cache cache1 = cm.getCache("c1"); + Cache cache2 = cm.getCache("c2"); + assertThat(nc == cache2.getNativeCache()).isTrue(); + + cm.setCaffeine(Caffeine.newBuilder().maximumSize(10)); + assertThat(cm.getCache("c1") != cache1).isTrue(); + assertThat(cm.getCache("c2") == cache2).isTrue(); } } diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java index 876beb3d58..0b976d3f00 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ConcurrentTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -130,6 +130,11 @@ public class ConcurrentTaskExecutor implements AsyncListenableTaskExecutor, Sche * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. * @since 4.3 */ public final void setTaskDecorator(TaskDecorator taskDecorator) { diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedTaskExecutor.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedTaskExecutor.java index d4095bad58..62cb3dd9fb 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedTaskExecutor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ import org.springframework.lang.Nullable; /** * JNDI-based variant of {@link ConcurrentTaskExecutor}, performing a default lookup for - * JSR-236's "java:comp/DefaultManagedExecutorService" in a Java EE 7 environment. + * JSR-236's "java:comp/DefaultManagedExecutorService" in a Java EE 7/8 environment. * *

Note: This class is not strictly JSR-236 based; it can work with any regular * {@link java.util.concurrent.Executor} that can be found in JNDI. @@ -37,10 +37,11 @@ import org.springframework.lang.Nullable; * * @author Juergen Hoeller * @since 4.0 + * @see javax.enterprise.concurrent.ManagedExecutorService */ public class DefaultManagedTaskExecutor extends ConcurrentTaskExecutor implements InitializingBean { - private JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); + private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); @Nullable private String jndiName = "java:comp/DefaultManagedExecutorService"; diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java index a21d18eccd..f5e9edf58b 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/ThreadPoolTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -205,6 +205,13 @@ public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. + * See the {@code ThreadPoolExecutor#afterExecute} javadoc for an example + * of how to access exceptions in such a {@code Future} case. * @since 4.3 */ public void setTaskDecorator(TaskDecorator taskDecorator) { diff --git a/spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java b/spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java index 0bc6aefee5..7d96032ad8 100644 --- a/spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java +++ b/spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,6 +126,11 @@ public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. * @since 4.3 */ public final void setTaskDecorator(TaskDecorator taskDecorator) { diff --git a/spring-core/src/main/java/org/springframework/core/task/TaskDecorator.java b/spring-core/src/main/java/org/springframework/core/task/TaskDecorator.java index 0d7734d3bd..753bf2c519 100644 --- a/spring-core/src/main/java/org/springframework/core/task/TaskDecorator.java +++ b/spring-core/src/main/java/org/springframework/core/task/TaskDecorator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,17 +27,24 @@ package org.springframework.core.task; *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. * + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * may be limited. Specifically in case of a {@code Future}-based operation, + * the exposed {@code Runnable} will be a wrapper which does not propagate + * any exceptions from its {@code run} method. + * * @author Juergen Hoeller * @since 4.3 * @see TaskExecutor#execute(Runnable) * @see SimpleAsyncTaskExecutor#setTaskDecorator + * @see org.springframework.core.task.support.TaskExecutorAdapter#setTaskDecorator */ @FunctionalInterface public interface TaskDecorator { /** * Decorate the given {@code Runnable}, returning a potentially wrapped - * {@code Runnable} for actual execution. + * {@code Runnable} for actual execution, internally delegating to the + * original {@link Runnable#run()} implementation. * @param runnable the original {@code Runnable} * @return the decorated {@code Runnable} */ diff --git a/spring-core/src/main/java/org/springframework/core/task/support/TaskExecutorAdapter.java b/spring-core/src/main/java/org/springframework/core/task/support/TaskExecutorAdapter.java index 2162c7c429..81da48db8e 100644 --- a/spring-core/src/main/java/org/springframework/core/task/support/TaskExecutorAdapter.java +++ b/spring-core/src/main/java/org/springframework/core/task/support/TaskExecutorAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,6 +70,11 @@ public class TaskExecutorAdapter implements AsyncListenableTaskExecutor { * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. * @since 4.3 */ public final void setTaskDecorator(TaskDecorator taskDecorator) { diff --git a/spring-tx/src/main/java/org/springframework/jca/work/WorkManagerTaskExecutor.java b/spring-tx/src/main/java/org/springframework/jca/work/WorkManagerTaskExecutor.java index 8a687f3c89..e4fe06bfeb 100644 --- a/spring-tx/src/main/java/org/springframework/jca/work/WorkManagerTaskExecutor.java +++ b/spring-tx/src/main/java/org/springframework/jca/work/WorkManagerTaskExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -175,6 +175,11 @@ public class WorkManagerTaskExecutor extends JndiLocatorSupport * execution callback (which may be a wrapper around the user-supplied task). *

The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. + *

NOTE: Exception handling in {@code TaskDecorator} implementations + * is limited to plain {@code Runnable} execution via {@code execute} calls. + * In case of {@code #submit} calls, the exposed {@code Runnable} will be a + * {@code FutureTask} which does not propagate any exceptions; you might + * have to cast it and call {@code Future#get} to evaluate exceptions. * @since 4.3 */ public void setTaskDecorator(TaskDecorator taskDecorator) {