相关疑难解决方法(0)

如何在异步任务执行程序中启用请求范围

在我的应用程序中,我有一些异步Web服务.服务器接受请求,返回OK响应并使用AsyncTaskExecutor启动处理请求.我的问题是如何在此处启用请求范围,因为在此处理中我需要获取注释的类:

@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
Run Code Online (Sandbox Code Playgroud)

现在我得到例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.requestContextImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web …
Run Code Online (Sandbox Code Playgroud)

java spring

32
推荐指数
5
解决办法
3万
查看次数

@Stateless vs @RequestScoped

我正在学习使用JAX-RS进行一些安静的api开发,并且对我的资源类有一个问题.

我的理解是我的资源类应该是RequestScoped,但是,当它是RequestScoped时,我调用实体管理器的persist方法会抛出TransactionRequiredException.

如果我将我的资源类更改为无状态,那么一切都很好,实体管理器可以保持没有任何问题.

我仍然是JavaEE的新手,想知道为什么会发生这种情况以及@Stateless注释会使持久化上下文正确注入的内容.我还想知道JAX-RS资源类是否是无状态而不是RequestScoped是否有任何问题,因为我见过的大多数教程都有.

我在下面列举了一些示例代码来说明.

@Path("Things")
//@Stateless //works just fine when em.persist() is called
@RequestScoped //throws transactionrequiredexception when em.persist() is called
public class ThingsResource{

    @PersistenceContext(unitName = "persistenceUnitName")
    EntityManager em;


    public ThingsResource() { }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response postThing(ThingDTO thing){

        ThingEntity newThing = new ThingEntity(thing);
        em.persist(newThing);
        em.flush();

        return Response.created(new URI("/" + newThing.getId()).build();

    }
}
Run Code Online (Sandbox Code Playgroud)

java jax-rs java-ee

19
推荐指数
2
解决办法
2万
查看次数

从线程调用bean时,范围类型javax.enterprise.context.RequestScoped没有活动的上下文

使用Weld-SE 2.1.2.Final获取bean并从线程调用它时,我遇到以下异常:

线程"main"中的异常org.jboss.weld.context.ContextNotActiveException:WELD-001303:范围类型javax.enterprise.context.RequestScoped没有活动的上下文

我的bean用@RequestScooped注释.如果我注释@ApplicationScoped然后它工作正常,但我需要保留@RequestScooped.

这是一个复制者:

public static void main(String[] args) throws Exception {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    final MyPojo pojo = container.instance().select(MyPojo.class).get();

    Thread t = new Thread() {
        public void run() {
            System.out.println(pojo.ping());   // This call fails
        }
    };
    t.start();
    t.join();
    System.out.println(pojo.ping()); // This call succeed
    weld.shutdown();

}

@RequestScoped
public class MyPojo {
 public String ping() {
    return "pong";
 }
}
Run Code Online (Sandbox Code Playgroud)

你遇到过这种行为吗?有什么想让这个工作好吗?

java multithreading cdi weld-se

14
推荐指数
1
解决办法
2万
查看次数

如何使并发实用程序 (JSR 236) 在普通的非 EE Tomcat 8 中工作?

我的技术栈包括

为了能够ManagedExecutorService更明智、更安全地使用和管理线程,我想包含此依赖项

我尝试以不同的方式将 bean 注入到不同范围的 bean(基于视图、会话、基于应用程序)中。

  @Inject
  private javax.enterprise.concurrent.ManagedExecutorService service;
Run Code Online (Sandbox Code Playgroud)

  @Resource
  private javax.enterprise.concurrent.ManagedExecutorService service;
Run Code Online (Sandbox Code Playgroud)

  @Resource(lookup="java:comp/DefaultManagedExecutorService")
  private javax.enterprise.concurrent.ManagedExecutorService service;
Run Code Online (Sandbox Code Playgroud)

似乎没有任何效果,异常消失

java.lang.RuntimeException:
    Error looking up java:comp/env/.../ManagedExecutorService in JNDI
javax.naming.NameNotFoundException:
    Name [.../ManagedExecutorService] is not bound in this Context.
Run Code Online (Sandbox Code Playgroud)

这可能是由于 bean 注册不当造成的。因为我没有找到。我扫描了整个类路径,但在ManagedExecutorService某处找不到任何定义或其任何实现的东西。

我试图自己注册它,context.xml但无济于事。我使用这个 TomEE 教程(天真地)假设我的非 EE Tomcat 的配置是相同的。

<Resource name="ManagedExecutorService"
          type="javax.enterprise.concurrent.ManagedExecutorService" />
Run Code Online (Sandbox Code Playgroud)

<Resource id="ManagedExecutorService"
          type="javax.enterprise.concurrent.ManagedExecutorService" />
Run Code Online (Sandbox Code Playgroud)

显然,事实并非如此。

我的问题是

  1. 是否可以在非 EE 服务器上设置并发实用程序,尤其是 …

java tomcat executorservice apache-tomee java-ee-7

5
推荐指数
0
解决办法
486
查看次数