Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-2147

Sharing CDI beans for more threads for CompletionStage and rxjava

XMLWordPrintable

    • Icon: Feature Request Feature Request
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • None
    • None
    • None

      Resteasy supports sharing context for more threads in CompletionStage in RESTEasy 4.0 (RESTEASY-1939)

      This doesn't work for injection of CDI beans. If injected bean is used from CompletionStage, Flowable, or something like that, it leads to:
      Method threw 'org.jboss.weld.contexts.ContextNotActiveException' exception. Cannot evaluate org.resteasy.simple.deployment.CustomBean$Proxy$_$$_WeldClientProxy.toString()

      Example:

      @RequestScoped
      public class CustomBean {
          public String get() {
              return "message from custom bean";
          }
      }
      
      @Path("/")
      @RequestScoped
      public class TestResource {
      
          @Inject
          CustomBean bean;
          
          @Path("a")
          @GET
          public CompletionStage<Response> a() {
              ExecutorService executor = Executors.newSingleThreadExecutor();
              CompletableFuture<Response> resp = new CompletableFuture<>();
              executor.submit(() -> {
                  System.out.println(bean.get());
                  resp.complete(Response.ok("a").build());
              });
              return resp;
          }
      
          @GET
          @Path("c")
          @Produces(MediaType.APPLICATION_JSON)
          @Stream(Stream.MODE.GENERAL)
          public Flowable<String> multiThreadRx() {
              System.out.println("get: " + Thread.currentThread().toString());
              return Flowable.create(new FlowableOnSubscribe<String>() {
      
                  @Override
                  public void subscribe(FlowableEmitter<String> emitter) throws Exception {
      				System.out.println(bean.get());
      				System.out.println("emmiter: " + Thread.currentThread().toString());
      				for (Integer i = 0; i < 5; i++) {
      					emitter.onNext("some string " + i);
      				}
      				emitter.onComplete();
      			}}, BackpressureStrategy.BUFFER).subscribeOn(Schedulers.newThread())
      		.observeOn(Schedulers.newThread());
          }
      }
      

      If injected bean is used in Completion stage with RESTEASY-1939 wrapping, it leads to ContextNotActiveException:

          @GET
          @Path("e")
          @Produces("text/plain")
          public CompletionStage<String> e(@Context HttpRequest req) {
              System.out.println("request (inline): " + req);
              System.out.println("application (inline): " + ResteasyContext.getContextData(Application.class));
              CompletableFuture<String> cs = new CompletableFuture<>();
              ExecutorService executor = Executors.newSingleThreadExecutor();
              executor.submit(
                      io.reactiverse.reactivecontexts.core.Context.wrap(new Runnable() {
                          public void run() {
                              try {
                                  System.out.println("request (async): " + req);
                                  System.out.println(bean.get());
                                  System.out.println("application (async): " + ResteasyContext.getContextData(Application.class));
                                  cs.complete("hello");
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          }
                      }));
              return cs;
          }
      
      10:48:19,502 ERROR [stderr] (pool-18-thread-1) org.jboss.weld.contexts.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
      10:48:19,503 ERROR [stderr] (pool-18-thread-1) 	at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:647)
      10:48:19,503 ERROR [stderr] (pool-18-thread-1) 	at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:89)
      10:48:19,504 ERROR [stderr] (pool-18-thread-1) 	at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:164)
      10:48:19,504 ERROR [stderr] (pool-18-thread-1) 	at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
      10:48:19,504 ERROR [stderr] (pool-18-thread-1) 	at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:87)
      10:48:19,505 ERROR [stderr] (pool-18-thread-1) 	at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:131)
      10:48:19,505 ERROR [stderr] (pool-18-thread-1) 	at org.resteasy.simple.deployment.CustomBean$Proxy$_$$_WeldClientProxy.get(Unknown Source)
      10:48:19,505 ERROR [stderr] (pool-18-thread-1) 	at org.resteasy.simple.deployment.TestResource$3.run(TestResource.java:131)
      10:48:19,506 ERROR [stderr] (pool-18-thread-1) 	at io.reactiverse.reactivecontexts.core.Context.lambda$wrap$0(Context.java:211)
      10:48:19,506 ERROR [stderr] (pool-18-thread-1) 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      10:48:19,507 ERROR [stderr] (pool-18-thread-1) 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      10:48:19,507 ERROR [stderr] (pool-18-thread-1) 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      10:48:19,508 ERROR [stderr] (pool-18-thread-1) 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      10:48:19,509 ERROR [stderr] (pool-18-thread-1) 	at java.lang.Thread.run(Thread.java:748)
      

      cc: rsigal@redhat.com, rhn-support-asoldano, mstefank, FroMage

            Unassigned Unassigned
            mkopecky@redhat.com Marek Kopecky
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: