Example usage for java.util.concurrent ExecutorService awaitTermination

List of usage examples for java.util.concurrent ExecutorService awaitTermination

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService awaitTermination.

Prototype

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Usage

From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java

@Test
public void testPublisherConfirmNotReceivedMultiThreads() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);
    when(mockChannel1.getNextPublishSeqNo()).thenReturn(1L, 2L, 3L, 4L);
    when(mockChannel2.getNextPublishSeqNo()).thenReturn(1L, 2L, 3L, 4L);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    PublisherCallbackChannelImpl channel1 = new PublisherCallbackChannelImpl(mockChannel1);
    PublisherCallbackChannelImpl channel2 = new PublisherCallbackChannelImpl(mockChannel2);
    when(mockConnection.createChannel()).thenReturn(channel1).thenReturn(channel2);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherConfirms(true);/*w  ww  . j  av  a2  s.c o m*/
    ccf.setChannelCacheSize(3);
    final RabbitTemplate template = new RabbitTemplate(ccf);

    final AtomicBoolean confirmed = new AtomicBoolean();
    template.setConfirmCallback((correlationData, ack, cause) -> confirmed.set(true));

    // Hold up the first thread so we get two channels
    final CountDownLatch threadLatch = new CountDownLatch(1);
    final CountDownLatch threadSentLatch = new CountDownLatch(1);
    //Thread 1
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> template.execute(channel -> {
        try {
            threadLatch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        template.doSend(channel, "", ROUTE,
                new SimpleMessageConverter().toMessage("message", new MessageProperties()), false,
                new CorrelationData("def"));
        threadSentLatch.countDown();
        return null;
    }));

    // Thread 2
    template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); // channel y
    threadLatch.countDown();
    assertTrue(threadSentLatch.await(5, TimeUnit.SECONDS));
    assertEquals(2, template.getUnconfirmedCount());
    Collection<CorrelationData> unconfirmed = template.getUnconfirmed(-1);
    assertEquals(2, unconfirmed.size());
    assertEquals(0, template.getUnconfirmedCount());
    Set<String> ids = new HashSet<String>();
    Iterator<CorrelationData> iterator = unconfirmed.iterator();
    ids.add(iterator.next().getId());
    ids.add(iterator.next().getId());
    assertTrue(ids.remove("abc"));
    assertTrue(ids.remove("def"));
    assertFalse(confirmed.get());
    DirectFieldAccessor dfa = new DirectFieldAccessor(template);
    Map<?, ?> pendingConfirms = (Map<?, ?>) dfa.getPropertyValue("publisherConfirmChannels");
    assertThat(pendingConfirms.size(), greaterThan(0)); // might use 2 or only 1 channel
    exec.shutdown();
    assertTrue(exec.awaitTermination(10, TimeUnit.SECONDS));
    ccf.destroy();
    assertEquals(0, pendingConfirms.size());
}

From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorTest.java

