List of usage examples for java.util.concurrent Executors newCachedThreadPool
public static ExecutorService newCachedThreadPool()
From source file:com.netflix.curator.framework.recipes.queue.TestDistributedQueue.java
@Test public void testPutListener() throws Exception { final int itemQty = 10; DistributedQueue<TestQueueItem> queue = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/* www . j av a 2 s . c o m*/ try { BlockingQueueConsumer<TestQueueItem> consumer = new BlockingQueueConsumer<TestQueueItem>( Mockito.mock(ConnectionStateListener.class)); queue = QueueBuilder.builder(client, consumer, serializer, QUEUE_PATH).buildQueue(); queue.start(); QueueTestProducer producer = new QueueTestProducer(queue, itemQty, 0); final AtomicInteger listenerCalls = new AtomicInteger(0); QueuePutListener<TestQueueItem> listener = new QueuePutListener<TestQueueItem>() { @Override public void putCompleted(TestQueueItem item) { listenerCalls.incrementAndGet(); } @Override public void putMultiCompleted(MultiItem<TestQueueItem> items) { } }; queue.getPutListenerContainer().addListener(listener); ExecutorService service = Executors.newCachedThreadPool(); service.submit(producer); int iteration = 0; while (consumer.size() < itemQty) { Assert.assertTrue(++iteration < 10); Thread.sleep(1000); } int i = 0; for (TestQueueItem item : consumer.getItems()) { Assert.assertEquals(item.str, Integer.toString(i++)); } Assert.assertEquals(listenerCalls.get(), itemQty); } finally { IOUtils.closeQuietly(queue); IOUtils.closeQuietly(client); } }
From source file:com.netflix.curator.framework.recipes.barriers.TestDistributedDoubleBarrier.java
@Test public void testBasic() throws Exception { final Timing timing = new Timing(); final List<Closeable> closeables = Lists.newArrayList(); final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try {//from www .j a v a 2 s.com closeables.add(client); client.start(); final CountDownLatch postEnterLatch = new CountDownLatch(QTY); final CountDownLatch postLeaveLatch = new CountDownLatch(QTY); final AtomicInteger count = new AtomicInteger(0); final AtomicInteger max = new AtomicInteger(0); List<Future<Void>> futures = Lists.newArrayList(); ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < QTY; ++i) { Future<Void> future = service.submit(new Callable<Void>() { @Override public Void call() throws Exception { DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier", QTY); Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS)); synchronized (TestDistributedDoubleBarrier.this) { int thisCount = count.incrementAndGet(); if (thisCount > max.get()) { max.set(thisCount); } } postEnterLatch.countDown(); Assert.assertTrue(timing.awaitLatch(postEnterLatch)); Assert.assertEquals(count.get(), QTY); Assert.assertTrue(barrier.leave(10, TimeUnit.SECONDS)); count.decrementAndGet(); postLeaveLatch.countDown(); Assert.assertTrue(timing.awaitLatch(postLeaveLatch)); return null; } }); futures.add(future); } for (Future<Void> f : futures) { f.get(); } Assert.assertEquals(count.get(), 0); Assert.assertEquals(max.get(), QTY); } finally { for (Closeable c : closeables) { IOUtils.closeQuietly(c); } } }
From source file:com.microsoft.alm.plugin.idea.ui.pullrequest.CreatePullRequestModel.java
public CreatePullRequestModel(@NotNull final Project project, @NotNull final GitRepository gitRepository) { this.project = project; this.gitRepository = gitRepository; this.tfGitRemotes = TfGitHelper.getTfGitRemotes(gitRepository); this.remoteBranchComboModel = createRemoteBranchDropdownModel(); this.targetBranch = (GitRemoteBranch) this.remoteBranchComboModel.getSelectedItem(); this.applicationProvider = new ApplicationProvider(); this.pullRequestHelper = new PullRequestHelper(); this.diffCompareInfoProvider = new DiffCompareInfoProvider(); this.diffCache = CacheBuilder.newBuilder().maximumSize(20) .build(new CacheLoader<Pair<String, String>, GitCommitCompareInfo>() { @Override/* w w w. j a va2 s . c o m*/ public GitCommitCompareInfo load(Pair<String, String> key) throws Exception { // if we missed the cache, then show the loading spinner, otherwise // just switch to the diff we have to avoid flickering the screen applicationProvider.invokeAndWaitWithAnyModality(new Runnable() { @Override public void run() { // set the view to show loading setLoading(true); } }); return getDiffCompareInfoProvider().getBranchCompareInfo(project, gitRepository, key.getFirst(), key.getSecond()); } }); this.executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); }
From source file:com.linkedin.pinot.broker.broker.BrokerServerBuilder.java
public void buildNetwork() throws ConfigurationException { // build transport Configuration transportConfigs = _config.subset(TRANSPORT_CONFIG_PREFIX); TransportClientConf conf = new TransportClientConf(); conf.init(transportConfigs);/*from w ww.j av a 2s .c o m*/ _registry = new MetricsRegistry(); MetricsHelper.initializeMetrics(_config.subset(METRICS_CONFIG_PREFIX)); MetricsHelper.registerMetricsRegistry(_registry); _brokerMetrics = new BrokerMetrics(_registry); _brokerMetrics.initializeGlobalMeters(); _state.set(State.INIT); _eventLoopGroup = new NioEventLoopGroup(); /** * Some of the client metrics uses histogram which is doing synchronous operation. * These are fixed overhead per request/response. * TODO: Measure the overhead of this. */ final NettyClientMetrics clientMetrics = new NettyClientMetrics(_registry, "client_"); // Setup Netty Connection Pool _resourceManager = new PooledNettyClientResourceManager(_eventLoopGroup, new HashedWheelTimer(), clientMetrics); _poolTimeoutExecutor = new ScheduledThreadPoolExecutor(50); // _requestSenderPool = MoreExecutors.sameThreadExecutor(); final ConnectionPoolConfig cfg = conf.getConnPool(); _requestSenderPool = Executors.newCachedThreadPool(); ConnectionPoolConfig connPoolCfg = conf.getConnPool(); _connPool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>( connPoolCfg.getMinConnectionsPerServer(), connPoolCfg.getMaxConnectionsPerServer(), connPoolCfg.getIdleTimeoutMs(), connPoolCfg.getMaxBacklogPerServer(), _resourceManager, _poolTimeoutExecutor, _requestSenderPool, _registry); // MoreExecutors.sameThreadExecutor(), _registry); _resourceManager.setPool(_connPool); // Setup Routing Table if (conf.getRoutingMode() == RoutingMode.CONFIG) { final CfgBasedRouting rt = new CfgBasedRouting(); rt.init(conf.getCfgBasedRouting()); _routingTable = rt; } else { // Helix based routing is already initialized. } // Setup ScatterGather _scatterGather = new ScatterGatherImpl(_connPool, _requestSenderPool); // Setup Broker Request Handler ReduceServiceRegistry reduceServiceRegistry = buildReduceServiceRegistry(); _requestHandler = new BrokerRequestHandler(_routingTable, _timeBoundaryService, _scatterGather, reduceServiceRegistry, _brokerMetrics, _config); // Register SerDe for data-table. DataTableSerDeRegistry.getInstance().register(new DataTableCustomSerDe(_brokerMetrics)); LOGGER.info("Network initialized !!"); }
From source file:com.amazonaws.services.kinesis.multilang.MessageReaderTest.java
@Test public void readLineFails() throws IOException { BufferedReader input = Mockito.mock(BufferedReader.class); Mockito.doThrow(IOException.class).when(input).readLine(); MessageReader reader = new MessageReader().initialize(input, shardId, new ObjectMapper(), Executors.newCachedThreadPool()); Future<Message> readTask = reader.getNextMessageFromSTDOUT(); try {// www . j a v a2 s . c o m readTask.get(); Assert.fail("The reading task should have failed due to an IOException."); } catch (InterruptedException e) { Assert.fail( "The reading task should not have been interrupted. It should have failed due to an IOException."); } catch (ExecutionException e) { // Yay!! } }
From source file:com.cndatacom.ordersystem.manager.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient.//w ww . j ava 2 s. c om */ public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }
From source file:com.LaunchKeyManager.http.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient./*from www . j a v a 2 s. c om*/ */ public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", this.workAroundReverseDnsBugInHoneycombAndEarlier(), 443)); // schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }
From source file:com.chess.genesis.net.SyncClient.java
private void sync_archive(final JSONObject json) { try {/*from w w w. ja v a2 s .com*/ final ArrayList<String> list_need = getNeedList(json.getJSONArray("gameids")); final ExecutorService pool = Executors.newCachedThreadPool(); for (final String item : list_need) { if (error) return; final NetworkClient nc = new NetworkClient(context, handle); nc.game_data(item); pool.submit(nc); lock++; } } catch (final JSONException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:com.uwindsor.elgg.project.http.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient./*from w w w . java 2s . c o m*/ */ public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }
From source file:hivemall.mix.server.MixServerTest.java
@Test public void test2ClientsZeroOneSparseModel() throws InterruptedException { final int port = NetUtils.getAvailablePort(); CommandLine cl = CommandLineUtils.parseOptions( new String[] { "-port", Integer.toString(port), "-sync_threshold", "30" }, MixServer.getOptions()); MixServer server = new MixServer(cl); ExecutorService serverExec = Executors.newSingleThreadExecutor(); serverExec.submit(server);// w ww . ja v a2s . co m waitForState(server, ServerState.RUNNING); final ExecutorService clientsExec = Executors.newCachedThreadPool(); for (int i = 0; i < 2; i++) { clientsExec.submit(new Runnable() { @Override public void run() { try { invokeClient01("test2ClientsZeroOne", port, false, false); } catch (InterruptedException e) { Assert.fail(e.getMessage()); } } }); } clientsExec.awaitTermination(30, TimeUnit.SECONDS); clientsExec.shutdown(); serverExec.shutdown(); }