Example usage for java.util.concurrent ExecutorService isShutdown

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

Introduction

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

Prototype

boolean isShutdown();

Source Link

Document

Returns true if this executor has been shut down.

Usage

From source file:com.graphhopper.jsprit.core.algorithm.io.VehicleRoutingAlgorithms.java

private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp,
        XMLConfiguration config, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator,
        final StateManager stateManager, ConstraintManager constraintManager,
        boolean addDefaultCostCalculators) {
    // map to store constructed modules
    TypedMap definedClasses = new TypedMap();

    // algorithm listeners
    Set<PrioritizedVRAListener> algorithmListeners = new HashSet<PrioritizedVRAListener>();

    // insertion listeners
    List<InsertionListener> insertionListeners = new ArrayList<InsertionListener>();

    //threading//from   ww  w.j  a  v a  2  s  . c  om
    final ExecutorService executorService;
    if (nuOfThreads > 0) {
        log.debug("setup executor-service with " + nuOfThreads + " threads");
        executorService = Executors.newFixedThreadPool(nuOfThreads);
        algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() {

            @Override
            public void informAlgorithmEnds(VehicleRoutingProblem problem,
                    Collection<VehicleRoutingProblemSolution> solutions) {
                log.debug("shutdown executor-service");
                executorService.shutdown();
            }
        }));
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread arg0, Throwable arg1) {
                System.err.println(arg1.toString());
            }
        });
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                if (!executorService.isShutdown()) {
                    System.err.println("shutdowHook shuts down executorService");
                    executorService.shutdown();
                }
            }
        });
    } else
        executorService = null;

    //create fleetmanager
    final VehicleFleetManager vehicleFleetManager = createFleetManager(vrp);

    String switchString = config.getString("construction.insertion.allowVehicleSwitch");
    final boolean switchAllowed;
    if (switchString != null) {
        switchAllowed = Boolean.parseBoolean(switchString);
    } else
        switchAllowed = true;
    ActivityTimeTracker.ActivityPolicy activityPolicy;
    if (stateManager.timeWindowUpdateIsActivated()) {
        UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(
                stateManager, vrp.getTransportCosts(), vrp.getActivityCosts());
        timeWindowUpdater
                .setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
                    Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>();

                    @Override
                    public Collection<Vehicle> get(VehicleRoute vehicleRoute) {
                        if (uniqueTypes.isEmpty()) {
                            for (Vehicle v : vrp.getVehicles()) {
                                if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) {
                                    uniqueTypes.put(v.getVehicleTypeIdentifier(), v);
                                }
                            }
                        }
                        Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
                        vehicles.addAll(uniqueTypes.values());
                        return vehicles;
                    }
                });
        stateManager.addStateUpdater(timeWindowUpdater);
        activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
    } else {
        activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED;
    }
    stateManager.addStateUpdater(
            new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts()));
    stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(),
            stateManager, activityPolicy));

    final SolutionCostCalculator costCalculator;
    if (solutionCostCalculator == null)
        costCalculator = getDefaultCostCalculator(stateManager);
    else
        costCalculator = solutionCostCalculator;

    PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager,
            stateManager, constraintManager);
    //construct initial solution creator
    final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager,
            stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator,
            constraintManager, addDefaultCostCalculators);
    if (initialInsertionStrategy != null)
        prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator);

    //construct algorithm, i.e. search-strategies and its modules
    int solutionMemory = config.getInt("strategy.memory");
    List<HierarchicalConfiguration> strategyConfigs = config
            .configurationsAt("strategy.searchStrategies.searchStrategy");
    for (HierarchicalConfiguration strategyConfig : strategyConfigs) {
        String name = getName(strategyConfig);
        SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses,
                solutionMemory);
        SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses);

        SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator);
        strategy.setName(name);
        List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module");
        for (HierarchicalConfiguration moduleConfig : modulesConfig) {
            SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager,
                    algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager,
                    addDefaultCostCalculators);
            strategy.addModule(module);
        }
        prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability"));
    }

    //construct algorithm
    VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build();
    int maxIterations = getMaxIterations(config);
    if (maxIterations > -1)
        metaAlgorithm.setMaxIterations(maxIterations);

    //define prematureBreak
    PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config,
            algorithmListeners);
    if (prematureAlgorithmTermination != null)
        metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination);
    else {
        List<HierarchicalConfiguration> terminationCriteria = config
                .configurationsAt("terminationCriteria.termination");
        for (HierarchicalConfiguration terminationConfig : terminationCriteria) {
            PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig,
                    algorithmListeners);
            if (termination != null)
                metaAlgorithm.addTerminationCriterion(termination);
        }
    }
    for (PrioritizedVRAListener l : algorithmListeners) {
        metaAlgorithm.getAlgorithmListeners().add(l);
    }
    return metaAlgorithm;
}

From source file:jsprit.core.algorithm.io.VehicleRoutingAlgorithms.java

private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp,
        XMLConfiguration config, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator,
        final StateManager stateManager, ConstraintManager constraintManager,
        boolean addDefaultCostCalculators) {
    // map to store constructed modules
    TypedMap definedClasses = new TypedMap();

    // algorithm listeners
    Set<PrioritizedVRAListener> algorithmListeners = new HashSet<PrioritizedVRAListener>();

    // insertion listeners
    List<InsertionListener> insertionListeners = new ArrayList<InsertionListener>();

    //threading/*from w w  w .j  ava 2  s .  c  om*/
    final ExecutorService executorService;
    if (nuOfThreads > 0) {
        log.debug("setup executor-service with " + nuOfThreads + " threads");
        executorService = Executors.newFixedThreadPool(nuOfThreads);
        algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() {

            @Override
            public void informAlgorithmEnds(VehicleRoutingProblem problem,
                    Collection<VehicleRoutingProblemSolution> solutions) {
                log.debug("shutdown executor-service");
                executorService.shutdown();
            }
        }));
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread arg0, Throwable arg1) {
                System.err.println(arg1.toString());
            }
        });
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                if (!executorService.isShutdown()) {
                    System.err.println("shutdowHook shuts down executorService");
                    executorService.shutdown();
                }
            }
        });
    } else
        executorService = null;

    //create fleetmanager
    final VehicleFleetManager vehicleFleetManager = createFleetManager(vrp);

    String switchString = config.getString("construction.insertion.allowVehicleSwitch");
    final boolean switchAllowed;
    if (switchString != null) {
        switchAllowed = Boolean.parseBoolean(switchString);
    } else
        switchAllowed = true;
    ActivityTimeTracker.ActivityPolicy activityPolicy;
    if (stateManager.timeWindowUpdateIsActivated()) {
        UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(
                stateManager, vrp.getTransportCosts());
        timeWindowUpdater
                .setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
                    Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>();

                    @Override
                    public Collection<Vehicle> get(VehicleRoute vehicleRoute) {
                        if (uniqueTypes.isEmpty()) {
                            for (Vehicle v : vrp.getVehicles()) {
                                if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) {
                                    uniqueTypes.put(v.getVehicleTypeIdentifier(), v);
                                }
                            }
                        }
                        Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
                        vehicles.addAll(uniqueTypes.values());
                        return vehicles;
                    }
                });
        stateManager.addStateUpdater(timeWindowUpdater);
        activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
    } else {
        activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED;
    }
    stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy));
    stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(),
            stateManager, activityPolicy));

    final SolutionCostCalculator costCalculator;
    if (solutionCostCalculator == null)
        costCalculator = getDefaultCostCalculator(stateManager);
    else
        costCalculator = solutionCostCalculator;

    PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager,
            stateManager, constraintManager);
    //construct initial solution creator
    final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager,
            stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator,
            constraintManager, addDefaultCostCalculators);
    if (initialInsertionStrategy != null)
        prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator);

    //construct algorithm, i.e. search-strategies and its modules
    int solutionMemory = config.getInt("strategy.memory");
    List<HierarchicalConfiguration> strategyConfigs = config
            .configurationsAt("strategy.searchStrategies.searchStrategy");
    for (HierarchicalConfiguration strategyConfig : strategyConfigs) {
        String name = getName(strategyConfig);
        SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses,
                solutionMemory);
        SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses);

        SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator);
        strategy.setName(name);
        List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module");
        for (HierarchicalConfiguration moduleConfig : modulesConfig) {
            SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager,
                    algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager,
                    addDefaultCostCalculators);
            strategy.addModule(module);
        }
        prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability"));
    }

    //construct algorithm
    VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build();
    int maxIterations = getMaxIterations(config);
    if (maxIterations > -1)
        metaAlgorithm.setMaxIterations(maxIterations);

    //define prematureBreak
    PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config,
            algorithmListeners);
    if (prematureAlgorithmTermination != null)
        metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination);
    else {
        List<HierarchicalConfiguration> terminationCriteria = config
                .configurationsAt("terminationCriteria.termination");
        for (HierarchicalConfiguration terminationConfig : terminationCriteria) {
            PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig,
                    algorithmListeners);
            if (termination != null)
                metaAlgorithm.addTerminationCriterion(termination);
        }
    }
    for (PrioritizedVRAListener l : algorithmListeners) {
        metaAlgorithm.getAlgorithmListeners().add(l);
    }
    return metaAlgorithm;
}

From source file:com.graphhopper.jsprit.io.algorithm.VehicleRoutingAlgorithms.java

private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp,
        XMLConfiguration config, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator,
        final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators,
        boolean addCoreConstraints) {
    // map to store constructed modules
    TypedMap definedClasses = new TypedMap();

    // algorithm listeners
    Set<PrioritizedVRAListener> algorithmListeners = new HashSet<PrioritizedVRAListener>();

    // insertion listeners
    List<InsertionListener> insertionListeners = new ArrayList<InsertionListener>();

    //threading//from ww w . j av a  2s.c  om
    final ExecutorService executorService;
    if (nuOfThreads > 0) {
        log.debug("setup executor-service with " + nuOfThreads + " threads");
        executorService = Executors.newFixedThreadPool(nuOfThreads);
        algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() {

            @Override
            public void informAlgorithmEnds(VehicleRoutingProblem problem,
                    Collection<VehicleRoutingProblemSolution> solutions) {
                log.debug("shutdown executor-service");
                executorService.shutdown();
            }
        }));
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread arg0, Throwable arg1) {
                System.err.println(arg1.toString());
            }
        });
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                if (!executorService.isShutdown()) {
                    System.err.println("shutdowHook shuts down executorService");
                    executorService.shutdown();
                }
            }
        });
    } else
        executorService = null;

    //create fleetmanager
    final VehicleFleetManager vehicleFleetManager = createFleetManager(vrp);

    String switchString = config.getString("construction.insertion.allowVehicleSwitch");
    final boolean switchAllowed;
    if (switchString != null) {
        switchAllowed = Boolean.parseBoolean(switchString);
    } else
        switchAllowed = true;
    ActivityTimeTracker.ActivityPolicy activityPolicy;
    if (stateManager.timeWindowUpdateIsActivated()) {
        UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows(
                stateManager, vrp.getTransportCosts(), vrp.getActivityCosts());
        timeWindowUpdater
                .setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() {
                    Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>();

                    @Override
                    public Collection<Vehicle> get(VehicleRoute vehicleRoute) {
                        if (uniqueTypes.isEmpty()) {
                            for (Vehicle v : vrp.getVehicles()) {
                                if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) {
                                    uniqueTypes.put(v.getVehicleTypeIdentifier(), v);
                                }
                            }
                        }
                        Collection<Vehicle> vehicles = new ArrayList<Vehicle>();
                        vehicles.addAll(uniqueTypes.values());
                        return vehicles;
                    }
                });
        stateManager.addStateUpdater(timeWindowUpdater);
        activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS;
    } else {
        activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED;
    }
    stateManager.addStateUpdater(
            new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts()));
    stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(),
            stateManager, activityPolicy));

    final SolutionCostCalculator costCalculator;
    if (solutionCostCalculator == null)
        costCalculator = getDefaultCostCalculator(stateManager);
    else
        costCalculator = solutionCostCalculator;

    PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager,
            stateManager, constraintManager);
    if (addCoreConstraints)
        prettyAlgorithmBuilder.addCoreStateAndConstraintStuff();
    //construct initial solution creator
    final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager,
            stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator,
            constraintManager, addDefaultCostCalculators);
    if (initialInsertionStrategy != null)
        prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator);

    //construct algorithm, i.e. search-strategies and its modules
    int solutionMemory = config.getInt("strategy.memory");
    List<HierarchicalConfiguration> strategyConfigs = config
            .configurationsAt("strategy.searchStrategies.searchStrategy");
    for (HierarchicalConfiguration strategyConfig : strategyConfigs) {
        String name = getName(strategyConfig);
        SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses,
                solutionMemory);
        SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses);

        SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator);
        strategy.setName(name);
        List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module");
        for (HierarchicalConfiguration moduleConfig : modulesConfig) {
            SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager,
                    algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager,
                    addDefaultCostCalculators);
            strategy.addModule(module);
        }
        prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability"));
    }

    //construct algorithm
    VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build();
    int maxIterations = getMaxIterations(config);
    if (maxIterations > -1)
        metaAlgorithm.setMaxIterations(maxIterations);

    //define prematureBreak
    PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config,
            algorithmListeners);
    if (prematureAlgorithmTermination != null)
        metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination);
    else {
        List<HierarchicalConfiguration> terminationCriteria = config
                .configurationsAt("terminationCriteria.termination");
        for (HierarchicalConfiguration terminationConfig : terminationCriteria) {
            PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig,
                    algorithmListeners);
            if (termination != null)
                metaAlgorithm.addTerminationCriterion(termination);
        }
    }
    for (PrioritizedVRAListener l : algorithmListeners) {
        metaAlgorithm.getAlgorithmListeners().add(l);
    }
    return metaAlgorithm;
}

From source file:org.apache.helix.messaging.handling.HelixTaskExecutor.java

@Override
public void registerMessageHandlerFactory(String type, MessageHandlerFactory factory, int threadpoolSize) {
    if (!factory.getMessageTypes().contains(type)) {
        throw new HelixException(
                "Message factory type mismatch. Type: " + type + ", factory: " + factory.getMessageTypes());
    }/*  ww w  .jav  a2  s . com*/

    _isShuttingDown = false;

    MsgHandlerFactoryRegistryItem newItem = new MsgHandlerFactoryRegistryItem(factory, threadpoolSize);
    MsgHandlerFactoryRegistryItem prevItem = _hdlrFtyRegistry.putIfAbsent(type, newItem);
    if (prevItem == null) {
        ExecutorService newPool = Executors.newFixedThreadPool(threadpoolSize, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "HelixTaskExecutor-message_handle_thread");
            }
        });
        ExecutorService prevExecutor = _executorMap.putIfAbsent(type, newPool);
        if (prevExecutor != null) {
            LOG.warn("Skip creating a new thread pool for type: " + type + ", already existing pool: "
                    + prevExecutor + ", isShutdown: " + prevExecutor.isShutdown());
            newPool.shutdown();
            newPool = null;
        } else {
            _monitor.createExecutorMonitor(type, newPool);
        }
        LOG.info("Registered message handler factory for type: " + type + ", poolSize: " + threadpoolSize
                + ", factory: " + factory + ", pool: " + _executorMap.get(type));
    } else {
        LOG.info("Skip register message handler factory for type: " + type + ", poolSize: " + threadpoolSize
                + ", factory: " + factory + ", already existing factory: " + prevItem.factory());
        newItem = null;
    }
}

From source file:org.apache.helix.messaging.handling.HelixTaskExecutor.java

void init() {
    LOG.info("Init HelixTaskExecutor");

    if (_messageQueueMonitor != null) {
        _messageQueueMonitor.init();//from   w ww .j a v a  2 s. co m
    }

    _isShuttingDown = false;

    // Re-init all existing factories
    for (final String msgType : _hdlrFtyRegistry.keySet()) {
        MsgHandlerFactoryRegistryItem item = _hdlrFtyRegistry.get(msgType);
        ExecutorService newPool = Executors.newFixedThreadPool(item.threadPoolSize(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "HelixTaskExecutor-message_handle_" + msgType);
            }
        });
        ExecutorService prevPool = _executorMap.putIfAbsent(msgType, newPool);
        if (prevPool != null) {
            // Will happen if we register and call init
            LOG.info("Skip init a new thread pool for type: " + msgType + ", already existing pool: " + prevPool
                    + ", isShutdown: " + prevPool.isShutdown());
            newPool.shutdown();
        } else {
            _monitor.createExecutorMonitor(msgType, newPool);
        }
    }
}

From source file:ch.cyberduck.core.s3.S3Path.java

private Future<MultipartPart> submitPart(final BandwidthThrottle throttle, final StreamListener listener,
        final TransferStatus status, final MultipartUpload multipart, final ExecutorService pool,
        final int partNumber, final long offset, final long length) throws ConnectionCanceledException {
    if (pool.isShutdown()) {
        throw new ConnectionCanceledException();
    }//from   w  w w . java 2  s .  co m
    log.info(String.format("Submit part %d to queue", partNumber));
    return pool.submit(new Callable<MultipartPart>() {
        @Override
        public MultipartPart call() throws IOException, ServiceException {
            final Map<String, String> requestParameters = new HashMap<String, String>();
            requestParameters.put("uploadId", multipart.getUploadId());
            requestParameters.put("partNumber", String.valueOf(partNumber));

            InputStream in = null;
            ResponseOutputStream<StorageObject> out = null;
            MessageDigest digest = null;
            try {
                if (!Preferences.instance().getBoolean("s3.upload.metadata.md5")) {
                    // Content-MD5 not set. Need to verify ourselves instad of S3
                    try {
                        digest = MessageDigest.getInstance("MD5");
                    } catch (NoSuchAlgorithmException e) {
                        log.error(e.getMessage());
                    }
                }
                if (null == digest) {
                    log.warn("MD5 calculation disabled");
                    in = getLocal().getInputStream();
                } else {
                    in = new DigestInputStream(getLocal().getInputStream(), digest);
                }
                out = write(new StorageObject(getKey()), length, requestParameters);
                upload(out, in, throttle, listener, offset, length, status);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
            final StorageObject part = out.getResponse();
            if (null != digest) {
                // Obtain locally-calculated MD5 hash
                String hexMD5 = ServiceUtils.toHex(digest.digest());
                getSession().getClient().verifyExpectedAndActualETagValues(hexMD5, part);
            }
            // Populate part with response data that is accessible via the object's metadata
            return new MultipartPart(partNumber, part.getLastModifiedDate(), part.getETag(),
                    part.getContentLength());
        }
    });
}

From source file:org.apache.hadoop.hbase.client.TestHCM.java

@Test
public void testClusterConnection() throws IOException {
    ThreadPoolExecutor otherPool = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), Threads.newDaemonThreadFactory("test-hcm"));

    HConnection con1 = HConnectionManager.createConnection(TEST_UTIL.getConfiguration());
    HConnection con2 = HConnectionManager.createConnection(TEST_UTIL.getConfiguration(), otherPool);
    // make sure the internally created ExecutorService is the one passed
    assertTrue(otherPool == ((HConnectionImplementation) con2).getCurrentBatchPool());

    String tableName = "testClusterConnection";
    TEST_UTIL.createTable(tableName.getBytes(), FAM_NAM).close();
    HTable t = (HTable) con1.getTable(tableName, otherPool);
    // make sure passing a pool to the getTable does not trigger creation of an internal pool
    assertNull("Internal Thread pool should be null", ((HConnectionImplementation) con1).getCurrentBatchPool());
    // table should use the pool passed
    assertTrue(otherPool == t.getPool());
    t.close();/*from   www .  j  av a2  s.  c  o  m*/

    t = (HTable) con2.getTable(tableName);
    // table should use the connectin's internal pool
    assertTrue(otherPool == t.getPool());
    t.close();

    t = (HTable) con2.getTable(Bytes.toBytes(tableName));
    // try other API too
    assertTrue(otherPool == t.getPool());
    t.close();

    t = (HTable) con2.getTable(TableName.valueOf(tableName));
    // try other API too
    assertTrue(otherPool == t.getPool());
    t.close();

    t = (HTable) con1.getTable(tableName);
    ExecutorService pool = ((HConnectionImplementation) con1).getCurrentBatchPool();
    // make sure an internal pool was created
    assertNotNull("An internal Thread pool should have been created", pool);
    // and that the table is using it
    assertTrue(t.getPool() == pool);
    t.close();

    t = (HTable) con1.getTable(tableName);
    // still using the *same* internal pool
    assertTrue(t.getPool() == pool);
    t.close();

    con1.close();
    // if the pool was created on demand it should be closed upon connection close
    assertTrue(pool.isShutdown());

    con2.close();
    // if the pool is passed, it is not closed
    assertFalse(otherPool.isShutdown());
    otherPool.shutdownNow();
}

From source file:org.apache.hadoop.hbase.index.write.TestParalleIndexWriter.java

@SuppressWarnings("unchecked")
@Test//from ww w.j a v a  2s . c  o m
public void testSynchronouslyCompletesAllWrites() throws Exception {
    LOG.info("Starting " + test.getTableNameString());
    LOG.info("Current thread is interrupted: " + Thread.interrupted());
    Abortable abort = new StubAbortable();
    Stoppable stop = Mockito.mock(Stoppable.class);
    ExecutorService exec = Executors.newFixedThreadPool(1);
    Map<ImmutableBytesPtr, HTableInterface> tables = new HashMap<ImmutableBytesPtr, HTableInterface>();
    FakeTableFactory factory = new FakeTableFactory(tables);

    ImmutableBytesPtr tableName = new ImmutableBytesPtr(this.test.getTableName());
    Put m = new Put(row);
    m.add(Bytes.toBytes("family"), Bytes.toBytes("qual"), null);
    Multimap<HTableInterfaceReference, Mutation> indexUpdates = ArrayListMultimap
            .<HTableInterfaceReference, Mutation>create();
    indexUpdates.put(new HTableInterfaceReference(tableName), m);

    HTableInterface table = Mockito.mock(HTableInterface.class);
    final boolean[] completed = new boolean[] { false };
    Mockito.when(table.batch(Mockito.anyList())).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            // just keep track that it was called
            completed[0] = true;
            return null;
        }
    });
    Mockito.when(table.getTableName()).thenReturn(test.getTableName());
    // add the table to the set of tables, so its returned to the writer
    tables.put(tableName, table);

    // setup the writer and failure policy
    ParallelWriterIndexCommitter writer = new ParallelWriterIndexCommitter();
    writer.setup(factory, exec, abort, stop, 1);
    writer.write(indexUpdates);
    assertTrue("Writer returned before the table batch completed! Likely a race condition tripped",
            completed[0]);
    writer.stop(this.test.getTableNameString() + " finished");
    assertTrue("Factory didn't get shutdown after writer#stop!", factory.shutdown);
    assertTrue("ExectorService isn't terminated after writer#stop!", exec.isShutdown());
}

From source file:org.apache.phoenix.hbase.index.write.TestParalleIndexWriter.java

