- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个简单的 Spring Boot 程序并练习 @Async。我想获取 teacher
的所有记录实体异步。
我的数据库访问对象:
public interface TeacherService {
List<Teacher> findAll(Pageable pageable);
}
它是如何实现的:
public class TeacherServiceImpl implements TeacherService{
@Autowired
private TeacherRepository repository;
@Override
public List<Teacher> findAll(Pageable pageable) {
Page<Teacher> page = repository.findAll(pageable);
return page.get().collect(Collectors.toList());
}
}
我的异步方法:
@Component
public class PrintAllTeacherDataBaseResult {
@Autowired
TeacherService teacher;
@Async
public AsyncResult<List<Teacher>> printPage(Pageable page){
return new AsyncResult<>(teacher.findAll(page));
}
}
当我打电话时
printPage
没有
@Async
的方法注释它工作正常。但是当我使用它时会抛出以下异常:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) [spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) [spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) [spring-boot-2.4.2.jar:2.4.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) [spring-boot-2.4.2.jar:2.4.2]
at com.hatef.demo.DemoApplication.main(DemoApplication.java:97) [classes/:na]
Caused by: java.lang.ClassCastException: org.springframework.util.concurrent.ListenableFutureTask cannot be cast to org.springframework.scheduling.annotation.AsyncResult
at async.PrintAllTeacherDataBaseResult$$EnhancerBySpringCGLIB$$ccbb6656.printPage(<generated>) ~[classes/:na]
at com.hatef.demo.DemoApplication.run(DemoApplication.java:103) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) [spring-boot-2.4.2.jar:2.4.2]
... 5 common frames omitted
java.lang.IllegalStateException: EntityManagerFactory is closed
at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:513) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.internal.SessionFactoryImpl.getMetamodel(SessionFactoryImpl.java:660) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findEntityPersisterByName(SessionFactoryHelper.java:141) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:167) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:77) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:333) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3758) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3647) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:732) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:588) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:325) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:273) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:779) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:314) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:165) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:742) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:113) ~[hibernate-core-5.4.27.Final.jar:5.4.27.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311) ~[spring-orm-5.3.3.jar:5.3.3]
at com.sun.proxy.$Proxy101.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:703) ~[spring-data-jpa-2.4.3.jar:2.4.3]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:654) ~[spring-data-jpa-2.4.3.jar:2.4.3]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:444) ~[spring-data-jpa-2.4.3.jar:2.4.3]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:411) ~[spring-data-jpa-2.4.3.jar:2.4.3]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:524) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:531) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:156) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.4.3.jar:2.4.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.3.jar:5.3.3]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.3.jar:5.3.3]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) [spring-tx-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) [spring-tx-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) [spring-data-jpa-2.4.3.jar:2.4.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) [spring-aop-5.3.3.jar:5.3.3]
at com.sun.proxy.$Proxy105.findAll(Unknown Source) [na:na]
at services.TeacherServiceImpl.findAll(TeacherServiceImpl.java:33) [classes/:na]
at async.PrintAllTeacherDataBaseResult.printPage(PrintAllTeacherDataBaseResult.java:23) [classes/:na]
at async.PrintAllTeacherDataBaseResult$$FastClassBySpringCGLIB$$ccfcfab4.invoke(<generated>) [classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) [spring-core-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) [spring-aop-5.3.3.jar:5.3.3]
at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) [spring-aop-5.3.3.jar:5.3.3]
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) ~[na:1.8.0_151]
at java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_151]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_151]
以及我如何称呼它:
@Autowired
ApplicationContextProvider applicationContextProvider;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String...args) throws Exception {
PrintAllTeacherDataBaseResult p = applicationContextProvider.getApplicationContext().getBean(PrintAllTeacherDataBaseResult.class);
AsyncResult<List<Teacher>> a = p.printPage(PageRequest.of(0, 2));
a.get().stream().forEach(System.out::println);
}
但我的问题是为什么它会抛出异常。
@EnableAsync
至
@EnableAsync(mode = AdviceMode.ASPECTJ)
一切正常!
但它没有创建新线程
@EnableAsync
时会抛出异常以及为什么它在
@EnableAsync(mode = AdviceMode.ASPECTJ)
时有效
@EnableAsync(mode = AdviceMode.ASPECTJ)
至
@EnableAsync
进而:
public interface TeacherService {
@Async
Future<List<Teacher>> findAll(Pageable pageable);
}
以及它是如何实现的:
public class TeacherServiceImpl implements TeacherService{
@Autowired
private TeacherRepository repository;
@Override
public Future<List<Teacher>> findAll(Pageable pageable) {
Page<Teacher> page = repository.findAll(pageable);
return new AsyncResult<>(page.get().collect(Collectors.toList()));
}
}
以及我如何运行上述方法:
@Autowired
ApplicationContextProvider applicationContextProvider;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String...args) throws Exception {
PrintAllTeacherDataBaseResult p = applicationContextProvider.getApplicationContext().getBean(PrintAllTeacherDataBaseResult.class);
Future<List<Teacher>> a = p.printPage(PageRequest.of(0, 2));
a.get().forEach(System.out::println);
}
我感谢任何能够准确说明正在发生的事情的帮助。
最佳答案
你应该看看这篇文章https://www.baeldung.com/spring-async
首先,返回类型的异步方法应该包含在 Future 中。
每次在特征对象上调用 get() 时,您都会订阅结果,当前线程将等待直到计算结果。
更新:
首先尝试阅读 AsyncExecutionInterceptor 和 Async 的 javadoc。
<p>In terms of target method signatures, any parameter types are supported.
However, the return type is constrained to either {@code void} or
{@link java.util.concurrent.Future}. In the latter case, you may declare the
more specific {@link org.springframework.util.concurrent.ListenableFuture} or
{@link java.util.concurrent.CompletableFuture} types which allow for richer
interaction with the asynchronous task and for immediate composition with
further processing steps.
它是一个 API,你应该遵循它。
@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;
}
}
你进入了第二个 else 语句,后来它提供了 ClassCastException (你可以更深入地调试它)。
Future<List<Teacher>> a = p.printPage(PageRequest.of(0, 2));
实际上你没有调用printPage,你只是创建了一个future,一旦你开始执行它就会在里面调用它。
关于java - 为什么我无法在@Async 方法中取回结果? ("IllegalStateException: EntityManagerFactory is closed"被抛出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66005908/
如何处理在 EditText 上键入时按返回键的事件?当显示虚拟键盘并且用户按回时,它会被隐藏。我想处理这个事件,但是在 EditText 中设置 OnKeyListener 并没有帮助。 最佳答案
我编写了 SMS 应用程序,并为其编写了 BroadcastReceiver。我想从 BroadcastReceiver 获取数据到我的 Activity 中,那么我该如何获取呢。 我的广播接收器代码
有一个场景,其中 httpentity 在 InputStream 中有图像的二进制数据,为了进一步处理,它被转换为库文件中的字符串 [String str = EntityUtils.toStrin
如何获取 uiwebview 的返回和转发 URL。我想要 URL,以便我可以相应地启用和禁用我的网络浏览器上的后退和前进按钮 谢谢 最佳答案 UIWebView 包含后退或前进的构建方法 只需使用执
CGRect rect = Temp_3_Artwork_Big_Image.frame; NSLog(@"Rect : %.2f,%.2f,%.2f,%.2f",rect.origin.x,rect
在返回旧项目并更新其依赖项后,我必须意识到,自版本 1.1.5 以来,logback 不再将 MDC 传播给子项:https://github.com/qos-ch/logback/commit/aa
holder.js 我想向我的页面动态添加占位符图像。 这样插入是行不通的: $('',{class:'file-item'}) .append($('',{'data-src':'holde
我在 C# 中使用 ExecuteOracleNonQuery 来使用存储过程将记录插入到我的 Oracle 数据库中,但似乎无法返回 ROWID。 在 C# 中 ... using (OracleC
我正在努力将一条记录插入到 postgresql 中: val res = DB.withConnection { implicit con => SQL(
我是新手,正在尝试使用 React 和 Redux 构建一个简单的书签应用程序。 我无法解决这个问题: 用户可以创建一个书签并将其添加到多个文件夹。所以我发送一个 addMark(bookmark)
我有一个像下面这样的网址 /pages/edit_product/11 在我的行动 edit_product我怎样才能得到 id 11 这样我就可以做@p = Product.find_by_id(1
我在php中有一个外部文件,输出为json格式 我希望数据以数组的形式发送回 jquery ....该怎么做? php: $options = "data 0 data 1 data 2 data 3
我有以下 C++ 代码: __declspec(dllimport) char* get_mac() { size_t byteToAlloc = 17; char *mac_addr
我正在编写一个 Azure 函数,该函数从 Microsoft 获取 OAuth token ,我已经成功获取了该 token 。我正在尝试使用该 token 访问 Microsoft Graph。我
我有一个 Python Google App Engine 应用程序,我想在 Emacs 中的开发服务器上进行调试。我创建了一个 pdb 可执行文件,以便在 Emacs 中进行调试: $ which
我正在开发一个 google chrome 扩展程序,需要在其中对 Twitch 上的用户进行身份验证。根据https://github.com/justintv/Twitch-API/blob/ma
我有一个简单的 Web API,它返回一个 Iobservable。我正在使用 HttpClient 获取 Observable,以便我可以订阅它。我的问题是订阅时返回的 Iobservable 发出
我正在使用 Ionic 2 构建 Web 应用程序的移动版本,该应用程序使用 SAML 进行 SSO,在我的客户端服务器上运行。现在我们有一个 api,当您未登录网站时会调用它,该网站会重定向到他们的
几个月前,我创建了一个使用 oauth2 与谷歌进行身份验证的 rails 应用程序 - 特别是 omniauth-google-oauth2 gem。我已经完成了创建身份验证和存储刷新 token
我正在尝试将一个值作为 JSON 从我的后端传递到我的应用程序的前端。我目前正在运行 express.js 并且所有发布方法的连接都是完美的。 在我的应用程序的前端单击按钮后,我想从我的服务器取回发票
我是一名优秀的程序员,十分优秀!