Java Code Examples for io.netty.util.concurrent.EventExecutor#submit()

The following examples show how to use io.netty.util.concurrent.EventExecutor#submit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractWorker.java    From pampas with Apache License 2.0 5 votes vote down vote up
public PampasResponse execute(PampasRequest<Q> req, Locator locator, FilterChain filterChain) {
    try {
        PampasResponse beforeCallResponse = filterChain.executeBeforeCall(req, locator);

        if (filterChain.isFilterChainStop()) {
            return beforeCallResponse;
        }


        CompletableFuture<PampasResponse<R>> future = null;

        // 调用具体的worker 处理此request
        future = doExecute(req, locator);
        PampasResponse<R> gatewayResponse = future.join();

        log.debug("获取响应:{}", gatewayResponse);
        PampasResponse executeAfterResponse = filterChain.executeAfter(req, locator, gatewayResponse);
        if(filterChain.isFilterChainStop()){
            return executeAfterResponse;
        }
        EventExecutor executor = req.channelHandlerContext().executor();
        executor.submit(() -> doAfter());
        return gatewayResponse;
    } catch (Exception ex) {
        log.error("处理请求发生错误:{}", ex.getMessage(), ex);
        return PampasResponseHelper.buildFailedResponse(ex);
    } finally {
        ReferenceCountUtil.release(req.requestData());
    }
}
 
Example 2
Source File: NettyUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Runs a task in the given {@link EventExecutor}. Runs immediately if the current thread is in the
 * eventExecutor.
 *
 * @param eventExecutor Executor to run task in.
 * @param runnable Task to run.
 */
public static void doInEventLoop(EventExecutor eventExecutor, Runnable runnable) {
    if (eventExecutor.inEventLoop()) {
        runnable.run();
    } else {
        eventExecutor.submit(runnable);
    }
}
 
Example 3
Source File: ColocatedEventLoopGroup.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
ColocatedEventLoopGroup(EventLoopGroup eventLoopGroup) {
	this.eventLoopGroup = eventLoopGroup;
	for (EventExecutor ex : eventLoopGroup) {
		if (ex instanceof EventLoop) {
			//"FutureReturnValueIgnored" this is deliberate
			ex.submit(() -> {
				if (!localLoop.isSet()) {
					localLoop.set((EventLoop) ex);
				}
			});
		}
	}
}
 
Example 4
Source File: NettyUtils.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Runs a task in the given {@link EventExecutor}. Runs immediately if the current thread is in the
 * eventExecutor.
 *
 * @param eventExecutor Executor to run task in.
 * @param runnable Task to run.
 */
public static void doInEventLoop(EventExecutor eventExecutor, Runnable runnable) {
  if (eventExecutor.inEventLoop()) {
    runnable.run();
  } else {
    eventExecutor.submit(runnable);
  }
}
 
Example 5
Source File: RequestContextTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
public void contextAwareEventExecutor() throws Exception {
    when(channel.eventLoop()).thenReturn(eventLoop.get());
    final RequestContext context = createContext();
    final Set<Integer> callbacksCalled = Collections.newSetFromMap(new ConcurrentHashMap<>());
    final EventExecutor executor = context.contextAwareEventLoop();
    final CountDownLatch latch = new CountDownLatch(18);
    executor.execute(() -> checkCallback(1, context, callbacksCalled, latch));
    executor.schedule(() -> checkCallback(2, context, callbacksCalled, latch), 0, TimeUnit.SECONDS);
    executor.schedule(() -> {
        checkCallback(2, context, callbacksCalled, latch);
        return "success";
    }, 0, TimeUnit.SECONDS);
    executor.scheduleAtFixedRate(() -> checkCallback(3, context, callbacksCalled, latch), 0, 1000,
                                 TimeUnit.SECONDS);
    executor.scheduleWithFixedDelay(() -> checkCallback(4, context, callbacksCalled, latch), 0, 1000,
                                    TimeUnit.SECONDS);
    executor.submit(() -> checkCallback(5, context, callbacksCalled, latch));
    executor.submit(() -> checkCallback(6, context, callbacksCalled, latch), "success");
    executor.submit(() -> {
        checkCallback(7, context, callbacksCalled, latch);
        return "success";
    });
    executor.invokeAll(makeTaskList(8, 10, context, callbacksCalled, latch));
    executor.invokeAll(makeTaskList(11, 12, context, callbacksCalled, latch), 10000, TimeUnit.SECONDS);
    executor.invokeAny(makeTaskList(13, 13, context, callbacksCalled, latch));
    executor.invokeAny(makeTaskList(14, 14, context, callbacksCalled, latch), 10000, TimeUnit.SECONDS);
    final Promise<String> promise = executor.newPromise();
    promise.addListener(f -> checkCallback(15, context, callbacksCalled, latch));
    promise.setSuccess("success");
    executor.newSucceededFuture("success")
            .addListener(f -> checkCallback(16, context, callbacksCalled, latch));
    executor.newFailedFuture(new IllegalArgumentException())
            .addListener(f -> checkCallback(17, context, callbacksCalled, latch));
    final ProgressivePromise<String> progressivePromise = executor.newProgressivePromise();
    progressivePromise.addListener(f -> checkCallback(18, context, callbacksCalled, latch));
    progressivePromise.setSuccess("success");
    latch.await();
    eventLoop.get().shutdownGracefully();
    await().untilAsserted(() -> {
        assertThat(callbacksCalled).containsExactlyElementsOf(IntStream.rangeClosed(1, 18)
                                                                       .boxed()::iterator);
    });
}