@SuppressWarnings({ "unchecked", "deprecation" })
@Test/* ww w. ja  v  a 2 s. c o  m*/
public void testSynchronouslyCompletesAllWrites() throws Exception {
    LOG.info("Starting " + test.getTableNameString());
    LOG.info("Current thread is interrupted: " + Thread.interrupted());
    Abortable abort = new StubAbortable();
    Stoppable stop = Mockito.mock(Stoppable.class);
    ExecutorService exec = Executors.newFixedThreadPool(1);
    Map<ImmutableBytesPtr, HTableInterface> tables = new HashMap<ImmutableBytesPtr, HTableInterface>();
    FakeTableFactory factory = new FakeTableFactory(tables);

    ImmutableBytesPtr tableName = new ImmutableBytesPtr(this.test.getTableName());
    Put m = new Put(row);
    m.add(Bytes.toBytes("family"), Bytes.toBytes("qual"), null);
    Multimap<HTableInterfaceReference, Mutation> indexUpdates = ArrayListMultimap
            .<HTableInterfaceReference, Mutation>create();
    indexUpdates.put(new HTableInterfaceReference(tableName), m);

    HTableInterface table = Mockito.mock(HTableInterface.class);
    final boolean[] completed = new boolean[] { false };
    Mockito.when(table.batch(Mockito.anyList())).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            // just keep track that it was called
            completed[0] = true;
            return null;
        }
    });
    Mockito.when(table.getTableName()).thenReturn(test.getTableName());
    // add the table to the set of tables, so its returned to the writer
    tables.put(tableName, table);

    // setup the writer and failure policy
    ParallelWriterIndexCommitter writer = new ParallelWriterIndexCommitter(VersionInfo.getVersion());
    writer.setup(factory, exec, abort, stop, 1);
    writer.write(indexUpdates);
    assertTrue("Writer returned before the table batch completed! Likely a race condition tripped",
            completed[0]);
    writer.stop(this.test.getTableNameString() + " finished");
    assertTrue("Factory didn't get shutdown after writer#stop!", factory.shutdown);
    assertTrue("ExectorService isn't terminated after writer#stop!", exec.isShutdown());
}

From source file:org.apache.hadoop.hbase.index.write.TestIndexWriter.java

/**
 * With the move to using a pool of threads to write, we need to ensure that we still block until
 * all index writes for a mutation/batch are completed.
 * @throws Exception on failure/*from   w w w .  j  a  va  2s  . c o  m*/
 */
@SuppressWarnings("unchecked")
@Test
public void testSynchronouslyCompletesAllWrites() throws Exception {
    LOG.info("Starting " + testName.getTableNameString());
    LOG.info("Current thread is interrupted: " + Thread.interrupted());
    Abortable abort = new StubAbortable();
    Stoppable stop = Mockito.mock(Stoppable.class);
    ExecutorService exec = Executors.newFixedThreadPool(1);
    Map<ImmutableBytesPtr, HTableInterface> tables = new HashMap<ImmutableBytesPtr, HTableInterface>();
    FakeTableFactory factory = new FakeTableFactory(tables);

    byte[] tableName = this.testName.getTableName();
    Put m = new Put(row);
    m.add(Bytes.toBytes("family"), Bytes.toBytes("qual"), null);
    Collection<Pair<Mutation, byte[]>> indexUpdates = Arrays.asList(new Pair<Mutation, byte[]>(m, tableName));

    HTableInterface table = Mockito.mock(HTableInterface.class);
    final boolean[] completed = new boolean[] { false };
    Mockito.when(table.batch(Mockito.anyList())).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            // just keep track that it was called
            completed[0] = true;
            return null;
        }
    });
    Mockito.when(table.getTableName()).thenReturn(testName.getTableName());
    // add the table to the set of tables, so its returned to the writer
    tables.put(new ImmutableBytesPtr(tableName), table);

    // setup the writer and failure policy
    ParallelWriterIndexCommitter committer = new ParallelWriterIndexCommitter();
    committer.setup(factory, exec, abort, stop, 2);
    KillServerOnFailurePolicy policy = new KillServerOnFailurePolicy();
    policy.setup(stop, abort);
    IndexWriter writer = new IndexWriter(committer, policy);
    writer.write(indexUpdates);
    assertTrue("Writer returned before the table batch completed! Likely a race condition tripped",
            completed[0]);
    writer.stop(this.testName.getTableNameString() + " finished");
    assertTrue("Factory didn't get shutdown after writer#stop!", factory.shutdown);
    assertTrue("ExectorService isn't terminated after writer#stop!", exec.isShutdown());
}