DATACMNS-489 - Support for Future<T> as return value wrapper for repository methods.

Added a converter to process a NullableWrapper into a Future by producing an AsyncResult. This is to support repository methods annotated with @Async and returning a Future<T>. With Spring's asynchronous method invocation support activated this will cause the repository method being executed asynchronously.

Note, that this currently only works on Spring 4 as Spring 3.2.8 async support is not discovering the introduction on the repository proxy and thus fails to discover the @Async annotation on the repository method. This will be resolved in Spring 3.2.9, see the related tickets.

Related tickets: DATACMNS-483, SPR-11725, DATACMNS-499.
This commit is contained in:
Oliver Gierke
2014-04-22 16:19:44 +02:00
parent 57271baf54
commit 02a3c08ee1
4 changed files with 82 additions and 5 deletions

View File

@@ -18,9 +18,11 @@ package org.springframework.data.repository.util;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -48,6 +50,9 @@ public class QueryExecutionConverters {
Set<Class<?>> wrapperTypes = new HashSet<Class<?>>();
Set<Converter<?, ?>> converters = new HashSet<Converter<?, ?>>();
wrapperTypes.add(Future.class);
converters.add(ObjectToFutureConverter.INSTANCE);
if (GUAVA_PRESENT) {
wrapperTypes.add(ObjectToGuavaOptionalConverter.INSTANCE.getWrapperType());
converters.add(ObjectToGuavaOptionalConverter.INSTANCE);
@@ -133,4 +138,23 @@ public class QueryExecutionConverters {
return java.util.Optional.class;
}
}
/**
* A Spring {@link Converter} to support returning {@link Future} instances from repository methods.
*
* @author Oliver Gierke
*/
private static enum ObjectToFutureConverter implements Converter<NullableWrapper, Future<Object>> {
INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public Future<Object> convert(NullableWrapper source) {
return new AsyncResult<Object>(source.getValue());
}
}
}