Merge branch '5.2.x'

This commit is contained in:
Juergen Hoeller 2020-06-10 22:53:06 +02:00
commit 663f2e8afd
10 changed files with 142 additions and 47 deletions

View File

@ -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<String, Cache> cacheMap = new ConcurrentHashMap<>(16);
private boolean dynamic = true;
private Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
@Nullable
@ -67,6 +63,12 @@ public class CaffeineCacheManager implements CacheManager {
private boolean allowNullValues = true;
private boolean dynamic = true;
private final Map<String, Cache> cacheMap = new ConcurrentHashMap<>(16);
private final Collection<String> customCacheNames = new CopyOnWriteArrayList<>();
/**
* Construct a dynamic CaffeineCacheManager,
@ -135,6 +137,13 @@ public class CaffeineCacheManager implements CacheManager {
doSetCaffeine(Caffeine.from(cacheSpecification));
}
private void doSetCaffeine(Caffeine<Object, Object> 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<Object, Object> 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.
* <p>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())}
* <p>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<Object, Object> 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<Object, Object> 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.
* <p>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<Object, Object> createNativeCaffeineCache(String name) {
if (this.cacheLoader != null) {
return this.cacheBuilder.build(this.cacheLoader);
}
else {
return this.cacheBuilder.build();
}
}
private void doSetCaffeine(Caffeine<Object, Object> 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<String, Cache> entry : this.cacheMap.entrySet()) {
entry.setValue(createCaffeineCache(entry.getKey()));
if (!this.customCacheNames.contains(entry.getKey())) {
entry.setValue(createCaffeineCache(entry.getKey()));
}
}
}

View File

@ -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;
* <p>The CommonJ WorkManager will usually be retrieved from the application
* server's JNDI environment, as defined in the server's management console.
*
* <p>Note: On the upcoming EE 7 compliant versions of WebLogic and WebSphere, a
* <p>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).
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
* <p><b>NOTE:</b> 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) {

View File

@ -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<Object, Object> loader = mockCacheLoader();
@SuppressWarnings("unchecked")
CacheLoader<Object, Object> 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<Object, Object> mockCacheLoader() {
return mock(CacheLoader.class);
@Test
public void customCacheRegistration() {
CaffeineCacheManager cm = new CaffeineCacheManager("c1");
com.github.benmanes.caffeine.cache.Cache<Object, Object> 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();
}
}

View File

@ -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).
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
* <p><b>NOTE:</b> 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) {

View File

@ -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.
*
* <p>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";

View File

@ -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).
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
* <p><b>NOTE:</b> 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) {

View File

@ -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).
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
* <p><b>NOTE:</b> 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) {

View File

@ -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;
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
*
* <p><b>NOTE:</b> 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}
*/

View File

@ -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).
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
* <p><b>NOTE:</b> 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) {

View File

@ -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).
* <p>The primary use case is to set some execution context around the task's
* invocation, or to provide some monitoring/statistics for task execution.
* <p><b>NOTE:</b> 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) {