Search in sources :

Example 86 with Executor

use of java.util.concurrent.Executor in project fast-cast by RuedigerMoeller.

the class TestEchoServer method echoServer.

public static void echoServer(FastCast fc) throws InterruptedException {
    final FCPublisher echoresp = fc.onTransport("default").publish(fc.getPublisherConf("echoresp"));
    final Executor responseExec = Executors.newSingleThreadExecutor();
    final long startUpTime = (int) System.currentTimeMillis();
    startTestTopic(fc, startUpTime);
    startEchoTopic(fc, echoresp, responseExec);
    startUnreliableTopic(fc);
    while (true) {
        Thread.sleep(1000);
    }
}
Also used : Executor(java.util.concurrent.Executor) FCPublisher(org.nustaq.fastcast.api.FCPublisher)

Example 87 with Executor

use of java.util.concurrent.Executor in project cradle by BingLau7.

the class ChannelOperationExamples method writingToChannelManyThreads.

// Channel 是线程安全的,可以在多个线程中使用它,需要注意的是,消息将会被保证按顺序发送
public static void writingToChannelManyThreads() {
    // Get the channel reference from somewhere
    final Channel channel = null;
    // 创建持有要写数据的 ByteBuf
    final ByteBuf buf = Unpooled.copiedBuffer("your data", CharsetUtil.UTF_8).retain();
    // 创建将数据写到 Channel 的 Runnable
    Runnable writer = new Runnable() {

        @Override
        public void run() {
            channel.writeAndFlush(buf.duplicate());
        }
    };
    // 获取线程池引用
    Executor executor = Executors.newCachedThreadPool();
    // 递交写任务给线程池以便在某个线程中执行
    executor.execute(writer);
    // 递交另一个写任务以便在另一个线程中执行
    executor.execute(writer);
}
Also used : Executor(java.util.concurrent.Executor) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf)

Example 88 with Executor

use of java.util.concurrent.Executor in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method constructor_works_with_valid_args.

