gpt4 book ai didi

asynchronous - EJB 异步线程与 Vaadin,注入(inject)不工作(Vaadin 8、Vaadin-CDI、EJB ManagedExecutorService 线程)

转载 作者:行者123 更新时间:2023-12-02 02:59:09 25 4
gpt4 key购买 nike

我是 Vaadin 框架和 Java EE 的新手。我正在使用 Vaadin Framework 8.1.6,Vaadin-CDI 插件 2.0.0,在 TomEE 7.0.2 服务器上运行。我正在尝试使用 EJB 托管线程池 (ManagedExecutorService) 进行异步/后台数据加载。我在使用一些注入(inject)的 bean 时遇到问题。

所有注入(inject)的 bean 在单线程 UI 中都运行良好。当我想在后台线程中使用一些注入(inject)的bean时,其中一些无法访问。

Vaadin-CDI 用户界面

@CDIUI("")
@Theme("myuitheme")
@Push
public class MyUI extends UI {

// ...

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinCDIServlet {
}
}

数据服务

@NormalUIScoped
public class DataService {

// consts, functions, ...

public Data getData(String param) {
// call REST service, load, decode and aggregate data
// ...
}
}

我的 View

@CDIView(value=MyView.VIEWNAME, supportsParameters=true)
public class MyView extends Panel implements View {

public final static String VIEWNAME = "main";

@Resource
private ManagedExecutorService executor;

@Inject
private MyUI ui;

@Inject
private DataService dataService;

public void fun1() {
System.out.println("fun1, thread_id: " + Thread.currentThread().getId());
Future<Data> dataFuture = executor.submit(() -> loadData(param));
// ...
}

private Data loadData(String param) {

// launched in executor thread
System.out.println("loadData, thread_id: " + Thread.currentThread().getId());

// accessing injected ui works fine, the ui is updated correctly
ui.access(() -> {
clearMyView();
}

// accessing injected dataService doesn't work
// exception: java.lang.IllegalStateException
Data data = dataService.getData(param);

ui.access(() -> {
updateMyView(data);
}

return data;
}

//...

}

日志

fun1, thread_id: 77

loadData, thread_id: 102

异常

java.lang.IllegalStateException: Session data not recoverable for DataService, WebBeansType:MANAGED, Name:null, API Types:[xxx.DataService,java.lang.Object], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any] at com.vaadin.cdi.internal.UIScopedContext.getContextualStorage(UIScopedContext.java:68) at org.apache.deltaspike.core.util.context.AbstractContext.get(AbstractContext.java:113) at com.vaadin.cdi.internal.AbstractVaadinContext.get(AbstractVaadinContext.java:154) at com.vaadin.cdi.internal.ContextWrapper.get(ContextWrapper.java:49) at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:101) at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.get(NormalScopedBeanInterceptorHandler.java:71) at xxx.myui.data.DataService$$OwbNormalScopeProxy0.getData(xxx/myui/data/DataService.java) at xxx.myui.view.MyView.loadData(MyView.java:855) at xxx.myui.view.MyView.lambda$27(MyView.java:645) at org.apache.openejb.threads.task.CUCallable$1.call(CUCallable.java:34) at org.apache.openejb.threads.task.CUTask.invoke(CUTask.java:100) at org.apache.openejb.threads.task.CUCallable.call(CUCallable.java:31) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

怎么了?

最佳答案

您对注释 @NormalUIScoped 有什么具体原因吗?如果不是,则此更改有效

// @NormalUIScoped // why is this ?
@UIScoped @Stateful
public class DataService {
...
}

关于asynchronous - EJB 异步线程与 Vaadin,注入(inject)不工作(Vaadin 8、Vaadin-CDI、EJB ManagedExecutorService 线程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47453522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com