List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
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); waitForState(server, ServerState.RUNNING); final ExecutorService clientsExec = Executors.newCachedThreadPool(); for (int i = 0; i < 2; i++) { clientsExec.submit(new Runnable() { @Override//from www .j ava 2 s .c o m 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(); }
From source file:hivemall.mix.server.MixServerTest.java
@Test public void test2ClientsZeroOneDenseModel() 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); waitForState(server, ServerState.RUNNING); final ExecutorService clientsExec = Executors.newCachedThreadPool(); for (int i = 0; i < 2; i++) { clientsExec.submit(new Runnable() { @Override//from ww w .j a v a 2 s . c o m public void run() { try { invokeClient01("test2ClientsZeroOne", port, true, false); } catch (InterruptedException e) { Assert.fail(e.getMessage()); } } }); } clientsExec.awaitTermination(30, TimeUnit.SECONDS); clientsExec.shutdown(); serverExec.shutdown(); }
From source file:hivemall.mix.server.MixServerTest.java
@Test public void test2ClientsZeroOneSparseModelWithMixCanceling() 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); waitForState(server, ServerState.RUNNING); final ExecutorService clientsExec = Executors.newCachedThreadPool(); for (int i = 0; i < 2; i++) { clientsExec.submit(new Runnable() { @Override//from ww w . j av a 2 s . c o m public void run() { try { invokeClient01("test2ClientsZeroOne", port, false, true); } catch (InterruptedException e) { Assert.fail(e.getMessage()); } } }); } clientsExec.awaitTermination(30, TimeUnit.SECONDS); clientsExec.shutdown(); serverExec.shutdown(); }
From source file:hivemall.mix.server.MixServerTest.java
@Test public void test2ClientsZeroOneDenseModelWithMixCanceling() 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); waitForState(server, ServerState.RUNNING); final ExecutorService clientsExec = Executors.newCachedThreadPool(); for (int i = 0; i < 2; i++) { clientsExec.submit(new Runnable() { @Override/*from w w w .j ava 2 s . c o m*/ public void run() { try { invokeClient01("test2ClientsZeroOne", port, true, true); } catch (InterruptedException e) { Assert.fail(e.getMessage()); } } }); } clientsExec.awaitTermination(30, TimeUnit.SECONDS); clientsExec.shutdown(); serverExec.shutdown(); }
From source file:org.bpmscript.process.hibernate.SpringHibernateInstanceManagerTest.java
public void testInstanceManagerSeparateThread() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "/org/bpmscript/endtoend/spring.xml"); try {/* w w w . j ava2 s .co m*/ final IInstanceManager instanceManager = (IInstanceManager) context.getBean("instanceManager"); final String pid1 = instanceManager.createInstance("parentVersion", "definitionId", "test", IJavascriptProcessDefinition.DEFINITION_TYPE_JAVASCRIPT, "one"); IInstance instance = instanceManager.getInstance(pid1); assertNotNull(instance); instanceManager.createInstance("parentVersion", "definitionId", "test", IJavascriptProcessDefinition.DEFINITION_TYPE_JAVASCRIPT, "two"); ExecutorService executorService = Executors.newFixedThreadPool(2); final AtomicReference<Queue<String>> results = new AtomicReference<Queue<String>>( new LinkedList<String>()); Future<Object> future1 = executorService.submit(new Callable<Object>() { public Object call() throws Exception { return instanceManager.doWithInstance(pid1, new IInstanceCallback() { public IExecutorResult execute(IInstance instance) throws Exception { log.info("locking one"); Thread.sleep(2000); results.get().add("one"); return new IgnoredResult("", "", ""); } }); } }); Thread.sleep(100); assertNotNull(future1.get()); } finally { context.destroy(); } }
From source file:hivemall.mix.server.MixServerTest.java
@Test public void testMultipleClients() throws InterruptedException { final int port = NetUtils.getAvailablePort(); CommandLine cl = CommandLineUtils.parseOptions( new String[] { "-port", Integer.toString(port), "-sync_threshold", "3" }, MixServer.getOptions()); MixServer server = new MixServer(cl); ExecutorService serverExec = Executors.newSingleThreadExecutor(); serverExec.submit(server); waitForState(server, ServerState.RUNNING); final int numClients = 5; final ExecutorService clientsExec = Executors.newCachedThreadPool(); for (int i = 0; i < numClients; i++) { clientsExec.submit(new Runnable() { @Override//from w ww .ja v a2 s. c o m public void run() { try { invokeClient("testMultipleClients", port); } catch (InterruptedException e) { Assert.fail(e.getMessage()); } } }); } clientsExec.awaitTermination(10, TimeUnit.SECONDS); clientsExec.shutdown(); serverExec.shutdown(); }
From source file:com.asakusafw.runtime.util.cache.HadoopFileCacheRepositoryTest.java
/** * Conflict cache creation.// ww w. j a va 2s. com * @throws Exception if failed */ @Test public void conflict() throws Exception { File source = folder.newFile(); byte[] bytes = new byte[1024 * 1024]; try (OutputStream output = new FileOutputStream(source)) { for (int i = 0, n = 50; i < n; i++) { output.write(bytes); } } Path path = path(source); File cacheRepo = folder.newFolder(); Configuration configuration = new ConfigurationProvider().newInstance(); LockProvider<Path> locks = new LocalFileLockProvider<>(folder.newFolder()); RetryStrategy retrier = new ConstantRetryStrategy(30, 100, 200); FileCacheRepository cache = new HadoopFileCacheRepository(configuration, path(cacheRepo), locks, retrier); List<Future<Path>> futures = new ArrayList<>(); int count = 10; CountDownLatch latch = new CountDownLatch(count); ExecutorService executor = Executors.newFixedThreadPool(count); try { for (int i = 0; i < count; i++) { String label = String.format("thread-%d", i); futures.add(executor.submit(() -> { LOG.info("Wait: resolve @" + label); latch.countDown(); if (latch.await(5, TimeUnit.SECONDS) == false) { throw new TimeoutException(); } LOG.info("Start: resolve @" + label); Path result = cache.resolve(path); LOG.info("Finish: resolve @" + label); return result; })); } executor.shutdown(); if (executor.awaitTermination(30, TimeUnit.SECONDS) == false) { throw new TimeoutException(); } } finally { executor.shutdownNow(); } for (Future<Path> future : futures) { future.get(); } }
From source file:com.bazaarvoice.seo.sdk.BVUIContentServiceProvider.java
/** * Self executioner method.//from w ww . j a va 2s . c om * * @param reload * @return */ public StringBuilder executeCall(boolean reload) { if (reload) { return new StringBuilder(_uiContent); } boolean isSearchBot = showUserAgentSEOContent(); long executionTimeout = isSearchBot ? Long.parseLong( _bvConfiguration.getProperty(BVClientConfig.EXECUTION_TIMEOUT_BOT.getPropertyName())) : Long.parseLong(_bvConfiguration.getProperty(BVClientConfig.EXECUTION_TIMEOUT.getPropertyName())); if (!isSearchBot && executionTimeout == 0) { _message.append(BVMessageUtil.getMessage("MSG0004")); return null; } if (isSearchBot && executionTimeout < 100) { executionTimeout = 100; _message.append(BVMessageUtil.getMessage("MSG0005")); } ExecutorService executorService = BVThreadPool.getExecutorService(); Future<StringBuilder> future = executorService.submit(this); try { _uiContent = future.get(executionTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { // e.printStackTrace(); } catch (ExecutionException e) { // e.printStackTrace(); if (e.getCause() instanceof BVSdkException) { throw new BVSdkException(e.getCause().getMessage()); } } catch (TimeoutException e) { String err = isSearchBot ? "ERR0026" : "ERR0018"; _message.append(MessageFormat.format(BVMessageUtil.getMessage(err), new Object[] { executionTimeout })); } return new StringBuilder(_uiContent); }
From source file:com.cognifide.aet.job.common.collectors.source.SourceCollector.java
private byte[] getContent() throws ProcessingException { byte[] content; ExecutorService executor = Executors.newCachedThreadPool(); Callable<Object> task = new Callable<Object>() { @Override//from w w w . j ava2s . c o m public Object call() throws IOException { return httpRequestBuilder.executeRequest().getContent(); } }; Future<Object> future = executor.submit(task); try { content = (byte[]) future.get(timeoutValue, TimeUnit.MILLISECONDS); } catch (TimeoutException | InterruptedException | ExecutionException e) { throw new ProcessingException(e.getMessage(), e); } finally { future.cancel(true); } return content; }
From source file:com.sixt.service.framework.kafka.messaging.KafkaIntegrationTest.java
@Ignore("long running test") @Test/*from w w w. ja v a 2s .c om*/ public void partitionAssignmentChange() throws InterruptedException { ServiceProperties serviceProperties = new ServiceProperties(); serviceProperties.initialize(new String[] {}); // Reads environment variables set by DockerComposeHelper // Topics are created with 3 partitions - see docker-compose-integrationtest.yml Topic ping = new Topic("ping"); Topic pong = new Topic("pong"); Producer producer = new ProducerFactory(serviceProperties).createProducer(); final AtomicBoolean produceMessages = new AtomicBoolean(true); final AtomicInteger sentMessages = new AtomicInteger(0); final AtomicInteger receivedMessagesConsumer1 = new AtomicInteger(0); final CountDownLatch firstMessageProcessedConsumer1 = new CountDownLatch(1); final AtomicInteger receivedMessagesConsumer2 = new AtomicInteger(0); final CountDownLatch firstMessageProcessedConsumer2 = new CountDownLatch(1); final AtomicInteger receivedMessagesConsumer3 = new AtomicInteger(0); final CountDownLatch firstMessageProcessedConsumer3 = new CountDownLatch(1); // Produce messages until test tells producer to stop. ExecutorService producerExecutor = Executors.newSingleThreadExecutor(); producerExecutor.submit(new Runnable() { @Override public void run() { OrangeContext context = new OrangeContext(); Sleeper sleeper = new Sleeper(); try { while (produceMessages.get()) { String key = RandomStringUtils.randomAscii(5); SayHelloToCmd payload = SayHelloToCmd.newBuilder().setName(key).build(); Message request = Messages.requestFor(ping, pong, key, payload, context); producer.send(request); sentMessages.incrementAndGet(); sleeper.sleepNoException(250); } } catch (Throwable t) { logger.error("Exception in producer loop", t); } } }); // Start first producer. It should get all 3 partitions assigned. Consumer consumer1 = consumerFactoryWithHandler(serviceProperties, SayHelloToCmd.class, new MessageHandler<SayHelloToCmd>() { @Override public void onMessage(Message<SayHelloToCmd> message, OrangeContext context) { receivedMessagesConsumer1.incrementAndGet(); firstMessageProcessedConsumer1.countDown(); } }).consumerForTopic(ping, new DiscardFailedMessages()); // wait until consumer 1 is up. firstMessageProcessedConsumer1.await(); Thread.sleep(5000); // consume some messages // Now, start second processor. It should get at least one partition assigned. Consumer consumer2 = consumerFactoryWithHandler(serviceProperties, SayHelloToCmd.class, new MessageHandler<SayHelloToCmd>() { @Override public void onMessage(Message<SayHelloToCmd> message, OrangeContext context) { receivedMessagesConsumer2.incrementAndGet(); firstMessageProcessedConsumer2.countDown(); } }).consumerForTopic(ping, new DiscardFailedMessages()); // wait until the second consumer is up. firstMessageProcessedConsumer2.await(); Thread.sleep(5000); // let both consumers run a bit brutallyKillConsumer("pool-14-thread-1"); // consumer2 thread, HACKY: if this is too brittle, change the test to shutdown() //Need to wait a bit longer while Kafka "restabilizes the group" after consumer 2 was killed. // -> Consumer 1 should now get all three partitions back again. Thread.sleep(30000); // must be > than max.poll.interval.ms // Now, start third processor. It should get at least one partition assigned. Consumer consumer3 = consumerFactoryWithHandler(serviceProperties, SayHelloToCmd.class, new MessageHandler<SayHelloToCmd>() { @Override public void onMessage(Message<SayHelloToCmd> message, OrangeContext context) { receivedMessagesConsumer3.incrementAndGet(); firstMessageProcessedConsumer3.countDown(); } }).consumerForTopic(ping, new DiscardFailedMessages()); firstMessageProcessedConsumer3.await(); Thread.sleep(5000); // Now shut down the first consumer. consumer1.shutdown(); Thread.sleep(10000); // Stop the producer. produceMessages.set(false); producer.shutdown(); producerExecutor.shutdown(); Thread.sleep(3000); // give the remaining consumer the chance to consume all messages consumer3.shutdown(); // no assignment any longer // Finally, the assertions: int receivedMessagesTotal = receivedMessagesConsumer1.get() + receivedMessagesConsumer2.get() + receivedMessagesConsumer3.get(); assertEquals(sentMessages.get(), receivedMessagesTotal); assertTrue(receivedMessagesConsumer1.get() > 0); assertTrue(receivedMessagesConsumer2.get() > 0); assertTrue(receivedMessagesConsumer3.get() > 0); }