예제 #1
0
  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public boolean hasNext() {
    while (!hasElement && (co.getState() != Coroutine.State.FINISHED)) {
      co.run();
    }

    return hasElement;
  }
예제 #2
0
  /**
   * Produces the next value to be returned by the {@link #next} method.
   *
   * @param element The value that should be returned by {@link #next}
   * @throws SuspendExecution de.matthiasmann.coroutines.SuspendExecution This method will suspend
   *     the execution
   * @throws IllegalStateException DOCUMENT ME!
   */
  protected void produce(E element) throws SuspendExecution {
    if (hasElement) {
      throw new IllegalStateException("hasElement = true");
    }

    this.element = element;
    hasElement = true;
    Coroutine.yield();
  }
예제 #3
0
  public static NginxResponse handleRequest(final NginxRequest req) {
    try {

      if (coroutineEnabled) {
        CoroutineRunner coroutineRunner = new CoroutineRunner(req);
        Coroutine coroutine = new Coroutine(coroutineRunner);
        coroutine.resume();
        if (coroutine.getState() == Coroutine.State.FINISHED) {
          return coroutineRunner.response;
        } else {
          return new NginxJavaResponse(req, Constants.ASYNC_TAG);
        }
      } else {
        return req.handler().process(req);
      }
    } catch (Throwable e) {
      log.error("server unhandled exception!", e);
      return buildUnhandledExceptionResponse(req, e);
    }
  }
예제 #4
0
  @Test
  public void testInherit() {
    final C dut = new C();
    Coroutine c =
        new Coroutine(
            new Runnable() {
              public void run() throws SuspendExecution {
                dut.myMethod();
              }
            });
    for (int i = 0; i < 3; i++) {
      c.run();
    }

    assertEquals(5, dut.result.size());
    assertEquals("a", dut.result.get(0));
    assertEquals("o1", dut.result.get(1));
    assertEquals("o2", dut.result.get(2));
    assertEquals("b", dut.result.get(3));
    assertEquals("b", dut.result.get(4));
  }
예제 #5
0
    @SuppressWarnings("rawtypes")
    @Override
    public void run() throws SuspendExecution {
      try {
        response = request.handler().process(request);
      } catch (Throwable e) {
        response = buildUnhandledExceptionResponse(request, e);
        log.error("unhandled exception in coroutine", e);
      }

      if (Coroutine.getActiveCoroutine().getResumeCounter() != 1) {
        request.handler().completeAsyncResponse(request, response);
      }
    }
예제 #6
0
 public void otherMethod() throws SuspendExecution {
   result.add("o1");
   Coroutine.yield();
   result.add("o2");
 }
예제 #7
0
 public static void yield() throws SuspendExecution {
   Coroutine.yield();
 }