@Test
public void shouldAllowConcurrentModificationOfGlobals() throws Exception {
    // this test simulates a scenario that likely shouldn't happen - where globals are modified by multiple
    // threads.  globals are created in a synchronized fashion typically but it's possible that someone
    // could do something like this and this test validate that concurrency exceptions don't occur as a
    // result/*from   w w  w  .  j ava  2  s.  c  om*/
    final ExecutorService service = Executors.newFixedThreadPool(8, testingThreadFactory);
    final Bindings globals = new SimpleBindings();
    globals.put("g", -1);
    final GremlinExecutor gremlinExecutor = GremlinExecutor.build().globalBindings(globals).create();

    final AtomicBoolean failed = new AtomicBoolean(false);
    final int max = 512;
    final List<Pair<Integer, List<Integer>>> futures = Collections.synchronizedList(new ArrayList<>(max));
    IntStream.range(0, max).forEach(i -> {
        final int yValue = i * 2;
        final Bindings b = new SimpleBindings();
        b.put("x", i);
        b.put("y", yValue);
        final int zValue = i * -1;

        final String script = "z=" + zValue + ";[x,y,z,g]";
        try {
            service.submit(() -> {
                try {
                    // modify the global in a separate thread
                    gremlinExecutor.getGlobalBindings().put("g", i);
                    gremlinExecutor.getGlobalBindings().put(Integer.toString(i), i);
                    gremlinExecutor.getGlobalBindings().keySet().stream()
                            .filter(s -> i % 2 == 0 && !s.equals("g")).findFirst().ifPresent(globals::remove);
                    final List<Integer> result = (List<Integer>) gremlinExecutor.eval(script, b).get();
                    futures.add(Pair.with(i, result));
                } catch (Exception ex) {
                    failed.set(true);
                }
            });
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    });

    service.shutdown();
    assertThat(service.awaitTermination(60000, TimeUnit.MILLISECONDS), is(true));

    // likely a concurrency exception if it occurs - and if it does then we've messed up because that's what this
    // test is partially designed to protected against.
    assertThat(failed.get(), is(false));

    assertEquals(max, futures.size());
    futures.forEach(t -> {
        assertEquals(t.getValue0(), t.getValue1().get(0));
        assertEquals(t.getValue0() * 2, t.getValue1().get(1).intValue());
        assertEquals(t.getValue0() * -1, t.getValue1().get(2).intValue());
        assertThat(t.getValue1().get(3).intValue(), greaterThan(-1));
    });
}

From source file:org.lilyproject.repository.impl.AbstractSchemaCache.java

/**
 * Refresh the caches and put the cacheWatcher again on the cache
 * invalidation zookeeper-node./*from   ww w.j av a2s .c o  m*/
 */
private void refreshAll() throws InterruptedException, RepositoryException {

    watchPathsForExistence();

    // Set a watch on the parent path, in case everything needs to be
    // refreshed
    try {
        Stat stat = new Stat();
        ZkUtil.getData(zooKeeper, CACHE_INVALIDATION_PATH, parentWatcher, stat);
        if (parentVersion == null || (stat.getVersion() != parentVersion)) {
            // An explicit refresh was triggered
            parentVersion = stat.getVersion();
            bucketVersions.clear();
        }
    } catch (KeeperException e) {
        if (Thread.currentThread().isInterrupted()) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Failed to put parent watcher on " + CACHE_INVALIDATION_PATH + " : thread interrupted");
            }
        } else {
            log.warn("Failed to put parent watcher on " + CACHE_INVALIDATION_PATH, e);
            // Failed to put our watcher.
            // Relying on the ConnectionWatcher to put it again and
            // initialize the caches.
        }
    }

    if (bucketVersions.isEmpty()) {
        // All buckets need to be refreshed

        if (log.isDebugEnabled()) {
            log.debug("Refreshing all types in the schema cache, no bucket versions known yet");
        }
        // Set a watch again on all buckets
        final ExecutorService sixteenThreads = Executors.newFixedThreadPool(50);
        for (final CacheWatcher watcher : cacheWatchers) {
            sixteenThreads.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    String bucketId = watcher.getBucket();
                    String bucketPath = bucketPath(bucketId);
                    Stat stat = new Stat();
                    try {
                        ZkUtil.getData(zooKeeper, bucketPath, watcher, stat);
                        bucketVersions.put(bucketId, stat.getVersion());
                    } catch (KeeperException e) {
                        if (Thread.currentThread().isInterrupted()) {
                            if (log.isDebugEnabled()) {
                                log.debug("Failed to put watcher on bucket " + bucketPath
                                        + " : thread interrupted");
                            }
                        } else {
                            log.warn("Failed to put watcher on bucket " + bucketPath
                                    + " - Relying on connection watcher to reinitialize cache", e);
                            // Failed to put our watcher.
                            // Relying on the ConnectionWatcher to put it again and
                            // initialize the caches.
                        }
                    }

                    return null;
                }
            });
        }
        sixteenThreads.shutdown();
        sixteenThreads.awaitTermination(1, TimeUnit.HOURS);

        // Read all types in one go
        Pair<List<FieldType>, List<RecordType>> types = getTypeManager().getTypesWithoutCache();
        fieldTypesCache.refreshFieldTypes(types.getV1());
        updatedFieldTypes = true;
        recordTypes.refreshRecordTypes(types.getV2());
    } else {
        // Only the changed buckets need to be refreshed.
        // Upon a re-connection event it could be that some updates were
        // missed and the watches were not triggered.
        // By checking the version number of the buckets we know which
        // buckets to refresh.

        Map<String, Integer> newBucketVersions = new HashMap<String, Integer>();
        // Set a watch again on all buckets
        for (CacheWatcher watcher : cacheWatchers) {
            String bucketId = watcher.getBucket();
            String bucketPath = bucketPath(bucketId);
            Stat stat = new Stat();
            try {
                ZkUtil.getData(zooKeeper, bucketPath, watcher, stat);
                Integer oldVersion = bucketVersions.get(bucketId);
                if (oldVersion == null || (oldVersion != stat.getVersion())) {
                    newBucketVersions.put(bucketId, stat.getVersion());
                }
            } catch (KeeperException e) {
                if (Thread.currentThread().isInterrupted()) {
                    if (log.isDebugEnabled()) {
                        log.debug("Failed to put watcher on bucket " + bucketPath + " : thread is interrupted");
                    }
                } else {
                    log.warn("Failed to put watcher on bucket " + bucketPath
                            + " - Relying on connection watcher to reinitialize cache", e);
                    // Failed to put our watcher.
                    // Relying on the ConnectionWatcher to put it again and
                    // initialize the caches.
                }
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Refreshing all types in the schema cache, limiting to buckets"
                    + newBucketVersions.keySet());
        }
        for (Entry<String, Integer> entry : newBucketVersions.entrySet()) {
            bucketVersions.put(entry.getKey(), entry.getValue());
            TypeBucket typeBucket = getTypeManager().getTypeBucketWithoutCache(entry.getKey());
            fieldTypesCache.refreshFieldTypeBucket(typeBucket);
            updatedFieldTypes = true;
            recordTypes.refreshRecordTypeBucket(typeBucket);
        }
    }
}

