List of usage examples for org.springframework.core.task AsyncTaskExecutor submit
<T> Future<T> submit(Callable<T> task);
From source file:org.grails.plugin.platform.events.publisher.DefaultEventsPublisher.java
@SuppressWarnings("rawtypes") @Override/* w ww . ja va2s . c o m*/ public EventReply eventAsync(final EventMessage<?> event, final Map<String, Object> params) { AsyncTaskExecutor taskExecutor = params != null && params.containsKey(EXECUTOR) ? taskExecutors.get(params.get(EXECUTOR)) : taskExecutors.get(DEFAULT_EXECUTOR); Future<DefaultEventsRegistry.InvokeResult> invokeResult = taskExecutor.submit(new Callback(event)); final WrappedFuture reply = new WrappedFuture(invokeResult, -1); if (params != null) { reply.setOnError((Closure) params.get(ON_ERROR)); if (params.get(ON_REPLY) != null) { taskExecutor.execute(new Runnable() { @Override public void run() { try { if (params.get(TIMEOUT) != null) reply.get((Long) params.get(TIMEOUT), TimeUnit.MILLISECONDS); else reply.get(); reply.throwError(); ((Closure) params.get(ON_REPLY)).call(reply); } catch (Throwable e) { reply.setCallingError(e); } } }); } } return reply; }
From source file:com.crossbusiness.resiliency.aspect.AbstractAsyncAspect.java
/** * Intercept the given method invocation, submit the actual calling of the method to * the correct task executor and return immediately to the caller. * @return {@link Future} if the original method returns {@code Future}; {@code null} * otherwise.//from ww w.j a va2 s .c o m */ // @Around("asyncMethodExecution()") private Object doAsync(final ProceedingJoinPoint point, Async asyncConfig) throws Throwable { log.debug(point + " -> " + asyncConfig); String qualifier = asyncConfig.value(); MethodSignature targetMethodSig = (MethodSignature) point.getSignature(); AsyncTaskExecutor executor = getExecutor(targetMethodSig.getMethod(), qualifier); if (executor == null) { return point.proceed(); } Callable<Object> callable = new Callable<Object>() { public Object call() throws Exception { try { Object result = point.proceed(); if (result instanceof Future) { return ((Future<?>) result).get(); } } catch (Throwable ex) { ReflectionUtils.rethrowException(ex); } return null; } }; Future<?> result = executor.submit(callable); if (Future.class.isAssignableFrom(targetMethodSig.getMethod().getReturnType())) { return result; } else { return null; } }
From source file:org.springframework.aop.interceptor.AsyncExecutionAspectSupport.java
/** * Delegate for actually executing the given task with the chosen executor. * @param task the task to execute/* w ww. ja va 2 s.c o m*/ * @param executor the chosen executor * @param returnType the declared return type (potentially a {@link Future} variant) * @return the execution result (potentially a corresponding {@link Future} handle) */ @Nullable protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) { if (CompletableFuture.class.isAssignableFrom(returnType)) { return CompletableFuture.supplyAsync(() -> { try { return task.call(); } catch (Throwable ex) { throw new CompletionException(ex); } }, executor); } else if (ListenableFuture.class.isAssignableFrom(returnType)) { return ((AsyncListenableTaskExecutor) executor).submitListenable(task); } else if (Future.class.isAssignableFrom(returnType)) { return executor.submit(task); } else { executor.submit(task); return null; } }
From source file:org.springframework.aop.interceptor.AsyncExecutionInterceptor.java
/** * Intercept the given method invocation, submit the actual calling of the method to * the correct task executor and return immediately to the caller. * @param invocation the method to intercept and make asynchronous * @return {@link Future} if the original method returns {@code Future}; {@code null} * otherwise.//from w w w . j a va 2 s. c om */ @Override public Object invoke(final MethodInvocation invocation) throws Throwable { Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null); Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass); final Method userDeclaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod); AsyncTaskExecutor executor = determineAsyncExecutor(userDeclaredMethod); if (executor == null) { throw new IllegalStateException( "No executor specified and no default executor set on AsyncExecutionInterceptor either"); } Callable<Object> task = new Callable<Object>() { @Override public Object call() throws Exception { try { Object result = invocation.proceed(); if (result instanceof Future) { return ((Future<?>) result).get(); } } catch (Throwable ex) { handleError(ex, userDeclaredMethod, invocation.getArguments()); } return null; } }; Class<?> returnType = invocation.getMethod().getReturnType(); if (ListenableFuture.class.isAssignableFrom(returnType)) { return ((AsyncListenableTaskExecutor) executor).submitListenable(task); } else if (Future.class.isAssignableFrom(returnType)) { return executor.submit(task); } else { executor.submit(task); return null; } }
From source file:org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.java
@Override public void afterPropertiesSet() throws PersistenceException { JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter(); if (jpaVendorAdapter != null) { if (this.persistenceProvider == null) { this.persistenceProvider = jpaVendorAdapter.getPersistenceProvider(); }//from w w w .ja va 2 s. co m PersistenceUnitInfo pui = getPersistenceUnitInfo(); Map<String, ?> vendorPropertyMap = (pui != null ? jpaVendorAdapter.getJpaPropertyMap(pui) : jpaVendorAdapter.getJpaPropertyMap()); if (!CollectionUtils.isEmpty(vendorPropertyMap)) { vendorPropertyMap.forEach((key, value) -> { if (!this.jpaPropertyMap.containsKey(key)) { this.jpaPropertyMap.put(key, value); } }); } if (this.entityManagerFactoryInterface == null) { this.entityManagerFactoryInterface = jpaVendorAdapter.getEntityManagerFactoryInterface(); if (!ClassUtils.isVisible(this.entityManagerFactoryInterface, this.beanClassLoader)) { this.entityManagerFactoryInterface = EntityManagerFactory.class; } } if (this.entityManagerInterface == null) { this.entityManagerInterface = jpaVendorAdapter.getEntityManagerInterface(); if (!ClassUtils.isVisible(this.entityManagerInterface, this.beanClassLoader)) { this.entityManagerInterface = EntityManager.class; } } if (this.jpaDialect == null) { this.jpaDialect = jpaVendorAdapter.getJpaDialect(); } } AsyncTaskExecutor bootstrapExecutor = getBootstrapExecutor(); if (bootstrapExecutor != null) { this.nativeEntityManagerFactoryFuture = bootstrapExecutor.submit(this::buildNativeEntityManagerFactory); } else { this.nativeEntityManagerFactory = buildNativeEntityManagerFactory(); } // Wrap the EntityManagerFactory in a factory implementing all its interfaces. // This allows interception of createEntityManager methods to return an // application-managed EntityManager proxy that automatically joins // existing transactions. this.entityManagerFactory = createEntityManagerFactoryProxy(this.nativeEntityManagerFactory); }