当前位置: 首页 > 知识库问答 >
问题:

注入到任务中的 EJB 返回不满意的解除异常

边翔宇
2023-03-14

我刚刚安装了Glassfish 4,我正在尝试实现一个Web应用程序,其中包含一个长时间运行的线程,该线程不断将实体插入数据库,该线程在调用servlet时启动。当管理实体的 EJB 被注入到任务中时,我得到一个不满意的分辨率异常:WELD-001308:无法解析 Type:...实例.get() 方法中的错误。

EJB

@Stateless
public class MeasureEJB {

    @Resource
    ManagedExecutorService managedExecutorService;

    @Inject
    Instance<UpdatingTask> myTaskInstance;


    @PersistenceContext(unitName = "smarthomePU")
    private EntityManager em;

    public List<Measure> findMeasures() {
        Query query = em.createQuery("SELECT b FROM Measure b");
        return query.getResultList();
    }

    public Measure createMeasure (Measure _measure) {
        em.persist(_measure);
        return _measure;
    }

    public void executeBgThread() {
        UpdatingTask myTask = myTaskInstance.get(); <- Here I get the error
        this.managedExecutorService.submit(myTask);
    }
}

servlet

@WebServlet(description = "Servlet used to start the historizer", urlPatterns = { "/starthistorizer" })
public class StartHistorizer extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public StartHistorizer() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        measureEjb.executeBgThread();

        response.getWriter().append("Served at: ").append(request.getContextPath());
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

和任务

@无状态公共类UpdatengTask实现Runnable{

private static final String TAG = "smarthome";

@EJB
MeasureEJB measureEjb;

@Override
public void run() {
    // TODO Auto-generated method stub
    BufferedReader br = null;

    try {
        <Insert entities upon sensor messages>
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

}

最后是错误

[2016-09-02T13:19:17.419+0000] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=29 _ThreadName=http-listener-1(5)] [timeMillis: 1472822357419] [levelValue: 900] [[
  StandardWrapperValve[com.storassa.javaee.smarthome.StartHistorizer]: Servlet.service() for servlet com.storassa.javaee.smarthome.StartHistorizer threw exception
javax.ejb.EJBException
    at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:752)
    at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:702)
    at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:507)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at com.sun.proxy.$Proxy244.executeBgThread(Unknown Source)
    at com.storassa.javaee.smarthome.__EJB31_Generated__MeasureEJB__Intf____Bean__.executeBgThread(Unknown Source)
    at com.storassa.javaee.smarthome.StartHistorizer.doGet(StartHistorizer.java:36)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001308: Unable to resolve any beans for Type: class com.storassa.javaee.smarthome.UpdatingTask; Qualifiers: [@javax.enterprise.inject.Default()]
    at org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:101)
    at com.storassa.javaee.smarthome.MeasureEJB.executeBgThread(MeasureEJB.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    ... 34 more
]]

共有2个答案

浦德义
2023-03-14

@LocalBean注释添加到updateTask

会话Bean实现了一个接口,这意味着它只能在cdi中与该接口一起使用。使用<code>@LocalBean

沃弘图
2023-03-14

通常不可能在随机创建的线程中执行EJB。这是因为他们希望找到与当前执行的线程相关联的上下文信息,如安全和事务管理。

此外,在企业应用程序中创建非托管线程从来不是一个好主意,因为您无法控制资源的这种使用。

解决方法是利用JavaEE7的并发实用程序,它提供了从服务器中配置的线程池中获取正确准备的线程的机制

您可以在 Java EE 7:在异步 Servlet 中使用并发实用程序中阅读有关此内容的更多信息。

 类似资料:
  • My beans.xml如下所示: 我的服务(后端类型的EJB)如下所示: 而注释bean使用以下(web): Build已成功,但deploy给了我这个例外: 2018-11-12 11:28:25,706错误[org.jboss.MSC.service.fail](MSC服务线程1-2)MSC000001:无法启动服务jboss.deployment.unit。“kajava-ear-0.0.

  • 我正在尝试创建一个带有Wildfly和数据库连接的EJB Web项目,以持久化我的类“article”。但当我启动我的应用程序时,我得到了错误 包装服务; ArticleDaoImpl

  • 我试图弄清楚如何处理从返回的数据。 我有一个活动,我正在调用异步Firebase get操作(存在于其他类中的操作),并且我想使用检索到的ArrayList的大小更新活动中存在的。以下是我在活动创建中获取的 Firebase 调用: 这是我的Firebase操作: 在Android中,最好的方法是什么?是否有可能实现各种适配器或完成处理程序?

  • 问题内容: 使用新的EJB 3.1规范,是否可以将EJB注入pojo?我知道在EJB 3.0中,@ EJB批注可用于注入EJB,但这不适用于简单的pojos。 如果不是,我必须在JNDI中查找bean,因为我知道您不能简单地使用new关键字。 问题答案: 是的,使用JNDI查找。 由于您的POJO是由您创建的(我假设),因此容器不负责注入依赖项。

  • 我已经尝试了很多方法来解决这个问题,但都做不到。我发现,如果我们使用“abstractBinder”,那么这个问题可能会得到解决,但一旦我的Binder就位,就会出现404错误。 请帮帮忙 我的资源: 我的资源配置文件: 我的活页夹[如果到位] 服务: 用户服务及其实施 其他 断续器 现在,我可以使用访问我的资源http://localhost:8080/javaeeLearning/rest/s

  • 问题内容: 我有以下代码来管理两种存储库。两个存储库类都继承一个接口,以允许对其资源进行重新初始化。 全局的,应用程序范围的存储库: 每个用户的会话范围的存储库: 当将此(无)注入上下文时 有用。但是,在添加到类中时,部署失败,并显示以下异常: 添加CDI bean的名称,例如 结果相同。唯一可以结合使用的方法是在var声明中使用该接口: 但是,我可能在这里需要子类功能,因此(现在)确实不需要使用