From source file:org.apache.phoenix.execute.UpsertSelectOverlappingBatchesIT.java

/**
 * Tests that splitting a region is not blocked indefinitely by UPSERT SELECT load
 *///from  ww w .  j  ava  2 s .  co  m
@Test
public void testSplitDuringUpsertSelect() throws Exception {
    int numUpsertSelectRunners = 4;
    ExecutorService exec = Executors.newFixedThreadPool(numUpsertSelectRunners);
    try (Connection conn = driver.connect(url, props)) {
        final UpsertSelectRunner upsertSelectRunner = new UpsertSelectRunner(dataTable, 0, 105, 1);
        // keep running slow upsert selects
        SlowBatchRegionObserver.SLOW_MUTATE = true;
        for (int i = 0; i < numUpsertSelectRunners; i++) {
            exec.submit(new UpsertSelectLooper(upsertSelectRunner));
            Thread.sleep(300);
        }

        // keep trying to split the region
        final HBaseTestingUtility utility = getUtility();
        final Admin admin = utility.getAdmin();
        final TableName dataTN = TableName.valueOf(dataTable);
        assertEquals(1, utility.getHBaseCluster().getRegions(dataTN).size());
        utility.waitFor(60000L, 1000, new Waiter.Predicate<Exception>() {
            @Override
            public boolean evaluate() throws Exception {
                try {
                    List<RegionInfo> regions = admin.getRegions(dataTN);
                    if (regions.size() > 1) {
                        logger.info("Found region was split");
                        return true;
                    }
                    if (regions.size() == 0) {
                        // This happens when region in transition or closed
                        logger.info("No region returned");
                        return false;
                    }
                    ;
                    RegionInfo hRegion = regions.get(0);
                    logger.info("Attempting to split region");
                    admin.splitRegionAsync(hRegion.getRegionName(), Bytes.toBytes(2));
                    return false;
                } catch (NotServingRegionException | DoNotRetryRegionException re) {
                    // during split
                    return false;
                }
            }
        });
    } finally {
        SlowBatchRegionObserver.SLOW_MUTATE = false;
        exec.shutdownNow();
        exec.awaitTermination(60, TimeUnit.SECONDS);
    }
}

From source file:com.gs.collections.impl.parallel.SerialParallelLazyPerformanceTest.java

