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);
}
}
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);
}
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));
}
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));
}
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);
}
Aggregations