@Test
public void constructor_works_with_valid_args() {
    // given
    SslContext sslCtx = mock(SslContext.class);
    int maxRequestSizeInBytes = 42;
    Collection<Endpoint<?>> endpoints = Arrays.asList(getMockEndpoint("/some/path", HttpMethod.GET));
    RequestAndResponseFilter beforeSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(true).when(beforeSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    RequestAndResponseFilter afterSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(false).when(afterSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    List<RequestAndResponseFilter> reqResFilters = Arrays.asList(beforeSecurityRequestFilter, afterSecurityRequestFilter);
    Executor longRunningTaskExecutor = mock(Executor.class);
    RiposteErrorHandler riposteErrorHandler = mock(RiposteErrorHandler.class);
    RiposteUnhandledErrorHandler riposteUnhandledErrorHandler = mock(RiposteUnhandledErrorHandler.class);
    RequestValidator validationService = mock(RequestValidator.class);
    ObjectMapper requestContentDeserializer = mock(ObjectMapper.class);
    ResponseSender responseSender = mock(ResponseSender.class);
    @SuppressWarnings("unchecked") MetricsListener metricsListener = mock(MetricsListener.class);
    long defaultCompletableFutureTimeoutMillis = 4242L;
    AccessLogger accessLogger = mock(AccessLogger.class);
    List<PipelineCreateHook> pipelineCreateHooks = mock(List.class);
    RequestSecurityValidator requestSecurityValidator = mock(RequestSecurityValidator.class);
    long workerChannelIdleTimeoutMillis = 121000;
    long proxyRouterConnectTimeoutMillis = 4200;
    long incompleteHttpCallTimeoutMillis = 1234;
    int maxOpenChannelsThreshold = 1000;
    boolean debugChannelLifecycleLoggingEnabled = true;
    List<String> userIdHeaderKeys = mock(List.class);
    // when
    HttpChannelInitializer hci = new HttpChannelInitializer(sslCtx, maxRequestSizeInBytes, endpoints, reqResFilters, longRunningTaskExecutor, riposteErrorHandler, riposteUnhandledErrorHandler, validationService, requestContentDeserializer, responseSender, metricsListener, defaultCompletableFutureTimeoutMillis, accessLogger, pipelineCreateHooks, requestSecurityValidator, workerChannelIdleTimeoutMillis, proxyRouterConnectTimeoutMillis, incompleteHttpCallTimeoutMillis, maxOpenChannelsThreshold, debugChannelLifecycleLoggingEnabled, userIdHeaderKeys);
    // then
    assertThat(extractField(hci, "sslCtx"), is(sslCtx));
    assertThat(extractField(hci, "maxRequestSizeInBytes"), is(maxRequestSizeInBytes));
    assertThat(extractField(hci, "endpoints"), is(endpoints));
    assertThat(extractField(hci, "longRunningTaskExecutor"), is(longRunningTaskExecutor));
    assertThat(extractField(hci, "riposteErrorHandler"), is(riposteErrorHandler));
    assertThat(extractField(hci, "riposteUnhandledErrorHandler"), is(riposteUnhandledErrorHandler));
    assertThat(extractField(hci, "validationService"), is(validationService));
    assertThat(extractField(hci, "requestContentDeserializer"), is(requestContentDeserializer));
    assertThat(extractField(hci, "responseSender"), is(responseSender));
    assertThat(extractField(hci, "metricsListener"), is(metricsListener));
    assertThat(extractField(hci, "defaultCompletableFutureTimeoutMillis"), is(defaultCompletableFutureTimeoutMillis));
    assertThat(extractField(hci, "accessLogger"), is(accessLogger));
    assertThat(extractField(hci, "pipelineCreateHooks"), is(pipelineCreateHooks));
    assertThat(extractField(hci, "requestSecurityValidator"), is(requestSecurityValidator));
    assertThat(extractField(hci, "workerChannelIdleTimeoutMillis"), is(workerChannelIdleTimeoutMillis));
    assertThat(extractField(hci, "incompleteHttpCallTimeoutMillis"), is(incompleteHttpCallTimeoutMillis));
    assertThat(extractField(hci, "maxOpenChannelsThreshold"), is(maxOpenChannelsThreshold));
    assertThat(extractField(hci, "debugChannelLifecycleLoggingEnabled"), is(debugChannelLifecycleLoggingEnabled));
    assertThat(extractField(hci, "userIdHeaderKeys"), is(userIdHeaderKeys));
    StreamingAsyncHttpClient sahc = extractField(hci, "streamingAsyncHttpClientForProxyRouterEndpoints");
    assertThat(extractField(sahc, "idleChannelTimeoutMillis"), is(workerChannelIdleTimeoutMillis));
    assertThat(extractField(sahc, "downstreamConnectionTimeoutMillis"), is((int) proxyRouterConnectTimeoutMillis));
    assertThat(extractField(sahc, "debugChannelLifecycleLoggingEnabled"), is(debugChannelLifecycleLoggingEnabled));
    RequestFilterHandler beforeSecReqFH = extractField(hci, "beforeSecurityRequestFilterHandler");
    assertThat(extractField(beforeSecReqFH, "filters"), is(Collections.singletonList(beforeSecurityRequestFilter)));
    RequestFilterHandler afterSecReqFH = extractField(hci, "afterSecurityRequestFilterHandler");
    assertThat(extractField(afterSecReqFH, "filters"), is(Collections.singletonList(afterSecurityRequestFilter)));
    ResponseFilterHandler resFH = extractField(hci, "cachedResponseFilterHandler");
    List<RequestAndResponseFilter> reversedFilters = new ArrayList<>(reqResFilters);
    Collections.reverse(reversedFilters);
    assertThat(extractField(resFH, "filtersInResponseProcessingOrder"), is(reversedFilters));
}
Also used : RiposteUnhandledErrorHandler(com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler) ResponseFilterHandler(com.nike.riposte.server.handler.ResponseFilterHandler) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) PipelineCreateHook(com.nike.riposte.server.hooks.PipelineCreateHook) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) ResponseSender(com.nike.riposte.server.http.ResponseSender) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) Endpoint(com.nike.riposte.server.http.Endpoint) RequestValidator(com.nike.riposte.server.error.validation.RequestValidator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SslContext(io.netty.handler.ssl.SslContext) StreamingAsyncHttpClient(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) RequestSecurityValidator(com.nike.riposte.server.error.validation.RequestSecurityValidator) Endpoint(com.nike.riposte.server.http.Endpoint) RequestFilterHandler(com.nike.riposte.server.handler.RequestFilterHandler) MetricsListener(com.nike.riposte.metrics.MetricsListener) AccessLogger(com.nike.riposte.server.logging.AccessLogger) Test(org.junit.Test)

Example 89 with Executor

use of java.util.concurrent.Executor in project riposte by Nike-Inc.

the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfoForVipAddress_uses_provided_executor_for_future_if_provided_one.

@Test
public void getActiveInstanceInfoForVipAddress_uses_provided_executor_for_future_if_provided_one() {
    // given
    InstanceInfo iiMock = mock(InstanceInfo.class);
    doReturn(Optional.of(iiMock)).when(serviceSpy).getActiveInstanceInfoForVipAddressBlocking(vip);
    Executor executorSpy = spy(new SpyableExecutor());
    // when
    CompletableFuture<Optional<InstanceInfo>> result = serviceSpy.getActiveInstanceInfoForVipAddress(vip, Optional.of(executorSpy));
    // then
    assertThat(result.join()).isEqualTo(Optional.of(iiMock));
    verify(executorSpy).execute(any(Runnable.class));
}
Also used : Executor(java.util.concurrent.Executor) Optional(java.util.Optional) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 90 with Executor

use of java.util.concurrent.Executor in project riposte by Nike-Inc.

the class EurekaVipAddressRoundRobinServiceTest method getActiveInstanceInfosForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfosForVipAddressBlocking.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void getActiveInstanceInfosForVipAddress_returns_future_that_returns_data_from_getActiveInstanceInfosForVipAddressBlocking(boolean useExecutor) {
    // given
    InstanceInfo iiMock = mock(InstanceInfo.class);
    List<InstanceInfo> expectedInstances = Collections.singletonList(iiMock);
    doReturn(expectedInstances).when(serviceSpy).getActiveInstanceInfosForVipAddressBlocking(vip);
    Optional<Executor> executorOpt = useExecutor ? Optional.of(Executors.newSingleThreadExecutor()) : Optional.empty();
    // when
    CompletableFuture<List<InstanceInfo>> result = serviceSpy.getActiveInstanceInfosForVipAddress(vip, executorOpt);
    // then
    assertThat(result.join()).isEqualTo(expectedInstances);
}
Also used : Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) List(java.util.List) InstanceInfo(com.netflix.appinfo.InstanceInfo) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

Executor (java.util.concurrent.Executor)302 Test (org.junit.Test)127 ArrayList (java.util.ArrayList)35 CountDownLatch (java.util.concurrent.CountDownLatch)32 IOException (java.io.IOException)29 List (java.util.List)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)22 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Map (java.util.Map)15 Timeouts (org.neo4j.cluster.timeout.Timeouts)15 ExecutorService (java.util.concurrent.ExecutorService)13 InstanceId (org.neo4j.cluster.InstanceId)13 Config (org.neo4j.kernel.configuration.Config)13 File (java.io.File)12 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)12 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)12 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)12 InetSocketAddress (java.net.InetSocketAddress)10