private void anySatisfy(FastList<Integer> collection, int index, boolean expectedResult) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialAnySatisfyPerformance(collection, PREDICATES_LAMBDA.get(index),
            expectedResult, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables.add(() -> this.basicParallelLazyAnySatisfyPerformance(collection, PREDICATES_LAMBDA.get(index),
            "Lambda", expectedResult, PARALLEL_RUN_COUNT, cores, service));
    runnables.add(() -> this.basicParallelLazyAnySatisfyPerformance(collection, PREDICATES.get(index),
            "Predicate", expectedResult, PARALLEL_RUN_COUNT, cores, service));
    runnables.add(() -> this.basicParallelLazyAnySatisfyPerformance(collection,
            PREDICATES_METHOD_REF.get(index), "MethodRef", expectedResult, PARALLEL_RUN_COUNT, cores, service));
    runnables.add(() -> this.basicJava8ParallelLazyAnySatisfyPerformance(collection, "Lambda",
            JAVA_PREDICATES_LAMBDA.get(index), expectedResult, PARALLEL_RUN_COUNT));
    List<Integer> arrayList = new ArrayList<>(collection);
    runnables.add(() -> this.basicJava8ParallelLazyAnySatisfyPerformance(arrayList, "Lambda",
            JAVA_PREDICATES_LAMBDA.get(index), expectedResult, PARALLEL_RUN_COUNT));
    runnables.add(() -> this.basicJava8ParallelLazyAnySatisfyPerformance(arrayList, "Predicate",
            JAVA_PREDICATES.get(index), expectedResult, PARALLEL_RUN_COUNT));
    runnables.add(() -> this.basicJava8ParallelLazyAnySatisfyPerformance(arrayList, "MethodRef",
            JAVA_PREDICATES_METHOD_REF.get(index), expectedResult, PARALLEL_RUN_COUNT));
    this.shuffleAndRun(runnables);
    service.shutdown();/*from ww w . j  av  a  2  s.  c  o m*/
    try {
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openiam.idm.srvc.synch.srcadapter.RDBMSAdapter.java

@Override
public SyncResponse startSynch(final SynchConfig config, SynchReviewEntity sourceReview,
        final SynchReviewEntity resultReview) {

    log.debug("RDBMS SYNCH STARTED ^^^^^^^^");

    SyncResponse res = new SyncResponse(ResponseStatus.SUCCESS);
    SynchReview review = null;/* ww  w  .ja  v a2  s.  c om*/
    if (sourceReview != null) {
        review = synchReviewDozerConverter.convertToDTO(sourceReview, false);
    }
    LineObject rowHeaderForReport = null;
    InputStream input = null;

    try {
        final ValidationScript validationScript = org.mule.util.StringUtils.isNotEmpty(
                config.getValidationRule()) ? SynchScriptFactory.createValidationScript(config, review) : null;
        final List<TransformScript> transformScripts = SynchScriptFactory.createTransformationScript(config,
                review);
        final MatchObjectRule matchRule = matchRuleFactory.create(config.getCustomMatchRule()); // check if matchRule exists

        if (validationScript == null || transformScripts == null || matchRule == null) {
            res = new SyncResponse(ResponseStatus.FAILURE);
            res.setErrorText("The problem in initialization of RDBMSAdapter, please check validationScript= "
                    + validationScript + ", transformScripts=" + transformScripts + ", matchRule=" + matchRule
                    + " all must be set!");
            res.setErrorCode(ResponseCode.INVALID_ARGUMENTS);
            return res;
        }

        if (sourceReview != null && !sourceReview.isSourceRejected()) {
            return startSynchReview(config, sourceReview, resultReview, validationScript, transformScripts,
                    matchRule);
        }

        if (!connect(config)) {
            SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE);
            resp.setErrorCode(ResponseCode.FAIL_SQL_ERROR);
            return resp;
        }

        java.util.Date lastExec = null;

        if (config.getLastExecTime() != null) {
            lastExec = config.getLastExecTime();
        }
        final String changeLog = config.getQueryTimeField();
        StringBuilder sql = new StringBuilder(config.getQuery());
        // if its incremental synch, then add the change log parameter
        if (config.getSynchType().equalsIgnoreCase("INCREMENTAL")) {
            // execute the query
            if (StringUtils.isNotEmpty(sql.toString()) && (lastExec != null)) {

                String temp = sql.toString().toUpperCase();
                // strip off any trailing semi-colons. Not needed for jbdc
                temp = StringUtils.removeEnd(temp, ";");

                if (temp.contains("WHERE")) {
                    sql.append(" AND ");
                } else {
                    sql.append(" WHERE ");
                }
                sql.append(changeLog).append(" >= ?");
            }
        }

        log.debug("-SYNCH SQL=" + sql.toString());
        log.debug("-last processed record =" + lastExec);

        PreparedStatement ps = con.prepareStatement(sql.toString());
        if (config.getSynchType().equalsIgnoreCase("INCREMENTAL") && (lastExec != null)) {
            ps.setTimestamp(1, new Timestamp(lastExec.getTime()));
        }
        ResultSet rs = ps.executeQuery();

        // get the list of columns
        ResultSetMetaData rsMetadata = rs.getMetaData();
        DatabaseUtil.populateTemplate(rsMetadata, rowHeader);

        //Read Resultset to List
        List<LineObject> results = new LinkedList<LineObject>();
        while (rs.next()) {
            LineObject rowObj = rowHeader.copy();
            DatabaseUtil.populateRowObject(rowObj, rs, changeLog);
            results.add(rowObj);
        }

        // test
        log.debug("Result set contains following number of columns : " + rowHeader.getColumnMap().size());

        // Multithreading
        int allRowsCount = results.size();
        if (allRowsCount > 0) {
            int threadCoount = THREAD_COUNT;
            int rowsInOneExecutors = allRowsCount / threadCoount;
            int remains = rowsInOneExecutors > 0 ? allRowsCount % (rowsInOneExecutors * threadCoount) : 0;
            if (remains != 0) {
                threadCoount++;
            }
            log.debug("Thread count = " + threadCoount + "; Rows in one thread = " + rowsInOneExecutors
                    + "; Remains rows = " + remains);
            System.out.println("Thread count = " + threadCoount + "; Rows in one thread = " + rowsInOneExecutors
                    + "; Remains rows = " + remains);
            List<Future> threadResults = new LinkedList<Future>();
            // store the latest processed record by thread indx
            final Map<String, Timestamp> recentRecordByThreadInx = new HashMap<String, Timestamp>();
            final ExecutorService service = Executors.newCachedThreadPool();
            for (int i = 0; i < threadCoount; i++) {
                final int threadIndx = i;
                final int startIndex = i * rowsInOneExecutors;
                // Start index for current thread
                int shiftIndex = threadCoount > THREAD_COUNT && i == threadCoount - 1 ? remains
                        : rowsInOneExecutors;
                // Part of the rowas that should be processing with this thread
                final List<LineObject> part = results.subList(startIndex, startIndex + shiftIndex);
                threadResults.add(service.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Timestamp mostRecentRecord = proccess(config, resultReview, provService, part,
                                    validationScript, transformScripts, matchRule, resultReview, startIndex);
                            recentRecordByThreadInx.put("Thread_" + threadIndx, mostRecentRecord);
                        } catch (ClassNotFoundException e) {
                            log.error(e);
                            /*
                            synchStartLog.updateSynchAttributes("FAIL", ResponseCode.CLASS_NOT_FOUND.toString(), e.toString());
                            auditHelper.logEvent(synchStartLog);
                            */
                        }
                    }
                }));
                //Give THREAD_DELAY_BEFORE_START seconds time for thread to be UP (load all cache and begin the work)
                Thread.sleep(THREAD_DELAY_BEFORE_START);
            }
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    service.shutdown();
                    try {
                        if (!service.awaitTermination(SHUTDOWN_TIME, TimeUnit.MILLISECONDS)) { //optional *
                            log.warn("Executor did not terminate in the specified time."); //optional *
                            List<Runnable> droppedTasks = service.shutdownNow(); //optional **
                            log.warn("Executor was abruptly shut down. " + droppedTasks.size()
                                    + " tasks will not be executed."); //optional **
                        }
                    } catch (InterruptedException e) {
                        log.error(e);
                        /*
                        synchStartLog.updateSynchAttributes("FAIL", ResponseCode.INTERRUPTED_EXCEPTION.toString(), e.toString());
                        auditHelper.logEvent(synchStartLog);
                        */
                        SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE);
                        resp.setErrorCode(ResponseCode.INTERRUPTED_EXCEPTION);
                    }
                }
            });
            waitUntilWorkDone(threadResults);

        }
    } catch (ClassNotFoundException cnfe) {
        log.error(cnfe);
        res = new SyncResponse(ResponseStatus.FAILURE);
        res.setErrorCode(ResponseCode.CLASS_NOT_FOUND);
        return res;
    } catch (FileNotFoundException fe) {
        fe.printStackTrace();
        log.error(fe);
        //            auditBuilder.addAttribute(AuditAttributeName.DESCRIPTION, "FileNotFoundException: "+fe.getMessage());
        //            auditLogProvider.persist(auditBuilder);
        SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE);
        resp.setErrorCode(ResponseCode.FILE_EXCEPTION);
        log.debug("RDBMS SYNCHRONIZATION COMPLETE WITH ERRORS ^^^^^^^^");
        return resp;
    } catch (IOException io) {
        io.printStackTrace();
        /*
        synchStartLog.updateSynchAttributes("FAIL", ResponseCode.IO_EXCEPTION.toString(), io.toString());
        auditHelper.logEvent(synchStartLog);
        */
        SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE);
        resp.setErrorCode(ResponseCode.IO_EXCEPTION);
        log.debug("RDBMS SYNCHRONIZATION COMPLETE WITH ERRORS ^^^^^^^^");
        return resp;

    } catch (SQLException se) {

        log.error(se);
        closeConnection();
        /*
        synchStartLog.updateSynchAttributes("FAIL", ResponseCode.SQL_EXCEPTION.toString(), se.toString());
        auditHelper.logEvent(synchStartLog);
        */
        SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE);
        resp.setErrorCode(ResponseCode.SQL_EXCEPTION);
        resp.setErrorText(se.toString());
        return resp;

    } catch (InterruptedException e) {
        log.error(e);
        SyncResponse resp = new SyncResponse(ResponseStatus.FAILURE);
        resp.setErrorCode(ResponseCode.INTERRUPTED_EXCEPTION);

    } finally {
        if (resultReview != null) {
            if (CollectionUtils.isNotEmpty(resultReview.getReviewRecords())) { // add header row
                resultReview.addRecord(generateSynchReviewRecord(rowHeader, true));
            }
        }

        closeConnection();
    }

    log.debug("RDBMS SYNCH COMPLETE.^^^^^^^^");
    return new SyncResponse(ResponseStatus.SUCCESS);

}

From source file:org.eclipse.collections.impl.parallel.SerialParallelLazyPerformanceTest.java

private void forEach(FastList<Integer> collection) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialForEachPerformance(collection, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables.add(() -> {/*from   w ww  .ja  va  2s. co m*/
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE),
                PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Procedure",
                (Procedure<Integer>) each -> map.put(each, Boolean.TRUE), PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE),
                PARALLEL_RUN_COUNT);
    });
    List<Integer> arrayList = new ArrayList<>(collection);
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Lambda", item -> map.put(item, Boolean.TRUE),
                PARALLEL_RUN_COUNT);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Consumer",
                each -> map.put(each, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    this.shuffleAndRun(runnables);
    service.shutdown();
    try {
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.hadoop.hbase.regionserver.TestHStore.java

@Test
public void testScanWithDoubleFlush() throws IOException {
    Configuration conf = HBaseConfiguration.create();
    // Initialize region
    MyStore myStore = initMyStore(name.getMethodName(), conf, new MyStoreHook() {
        @Override/*  ww  w.ja v a  2 s.c om*/
        public void getScanners(MyStore store) throws IOException {
            final long tmpId = id++;
            ExecutorService s = Executors.newSingleThreadExecutor();
            s.submit(() -> {
                try {
                    // flush the store before storescanner updates the scanners from store.
                    // The current data will be flushed into files, and the memstore will
                    // be clear.
                    // -- phase (4/4)
                    flushStore(store, tmpId);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            s.shutdown();
            try {
                // wait for the flush, the thread will be blocked in HStore#notifyChangedReadersObservers.
                s.awaitTermination(3, TimeUnit.SECONDS);
            } catch (InterruptedException ex) {
            }
        }
    });
    byte[] oldValue = Bytes.toBytes("oldValue");
    byte[] currentValue = Bytes.toBytes("currentValue");
    MemStoreSize memStoreSize = new MemStoreSize();
    long ts = EnvironmentEdgeManager.currentTime();
    long seqId = 100;
    // older data whihc shouldn't be "seen" by client
    myStore.add(createCell(qf1, ts, seqId, oldValue), memStoreSize);
    myStore.add(createCell(qf2, ts, seqId, oldValue), memStoreSize);
    myStore.add(createCell(qf3, ts, seqId, oldValue), memStoreSize);
    long snapshotId = id++;
    // push older data into snapshot -- phase (1/4)
    StoreFlushContext storeFlushCtx = store.createFlushContext(snapshotId);
    storeFlushCtx.prepare();

    // insert current data into active -- phase (2/4)
    myStore.add(createCell(qf1, ts + 1, seqId + 1, currentValue), memStoreSize);
    myStore.add(createCell(qf2, ts + 1, seqId + 1, currentValue), memStoreSize);
    myStore.add(createCell(qf3, ts + 1, seqId + 1, currentValue), memStoreSize);
    TreeSet<byte[]> quals = new TreeSet<>(Bytes.BYTES_COMPARATOR);
    quals.add(qf1);
    quals.add(qf2);
    quals.add(qf3);
    try (InternalScanner scanner = (InternalScanner) myStore.getScanner(new Scan(new Get(row)), quals,
            seqId + 1)) {
        // complete the flush -- phase (3/4)
        storeFlushCtx.flushCache(Mockito.mock(MonitoredTask.class));
        storeFlushCtx.commit(Mockito.mock(MonitoredTask.class));

        List<Cell> results = new ArrayList<>();
        scanner.next(results);
        assertEquals(3, results.size());
        for (Cell c : results) {
            byte[] actualValue = CellUtil.cloneValue(c);
            assertTrue("expected:" + Bytes.toStringBinary(currentValue) + ", actual:"
                    + Bytes.toStringBinary(actualValue), Bytes.equals(actualValue, currentValue));
        }
    }
}

From source file:org.alfresco.repo.batch.BatchProcessor.java

/**
 * Invokes the worker for each entry in the collection, managing transactions and collating success / failure
 * information./*w  w  w.  ja  v  a  2s. co  m*/
 * 
 * @param worker
 *            the worker
 * @param splitTxns
 *            Can the modifications to Alfresco be split across multiple transactions for maximum performance? If
 *            <code>true</code>, worker invocations are isolated in separate transactions in batches for
 *            increased performance. If <code>false</code>, all invocations are performed in the current
 *            transaction. This is required if calling synchronously (e.g. in response to an authentication event in
 *            the same transaction).
 * @return the number of invocations
 */
@SuppressWarnings("serial")
public int process(final BatchProcessWorker<T> worker, final boolean splitTxns) {
    int count = workProvider.getTotalEstimatedWorkSize();
    synchronized (this) {
        this.startTime = new Date();
        if (this.logger.isInfoEnabled()) {
            if (count >= 0) {
                this.logger.info(getProcessName() + ": Commencing batch of " + count + " entries");
            } else {
                this.logger.info(getProcessName() + ": Commencing batch");

            }
        }
    }

    // Create a thread pool executor with the specified number of threads and a finite blocking queue of jobs
    ExecutorService executorService = splitTxns && this.workerThreads > 1
            ? new ThreadPoolExecutor(this.workerThreads, this.workerThreads, 0L, TimeUnit.MILLISECONDS,
                    new ArrayBlockingQueue<Runnable>(this.workerThreads * this.batchSize * 10) {
                        // Add blocking behaviour to work queue
                        @Override
                        public boolean offer(Runnable o) {
                            try {
                                put(o);
                            } catch (InterruptedException e) {
                                return false;
                            }
                            return true;
                        }

                    }, threadFactory)
            : null;
    try {
        Iterator<T> iterator = new WorkProviderIterator<T>(this.workProvider);
        int id = 0;
        List<T> batch = new ArrayList<T>(this.batchSize);
        while (iterator.hasNext()) {
            batch.add(iterator.next());
            boolean hasNext = iterator.hasNext();
            if (batch.size() >= this.batchSize || !hasNext) {
                final TxnCallback callback = new TxnCallback(id++, worker, batch, splitTxns);
                if (hasNext) {
                    batch = new ArrayList<T>(this.batchSize);
                }

                if (executorService == null) {
                    callback.run();
                } else {
                    executorService.execute(callback);
                }
            }
        }
        return count;
    } finally {
        if (executorService != null) {
            executorService.shutdown();
            try {
                executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
            }
        }
        synchronized (this) {
            reportProgress(true);
            this.endTime = new Date();
            if (this.logger.isInfoEnabled()) {
                if (count >= 0) {
                    this.logger.info(getProcessName() + ": Completed batch of " + count + " entries");
                } else {
                    this.logger.info(getProcessName() + ": Completed batch");

                }
            }
            if (this.totalErrors > 0 && this.logger.isErrorEnabled()) {
                this.logger.error(
                        getProcessName() + ": " + this.totalErrors
                                + " error(s) detected. Last error from entry \"" + this.lastErrorEntryId + "\"",
                        this.lastError);
            }
        }
    }
}

From source file:pt.ua.tm.neji.batch.FileBatchExecutor.java

private int processFiles(final String inputFolderPath, final String inputWildcardFilter,
        final String outputFolderPath, final int numThreads, Context context,
        final Class<? extends Processor> processorCls, Object... args) {

    int filesProcessed = 0;
    File inputFolder = new File(inputFolderPath);
    FileFilter fileFilter = newFileFilter(inputWildcardFilter, compressed);
    File[] files = inputFolder.listFiles(fileFilter);

    logger.info("Starting thread pool with support for {} threads...", numThreads);
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);

    LinkedList<Future> futures = new LinkedList<>();

    for (File file : files) {

        // Make corpus, output file
        Corpus corpus = new Corpus();

        // By default, the corpus identifier is the file name
        corpus.setIdentifier(FilenameUtils.getBaseName(file.getName()));

        // Make in/out corpus wrappers
        InputFile inputFile = new InputFile(corpus, file, compressed);
        List<OutputFile> outputFiles = new ArrayList<>();
        for (OutputFormat outputFormat : context.getConfiguration().getOutputFormats()) {
            File outFile = OutputFile.newOutputFile(outputFolderPath,
                    FilenameUtils.getBaseName(FilenameUtils.getBaseName(file.getName())), outputFormat,
                    compressed);/*from  www.j a  v  a 2s . c o m*/
            outputFiles.add(new OutputFile(corpus, outFile, compressed));
        }

        if (storeDocuments) {
            processedCorpora.add(corpus);
        }

        Processor processor;
        try {
            processor = newProcessor(processorCls, context, inputFile, outputFiles, addAnnotationsWithoutIDs,
                    args);
        } catch (NejiException ex) {
            String m = "There was a problem creating the processor of the file: " + file.getAbsolutePath();
            logger.error(m, ex);
            throw new RuntimeException(m, ex);
        }

        Future submit = executor.submit(processor);
        futures.add(submit);
    }

    logger.info("");
    logger.info("{} file(s) to process.", futures.size());
    logger.info("Started processing...");

    Iterator<Future> it = futures.iterator();
    while (it.hasNext()) {
        Future future = it.next();
        try {
            Object o = future.get();
            future = null;
            it.remove();
            filesProcessed++;
        } catch (ExecutionException | InterruptedException ex) {
            String m = "There was a problem running the processor.";
            logger.error(m, ex);
        }
    }

    executor.shutdown();
    try {
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    } catch (InterruptedException ex) {
        String m = "There was a problem executing the processing tasks.";
        logger.error(m, ex);
        throw new RuntimeException(m, ex);
    }

    return filesProcessed;
}