Example usage for java.util.concurrent Executors newScheduledThreadPool

List of usage examples for java.util.concurrent Executors newScheduledThreadPool

Introduction

In this page you can find the example usage for java.util.concurrent Executors newScheduledThreadPool.

Prototype

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 

Source Link

Document

Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.

Usage

From source file:com.comcast.cqs.test.stress.CqsStressTester.java

private ScheduledExecutorService createSenders(List<String> queueUrls) {

    int senderCount = CQSStressTestProperties.getInstance().getNumberOfSendersPerQueue();

    if (senderCount == 0) {
        return null;
    }// w ww. j a v a 2 s  .  co m

    int numberOfMessagesPerSec = CQSStressTestProperties.getInstance().getMessagesPerQueuePerSecond();
    ScheduledExecutorService scheduledExecutorService = Executors
            .newScheduledThreadPool(queueUrls.size() * senderCount);

    for (String queueUrl : queueUrls) {
        for (int i = 0; i < senderCount; i++) {
            scheduledExecutorService.scheduleWithFixedDelay(new MessageSender(queueUrl, i), rand.nextInt(100),
                    1000 * senderCount / numberOfMessagesPerSec, TimeUnit.MILLISECONDS);
        }
    }

    return scheduledExecutorService;
}

From source file:com.brucegiese.perfectposture.OrientationService.java

/**
 * Start monitoring the user's posture.//from   ww w .j  ava 2  s . co m
 */
private void startChecking() {
    mChinTuckReminderCounter = 0;
    mCurrentPostureGood = true; // start out assuming good posture
    mHysteresisCounter = 0;
    mBadPostureReminderCounter = 0;

    try {
        if (mOrientation.startOrienting()) {
            OrientationService.sIsRunning = true;

            if (mScheduledFuture == null) {
                // We use an additional thread for the periodic execution task.

                ScheduledExecutorService mScheduler = Executors.newScheduledThreadPool(1);
                mScheduledFuture = mScheduler.scheduleAtFixedRate(mDoPeriodicWork, UPDATE_INTERVAL,
                        UPDATE_INTERVAL, TimeUnit.SECONDS);
            } else {
                Log.e(TAG, "startChecking() was called when checking was already running");
            }
        } else {
            Toast.makeText(this, R.string.no_sensors, Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception when starting orientation and scheduler: ", e);
    }

    // Register the broadcast receiver
    IntentFilter iFilter = new IntentFilter();
    iFilter.addAction(TURN_OFF_SERVICE_ACTION);
    LocalBroadcastManager.getInstance(this).registerReceiver(mCommandReceiver, iFilter);
}

From source file:org.geowebcache.storage.blobstore.memory.guava.GuavaCacheProvider.java

/**
 * This method is used for creating a new cache object, from the defined configuration.
 * /*from ww  w.java  2  s  .co  m*/
 * @param configuration
 */
private void initCache(CacheConfiguration configuration) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Building new Cache");
    }
    // Initialization step
    int concurrency = configuration.getConcurrencyLevel();
    maxMemory = configuration.getHardMemoryLimit() * BYTES_TO_MB;
    long evictionTime = configuration.getEvictionTime();
    EvictionPolicy policy = configuration.getPolicy();

    // If Cache already exists, flush it
    if (cache != null) {
        cache.invalidateAll();
    }
    // Create the CacheBuilder
    CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
    // Add weigher
    Weigher<String, TileObject> weigher = new Weigher<String, TileObject>() {

        @Override
        public int weigh(String key, TileObject value) {
            currentSize.addAndGet(value.getBlobSize());
            return value.getBlobSize();
        }
    };
    // Create the builder
    CacheBuilder<String, TileObject> newBuilder = builder.maximumWeight(maxMemory).recordStats()
            .weigher(weigher).concurrencyLevel(concurrency)
            .removalListener(new RemovalListener<String, TileObject>() {

                @Override
                public void onRemoval(RemovalNotification<String, TileObject> notification) {
                    // TODO This operation is not atomic
                    TileObject obj = notification.getValue();
                    // Update the current size
                    currentSize.addAndGet(-obj.getBlobSize());
                    final String tileKey = generateTileKey(obj);
                    final String layerName = obj.getLayerName();
                    multimap.removeTile(layerName, tileKey);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Removed tile " + tileKey + " for layer " + layerName + " due to reason:"
                                + notification.getCause().toString());
                        LOGGER.debug("Removed tile was evicted? " + notification.wasEvicted());
                    }
                }
            });
    // Handle eviction policy
    boolean configuredPolicy = false;
    if (policy != null && evictionTime > 0) {
        if (policy == EvictionPolicy.EXPIRE_AFTER_ACCESS) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Configuring Expire After Access eviction policy");
            }
            newBuilder.expireAfterAccess(evictionTime, TimeUnit.SECONDS);
            configuredPolicy = true;
        } else if (policy == EvictionPolicy.EXPIRE_AFTER_WRITE) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Configuring Expire After Write eviction policy");
            }
            newBuilder.expireAfterWrite(evictionTime, TimeUnit.SECONDS);
            configuredPolicy = true;
        }
    }

    // Build the cache
    cache = newBuilder.build();

    // Created a new multimap
    multimap = new LayerMap();

    // Configure a new scheduling task if needed
    if (configuredPolicy) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Configuring Scheduled Task for cache eviction");
        }
        Runnable command = new Runnable() {

            @Override
            public void run() {
                if (configured.get()) {
                    // Increment the number of current operations
                    // This behavior is used in order to wait
                    // the end of all the operations after setting
                    // the configured parameter to false
                    actualOperations.incrementAndGet();
                    try {
                        cache.cleanUp();
                    } finally {
                        // Decrement the number of current operations.
                        actualOperations.decrementAndGet();
                    }
                }
            }
        };
        // Initialization of the internal Scheduler task for scheduling cache cleanup
        scheduledPool = Executors.newScheduledThreadPool(CORE_POOL_SIZE);
        scheduledPool.scheduleAtFixedRate(command, 10, evictionTime + 1, TimeUnit.SECONDS);
    }

    // Update the configured parameter
    configured.getAndSet(true);
}

From source file:org.eclipse.nebula.widgets.nattable.examples.examples._900_Everything_but_the_kitchen_sink.java

@Override
public void onStart() {
    Display.getDefault().asyncExec(new Runnable() {
        public void run() {
            scheduledThreadPool = Executors.newScheduledThreadPool(1);
            System.out.println("Starting data load.");
            scheduledThreadPool.schedule(new DataLoader(propertyChangeListener, baseEventList), 100L,
                    TimeUnit.MILLISECONDS);
            scheduledThreadPool.scheduleAtFixedRate(new DataUpdater(bodyDataProvider), 100L, 5000L,
                    TimeUnit.MILLISECONDS);
        }//  w  w  w  .  ja  v  a  2  s . c  o  m
    });
}

From source file:com.barchart.netty.server.http.TestHttpServer.java

@Test
public void testShutdown() throws Exception {

    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

    final AtomicBoolean pass = new AtomicBoolean(false);

    final Thread t = new Thread(new Runnable() {

        @Override/*from ww w. j  ava 2  s. c  o m*/
        public void run() {

            try {
                Thread.sleep(1000);
                server.shutdown();
            } catch (final InterruptedException e1) {
                e1.printStackTrace();
            }

            try {
                client.execute(new HttpGet("http://localhost:" + port + "/basic"));
            } catch (final HttpHostConnectException hhce) {
                pass.set(true);
            } catch (final Exception e) {
                e.printStackTrace();
            }

        }

    });

    t.start();

    final HttpGet get = new HttpGet("http://localhost:" + port + "/client-disconnect");
    final HttpResponse response = client.execute(get);
    EntityUtils.consume(response.getEntity());
    assertEquals(200, response.getStatusLine().getStatusCode());

    t.join();

    assertTrue(pass.get());

}

From source file:org.graylog.plugins.pipelineprocessor.functions.FunctionsSnippetsTest.java

@BeforeClass
public static void registerFunctions() {
    final Map<String, Function<?>> functions = commonFunctions();

    functions.put(BooleanConversion.NAME, new BooleanConversion());
    functions.put(DoubleConversion.NAME, new DoubleConversion());
    functions.put(LongConversion.NAME, new LongConversion());
    functions.put(StringConversion.NAME, new StringConversion());

    // message related functions
    functions.put(HasField.NAME, new HasField());
    functions.put(SetField.NAME, new SetField());
    functions.put(SetFields.NAME, new SetFields());
    functions.put(RenameField.NAME, new RenameField());
    functions.put(RemoveField.NAME, new RemoveField());

    functions.put(DropMessage.NAME, new DropMessage());
    functions.put(CreateMessage.NAME, new CreateMessage());

    // route to stream mocks
    final StreamService streamService = mock(StreamService.class);
    final Stream stream = mock(Stream.class);
    when(stream.isPaused()).thenReturn(false);
    when(stream.getTitle()).thenReturn("some name");
    when(stream.getId()).thenReturn("id");
    when(streamService.loadAll()).thenReturn(Lists.newArrayList(stream));

    functions.put(RouteToStream.NAME, new RouteToStream(streamService));

    // input related functions
    // TODO needs mock
    //functions.put(FromInput.NAME, new FromInput());

    // generic functions
    functions.put(RegexMatch.NAME, new RegexMatch());

    // string functions
    functions.put(Abbreviate.NAME, new Abbreviate());
    functions.put(Capitalize.NAME, new Capitalize());
    functions.put(Concat.NAME, new Concat());
    functions.put(Contains.NAME, new Contains());
    functions.put(Lowercase.NAME, new Lowercase());
    functions.put(Substring.NAME, new Substring());
    functions.put(Swapcase.NAME, new Swapcase());
    functions.put(Uncapitalize.NAME, new Uncapitalize());
    functions.put(Uppercase.NAME, new Uppercase());
    functions.put(KeyValue.NAME, new KeyValue());

    final ObjectMapper objectMapper = new ObjectMapperProvider().get();
    functions.put(JsonParse.NAME, new JsonParse(objectMapper));
    functions.put(SelectJsonPath.NAME, new SelectJsonPath(objectMapper));

    functions.put(Now.NAME, new Now());
    functions.put(FlexParseDate.NAME, new FlexParseDate());
    functions.put(ParseDate.NAME, new ParseDate());
    functions.put(FormatDate.NAME, new FormatDate());

    functions.put(Years.NAME, new Years());
    functions.put(Months.NAME, new Months());
    functions.put(Weeks.NAME, new Weeks());
    functions.put(Days.NAME, new Days());
    functions.put(Hours.NAME, new Hours());
    functions.put(Minutes.NAME, new Minutes());
    functions.put(Seconds.NAME, new Seconds());
    functions.put(Millis.NAME, new Millis());
    functions.put(PeriodParseFunction.NAME, new PeriodParseFunction());

    functions.put(CRC32.NAME, new CRC32());
    functions.put(CRC32C.NAME, new CRC32C());
    functions.put(MD5.NAME, new MD5());
    functions.put(Murmur3_32.NAME, new Murmur3_32());
    functions.put(Murmur3_128.NAME, new Murmur3_128());
    functions.put(SHA1.NAME, new SHA1());
    functions.put(SHA256.NAME, new SHA256());
    functions.put(SHA512.NAME, new SHA512());

    functions.put(IpAddressConversion.NAME, new IpAddressConversion());
    functions.put(CidrMatch.NAME, new CidrMatch());

    functions.put(IsNull.NAME, new IsNull());
    functions.put(IsNotNull.NAME, new IsNotNull());

    functions.put(SyslogPriorityConversion.NAME, new SyslogPriorityConversion());
    functions.put(SyslogPriorityToStringConversion.NAME, new SyslogPriorityToStringConversion());
    functions.put(SyslogFacilityConversion.NAME, new SyslogFacilityConversion());
    functions.put(SyslogLevelConversion.NAME, new SyslogLevelConversion());

    functions.put(UrlConversion.NAME, new UrlConversion());

    final GrokPatternService grokPatternService = mock(GrokPatternService.class);
    Set<GrokPattern> patterns = Sets.newHashSet(GrokPattern.create("GREEDY", ".*"),
            GrokPattern.create("BASE10NUM", "(?<![0-9.+-])(?>[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))"),
            GrokPattern.create("NUMBER", "(?:%{BASE10NUM:UNWANTED})"),
            GrokPattern.create("NUM", "%{BASE10NUM}"));
    when(grokPatternService.loadAll()).thenReturn(patterns);
    final EventBus clusterBus = new EventBus();
    final GrokPatternRegistry grokPatternRegistry = new GrokPatternRegistry(clusterBus, grokPatternService,
            Executors.newScheduledThreadPool(1));
    functions.put(GrokMatch.NAME, new GrokMatch(grokPatternRegistry));

    functionRegistry = new FunctionRegistry(functions);
}

From source file:com.barchart.http.server.TestHttpServer.java

@Test(expected = HttpHostConnectException.class)
public void testKill() throws Exception {

    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

    executor.schedule(new Runnable() {

        @Override/* ww w. j  a  v  a  2  s.c o m*/
        public void run() {
            server.kill();
        }

    }, 500, TimeUnit.MILLISECONDS);

    final HttpGet get = new HttpGet("http://localhost:" + port + "/client-disconnect");

    // Should throw exception
    client.execute(get);

}

From source file:org.galicaster.dashboard.DashboardService.java

@SuppressWarnings("unchecked")
protected void activate(ComponentContext cc) throws URISyntaxException {

    // Initialize the object to hold the agent's properties
    agentPasswords = new Properties();

    if ((cc == null) || (cc.getProperties() == null)) {

        mode = MODE_DEFAULT;/*from w  w  w  .  j a v  a 2s. c  om*/
        logger.debug("Setting default functioning mode: {}", mode.toString());

        pingTimeout = PING_TIMEOUT_DEFAULT;
        logger.debug("Setting default ping timeout: {}", pingTimeout);

        snapshotTimeout = SNAPSHOT_TIMEOUT_DEFAULT;
        logger.debug("Setting default snapshot timeout: {}", snapshotTimeout);

        snapshotPeriod = SNAPSHOT_PERIOD_DEFAULT;
        logger.debug("Setting default snapshot period: {}", snapshotPeriod);

        maxConcurrentSnapshots = MAX_CONCURRENT_DEFAULT;
        logger.debug("Setting default maximum concurrent snapshots: {}", maxConcurrentSnapshots);

        defaultPassword = DEFAULT_PASSWORD_DEFAULT;
        logger.debug("Setting default VNC password default: {}", DEFAULT_PASSWORD_DEFAULT);

    } else {

        // Get the component properties
        Dictionary<String, Object> properties = cc.getProperties();

        // Get the functioning mode.
        String strMode = (String) properties.get(MODE_PROPERTY);
        if (strMode != null) {
            try {
                mode = Mode.valueOf(strMode.toUpperCase());
            } catch (IllegalArgumentException iae) {
                logger.warn("Invalid functioning mode found in configuration file: {}", strMode);
                mode = MODE_DEFAULT;
                logger.debug("Setting default functioning mode: {}", mode.toString());
            }
        }

        // Read the properties that control the "snapshot taking" process
        try {
            pingTimeout = Integer.parseInt((String) properties.get(PING_TIMEOUT_PROPERTY));
        } catch (Exception e) {
            pingTimeout = PING_TIMEOUT_DEFAULT;
        }

        try {
            snapshotTimeout = Integer.parseInt((String) properties.get(SNAPSHOT_TIMEOUT_PROPERTY));
        } catch (Exception e) {
            pingTimeout = SNAPSHOT_TIMEOUT_DEFAULT;
        }

        try {
            snapshotPeriod = Long.parseLong((String) properties.get(SNAPSHOT_PERIOD_PROPERTY));
        } catch (Exception e) {
            snapshotPeriod = SNAPSHOT_PERIOD_DEFAULT;
        }

        try {
            maxConcurrentSnapshots = Integer.parseInt((String) properties.get(MAX_CONCURRENT_PROPERTY));
        } catch (Exception e) {
            maxConcurrentSnapshots = MAX_CONCURRENT_DEFAULT;
        }

        // Get the default VNC password, if it is defined in the config file
        defaultPassword = (String) properties.get(DEFAULT_PASSWORD_PROPERTY);
        if (defaultPassword == null)
            defaultPassword = DEFAULT_PASSWORD_DEFAULT;

        // Get the passwords per agent, if specified in the config file
        Pattern propertyPattern = Pattern.compile(PASSWORD_REGEXP);

        for (Enumeration<String> keys = properties.keys(); keys.hasMoreElements();) {
            String key = keys.nextElement();
            Matcher match = propertyPattern.matcher(key);

            if (match.matches()) {
                agentPasswords.setProperty(match.group(1), (String) properties.get(match.group()));
            }
        }
    }

    // If the mode is PULL, set the security information and start the snapshot threads accordingly
    if (mode == Mode.PULL) {

        // Set security information
        this.systemAccount = cc.getBundleContext().getProperty("org.opencastproject.security.digest.user");

        DefaultOrganization defaultOrg = new DefaultOrganization();
        securityService.setOrganization(defaultOrg);
        securityService
                .setUser(new JaxbUser(systemAccount, defaultOrg, new JaxbRole(GLOBAL_ADMIN_ROLE, defaultOrg)));

        // Start the threads that take the snapshots
        periodicSnapshots = Executors.newScheduledThreadPool(maxConcurrentSnapshots);

        Map<String, Agent> knownAgents = captureAgentStateService.getKnownAgents();
        Random randGen = new Random();
        for (Map.Entry<String, Agent> entry : knownAgents.entrySet()) {
            SnapshotWithDeadline task = new SnapshotWithDeadline(entry.getValue(), tempDir,
                    agentPasswords.getProperty(entry.getKey(), defaultPassword), snapshotTimeout,
                    TimeUnit.SECONDS);

            periodicSnapshots.scheduleAtFixedRate(task, randGen.nextLong() % snapshotPeriod, snapshotPeriod,
                    TimeUnit.SECONDS);
        }
    }

    logger.info("Galicaster Service activated");
}

From source file:org.j2free.config.ConfigurationListener.java

/**
 *
 * @param event/*from  w  ww  . ja  va2 s .c o m*/
 */
public synchronized void contextInitialized(ServletContextEvent event) {
    context = event.getServletContext();

    // Get the configuration file
    String configPathTemp = (String) context.getInitParameter(INIT_PARAM_CONFIG_PATH);

    // Use the default path if it wasn't specified
    if (StringUtils.isBlank(configPathTemp))
        configPathTemp = DEFAULT_CONFIG_PATH;

    // Finalize the config path (needs to be final for inner-Runnable below)
    final String configPath = configPathTemp;
    context.setAttribute(CONTEXT_ATTR_CONFIG_PATH, configPath);

    try {
        // Load the configuration
        DefaultConfigurationBuilder configBuilder = new DefaultConfigurationBuilder();
        configBuilder.setFileName(configPath);

        final CombinedConfiguration config = configBuilder.getConfiguration(true);

        // Save the config where we can get at it later
        context.setAttribute(CONTEXT_ATTR_CONFIG, config);
        Global.put(CONTEXT_ATTR_CONFIG, config);

        // Determine the localhost
        String localhost = config.getString(PROP_LOCALHOST, "ip");
        if (localhost.equalsIgnoreCase("ip")) {
            try {
                localhost = InetAddress.getLocalHost().getHostAddress();
                log.info("Using localhost: " + localhost);
            } catch (Exception e) {
                log.warn("Error determining localhost", e);
                localhost = "localhost";
            }
        }

        context.setAttribute(CONTEXT_ATTR_LOCALHOST, localhost);
        Global.put(CONTEXT_ATTR_LOCALHOST, localhost);
        loadedConfigPropKeys.add(CONTEXT_ATTR_LOCALHOST);

        // Set application context attributes for all config properties
        String prop, value;
        Iterator itr = config.getKeys();
        while (itr.hasNext()) {
            prop = (String) itr.next();
            value = config.getString(prop);

            // Anything with the value "localhost" will be set to the IP if possible
            value = (value.equals("localhost") ? localhost : value);

            log.debug("Config: " + prop + " = " + value);
            context.setAttribute(prop, value);
            loadedConfigPropKeys.add(prop);
        }

        // Run Mode configuration
        String runMode = config.getString(PROP_RUNMODE);
        try {
            RUN_MODE = RunMode.valueOf(runMode);
        } catch (Exception e) {
            log.warn("Error setting runmode, invalid value: " + runMode);
        }

        context.setAttribute("devMode", RUN_MODE != RunMode.PRODUCTION);
        loadedConfigPropKeys.add("devMode");

        // Fragment Cache Configuration
        if (config.getBoolean(FragmentCache.Properties.ENABLED, false)) {
            log.info("Enabling fragment caching...");
            FragmentCacheTag.enable();

            // This is expected to be in seconds
            long temp = config.getLong(FragmentCache.Properties.REQUEST_TIMEOUT, -1l);
            if (temp != -1) {
                log.info("Setting FragmentCacheTag request timeout: " + temp);
                FragmentCacheTag.setRequestTimeout(temp);
            }

            // The property is in seconds, but WARNING_COMPUTE_DURATION does NOT use a TimeUnit, so it's in ms
            temp = config.getLong(FragmentCache.Properties.WARNING_DURATION, -1l);
            if (temp != -1) {
                log.info("Setting FragmentCacheTag warning duration: " + temp);
                FragmentCacheTag.setWarningComputeDuration(temp * 1000);
            }

            // Get the fragment cache names
            String[] cacheNames = config.getStringArray(FragmentCache.Properties.ENGINE_NAMES);
            for (String cacheName : cacheNames) {
                String cacheClassName = config
                        .getString(String.format(FragmentCache.Properties.ENGINE_CLASS_TEMPLATE, cacheName));
                try {
                    // Load up the class
                    Class<? extends FragmentCache> cacheClass = (Class<? extends FragmentCache>) Class
                            .forName(cacheClassName);

                    // Look for a constructor that takes a config
                    Constructor<? extends FragmentCache> constructor = null;
                    try {
                        constructor = cacheClass.getConstructor(Configuration.class);
                    } catch (Exception e) {
                    }

                    FragmentCache cache;

                    // If we found the configuration constructor, use it
                    if (constructor != null)
                        cache = constructor.newInstance(config);
                    else {
                        // otherwise use a default no-args constructor
                        log.warn("Could not find a " + cacheClass.getSimpleName()
                                + " constructor that takes a Configuration, defaulting to no-args constructor");
                        cache = cacheClass.newInstance();
                    }

                    // register the cache with the FragmentCacheTag using the config strategy-name, or the engineName
                    // if a strategy-name is not specified
                    log.info("Registering FragmentCache strategy: [name=" + cacheName + ", class="
                            + cacheClass.getName() + "]");
                    FragmentCacheTag.registerStrategy(cacheName, cache);
                } catch (Exception e) {
                    log.error("Error enabling FragmentCache engine: " + cacheName, e);
                }
            }

        } else {
            // Have to call this here, because reconfiguration could turn
            // the cache off after it was previously enabled.
            FragmentCacheTag.disable();
        }

        // For Task execution
        ScheduledExecutorService taskExecutor;

        if (config.getBoolean(PROP_TASK_EXECUTOR_ON, false)) {
            int threads = config.getInt(PROP_TASK_EXECUTOR_THREADS, DEFAULT_TASK_EXECUTOR_THREADS);

            if (threads == 1)
                taskExecutor = Executors.newSingleThreadScheduledExecutor();
            else
                taskExecutor = Executors.newScheduledThreadPool(threads);

            context.setAttribute(CONTEXT_ATTR_TASK_MANAGER, taskExecutor);
            loadedConfigPropKeys.add(CONTEXT_ATTR_TASK_MANAGER);

            Global.put(CONTEXT_ATTR_TASK_MANAGER, taskExecutor);
        } else {
            // Not allowed to shutdown the taskExecutor if dynamic reconfig is enabled
            if (reconfigTask == null) {
                // Shutdown and remove references to the taskManager previously created
                taskExecutor = (ScheduledExecutorService) Global.get(CONTEXT_ATTR_TASK_MANAGER);
                if (taskExecutor != null) {
                    taskExecutor.shutdown(); // will block until all tasks complete
                    taskExecutor = null;
                    Global.remove(CONTEXT_ATTR_TASK_MANAGER);
                }
            } else {
                // We could just log a warning that you can't do this, but the user
                // might not see that, so we're going to refuse to reset a configuration
                // that cannot be loaded in whole successfully.
                throw new ConfigurationException(
                        "Cannot disable task execution service, dynamic reconfiguration is enabled!");
            }
        }

        // Email Service
        if (config.getBoolean(PROP_MAIL_SERVICE_ON, false)) {
            if (!SimpleEmailService.isEnabled()) {
                // Get the SMTP properties
                Properties props = System.getProperties();
                props.put(PROP_SMTP_HOST, config.getString(PROP_SMTP_HOST));
                props.put(PROP_SMTP_PORT, config.getString(PROP_SMTP_PORT));
                props.put(PROP_SMTP_AUTH, config.getString(PROP_SMTP_AUTH));

                Session session;

                if (config.getBoolean(PROP_SMTP_AUTH)) {
                    final String user = config.getString(PROP_SMTP_USER);
                    final String pass = config.getString(PROP_SMTP_PASS);

                    Authenticator auth = new Authenticator() {
                        @Override
                        public PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(user, pass);
                        }
                    };
                    session = Session.getInstance(props, auth);

                } else {
                    session = Session.getInstance(props);
                }

                // Get the global headers
                Iterator headerNames = config.getKeys(PROP_MAIL_HEADER_PREFIX);
                List<KeyValuePair<String, String>> headers = new LinkedList<KeyValuePair<String, String>>();

                String headerName;
                while (headerNames.hasNext()) {
                    headerName = (String) headerNames.next();
                    headers.add(new KeyValuePair<String, String>(headerName, config.getString(headerName)));
                }

                // Initialize the service
                SimpleEmailService.init(session);
                SimpleEmailService.setGlobalHeaders(headers);

                // Set whether we actually send the e-mails
                SimpleEmailService.setDummyMode(config.getBoolean(PROP_MAIL_DUMMY_MODE, false));

                // Set the failure policy
                String policy = config.getString(PROP_MAIL_ERROR_POLICY);
                if (policy != null) {
                    if (policy.equals(VALUE_MAIL_POLICY_DISCARD)) {
                        SimpleEmailService.setErrorPolicy(new SimpleEmailService.DiscardPolicy());
                    } else if (policy.equals(VALUE_MAIL_POLICY_REQUEUE)) {
                        Priority priority = null;
                        try {
                            priority = Priority.valueOf(config.getString(PROP_MAIL_REQUEUE_PRIORITY));
                        } catch (Exception e) {
                            log.warn("Error reading requeue policy priority: "
                                    + config.getString(PROP_MAIL_REQUEUE_PRIORITY, "") + ", using default");
                        }

                        if (priority == null)
                            SimpleEmailService.setErrorPolicy(new SimpleEmailService.RequeuePolicy());
                        else
                            SimpleEmailService.setErrorPolicy(new SimpleEmailService.RequeuePolicy(priority));
                    }
                }

                // Parse templates
                String emailTemplateDir = config.getString(PROP_MAIL_TEMPLATE_DIR);

                // If the template
                if (StringUtils.isBlank(emailTemplateDir))
                    emailTemplateDir = DEFAULT_EMAIL_TEMPLATE_DIR;

                log.debug("Looking for e-mail templates in: " + emailTemplateDir);
                Set<String> templates = context.getResourcePaths(emailTemplateDir);

                // E-mail templates
                if (templates != null && !templates.isEmpty()) {
                    log.debug("Found " + templates.size() + " templates");

                    String key;
                    String defaultTemplate = config.getString(PROP_MAIL_DEFAULT_TEMPLATE, EMPTY);

                    InputStream in;
                    StringBuilder builder;
                    Scanner scanner;

                    try {
                        Template template;
                        String[] parts;

                        ContentType contentType;

                        for (String path : templates) {
                            path = path.trim();
                            parts = path.split("\\.");

                            contentType = ContentType.valueOfExt(parts[1]);

                            try {
                                in = context.getResourceAsStream(path.trim());

                                if (in != null && in.available() > 0) {
                                    scanner = new Scanner(in);
                                    builder = new StringBuilder();

                                    while (scanner.hasNextLine()) {
                                        builder.append(scanner.nextLine());
                                        if (contentType == ContentType.PLAIN) {
                                            builder.append("\n");
                                        }
                                    }

                                    template = new Template(builder.toString(), contentType);

                                    key = parts[0].replace(emailTemplateDir, EMPTY);
                                    SimpleEmailService.registerTemplate(key, template,
                                            key.equals(defaultTemplate));
                                }
                            } catch (IOException ioe) {
                                log.error("Error loading e-mail template: " + path, ioe);
                            }
                        }
                    } catch (Exception e) {
                        log.error("Error loading e-mail templates", e);
                    }
                } else
                    log.debug("No e-mail templates found.");
            }
        } else if (SimpleEmailService.isEnabled()) {
            boolean shutdown = false;
            try {
                shutdown = SimpleEmailService.shutdown(30, TimeUnit.SECONDS);
            } catch (InterruptedException ie) {
                log.warn("Interrupted while shutting down SimpleEmailService");
            }

            if (!shutdown)
                SimpleEmailService.shutdown();
        }

        // QueuedHttpCallService
        if (config.getBoolean(PROP_HTTP_SRVC_ON, false)) {
            if (!SimpleHttpService.isEnabled()) // Don't double init...
            {
                int defaultThreadCount = Runtime.getRuntime().availableProcessors() + 1; // threads to use if unspecified
                SimpleHttpService.init(config.getInt(PROP_HTTP_SRVC_CORE_POOL, defaultThreadCount),
                        config.getInt(PROP_HTTP_SRVC_MAX_POOL, defaultThreadCount),
                        config.getLong(PROP_HTTP_SRVC_POOL_IDLE, DEFAULT_HTTP_SRVC_THREAD_IDLE),
                        config.getInt(PROP_HTTP_SRVC_CONNECT_TOUT, DEFAULT_HTTP_SRVC_CONNECT_TOUT),
                        config.getInt(PROP_HTTP_SRVE_SOCKET_TOUT, DEFAULT_HTTP_SRVE_SOCKET_TOUT));
            }
        } else if (SimpleHttpService.isEnabled()) {
            boolean shutdown = false;
            try {
                // Try to shutdown the service while letting currently waiting tasks complete
                shutdown = SimpleHttpService.shutdown(30, TimeUnit.SECONDS);
            } catch (InterruptedException ie) {
                log.warn("Interrupted while waiting for SimpleHttpService to shutdown");
            }
            if (!shutdown) {
                // But if that doesn't finish in 60 seconds, just cut it off
                int count = SimpleHttpService.shutdown().size();
                log.warn("SimpleHttpService failed to shutdown in 60 seconds, so it was terminated with "
                        + count + " tasks waiting");
            }
        }

        // Spymemcached Client
        if (config.getBoolean(PROP_SPYMEMCACHED_ON, false)) {
            String addresses = config.getString(PROP_SPYMEMCACHED_ADDRESSES);
            if (addresses == null) {
                log.error("Error configuring spymemcached; enabled but no addresses!");
            } else {
                try {
                    // Reflect our way to the constructor, this is all so that the
                    // spymemcached jar does not need to be included in a J2Free app
                    // unless it is actually to be used.
                    Class klass = Class.forName("net.spy.memcached.MemcachedClient");
                    Constructor constructor = klass.getConstructor(List.class);

                    klass = Class.forName("net.spy.memcached.AddrUtil");
                    Method method = klass.getMethod("getAddresses", String.class);

                    Object client = constructor.newInstance(method.invoke(null, addresses));

                    context.setAttribute(CONTEXT_ATTR_SPYMEMCACHED, client);
                    loadedConfigPropKeys.add(CONTEXT_ATTR_SPYMEMCACHED);

                    Global.put(CONTEXT_ATTR_SPYMEMCACHED, client);

                    log.info("Spymemcached client created, connected to " + addresses);
                } catch (Exception e) {
                    log.error("Error creating memcached client [addresses=" + addresses + "]", e);
                }
            }
        } else {
            // If a spymemcached client was previous created
            Object client = Global.get(CONTEXT_ATTR_SPYMEMCACHED);
            if (client != null) {
                try {
                    // Reflect our way to the shutdown method
                    Class klass = Class.forName("net.spy.memcached.MemcachedClient");
                    Method method = klass.getMethod("shutdown");

                    method.invoke(null, client); // and shut it down

                    log.info("Spymemcached client shutdown");
                } catch (Exception e) {
                    log.error("Error shutting down spymemcached client", e);
                }

                // Then remove any references
                Global.remove(CONTEXT_ATTR_SPYMEMCACHED);
                client = null;
            }
        }
    } catch (ConfigurationException ce) {
        log.error("Error configuring app", ce);
    }
}

From source file:org.apache.ambari.server.bootstrap.BSRunner.java

@Override
public void run() {

    if (sshHostInfo.getSshKey() == null || sshHostInfo.getSshKey().equals("")) {
        beforeBootStrap(sshHostInfo);// www  .ja v  a2  s .  c o m
    }

    String hostString = createHostString(sshHostInfo.getHosts());
    String user = sshHostInfo.getUser();
    String userRunAs = sshHostInfo.getUserRunAs();
    if (user == null || user.isEmpty()) {
        user = DEFAULT_USER;
    }
    String command[] = new String[12];
    BSStat stat = BSStat.RUNNING;
    String scriptlog = "";
    try {
        createRunDir();
        if (LOG.isDebugEnabled()) {
            // FIXME needs to be removed later
            // security hole
            LOG.debug("Using ssh key=\"" + sshHostInfo.getSshKey() + "\"");
        }

        String password = sshHostInfo.getPassword();
        if (password != null && !password.isEmpty()) {
            this.passwordFile = new File(this.requestIdDir, "host_pass");
            // TODO : line separator should be changed
            // if we are going to support multi platform server-agent solution
            String lineSeparator = System.getProperty("line.separator");
            password = password + lineSeparator;
            writePasswordFile(password);
        }

        writeSshKeyFile(sshHostInfo.getSshKey());
        /* Running command:
         * script hostlist bsdir user sshkeyfile
         */
        command[0] = this.bsScript;
        command[1] = hostString;
        command[2] = this.requestIdDir.toString();
        command[3] = user;
        command[4] = this.sshKeyFile.toString();
        command[5] = this.agentSetupScript.toString();
        command[6] = this.ambariHostname;
        command[7] = this.clusterOsFamily;
        command[8] = this.projectVersion;
        command[9] = this.serverPort + "";
        command[10] = userRunAs;
        command[11] = (this.passwordFile == null) ? "null" : this.passwordFile.toString();
        LOG.info("Host= " + hostString + " bs=" + this.bsScript + " requestDir=" + requestIdDir + " user="
                + user + " keyfile=" + this.sshKeyFile + " passwordFile " + this.passwordFile + " server="
                + this.ambariHostname + " version=" + projectVersion + " serverPort=" + this.serverPort
                + " userRunAs=" + userRunAs);

        String[] env = new String[] { "AMBARI_PASSPHRASE=" + agentSetupPassword };
        if (this.verbose)
            env = new String[] { env[0], " BS_VERBOSE=\"-vvv\" " };

        if (LOG.isDebugEnabled()) {
            LOG.debug(Arrays.toString(command));
        }

        String bootStrapOutputFilePath = requestIdDir + File.separator + "bootstrap.out";
        String bootStrapErrorFilePath = requestIdDir + File.separator + "bootstrap.err";

        Process process = Runtime.getRuntime().exec(command, env);

        PrintWriter stdOutWriter = null;
        PrintWriter stdErrWriter = null;

        try {
            stdOutWriter = new PrintWriter(bootStrapOutputFilePath);
            stdErrWriter = new PrintWriter(bootStrapErrorFilePath);
            IOUtils.copy(process.getInputStream(), stdOutWriter);
            IOUtils.copy(process.getErrorStream(), stdErrWriter);
        } finally {
            if (stdOutWriter != null)
                stdOutWriter.close();

            if (stdErrWriter != null)
                stdErrWriter.close();
        }

        // Startup a scheduled executor service to look through the logs
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        BSStatusCollector statusCollector = new BSStatusCollector();
        ScheduledFuture<?> handle = scheduler.scheduleWithFixedDelay(statusCollector, 0, 10, TimeUnit.SECONDS);
        LOG.info("Kicking off the scheduler for polling on logs in " + this.requestIdDir);
        try {

            LOG.info("Bootstrap output, log=" + bootStrapErrorFilePath + " " + bootStrapOutputFilePath);
            int exitCode = process.waitFor();
            String outMesg = "";
            String errMesg = "";
            try {
                outMesg = FileUtils.readFileToString(new File(bootStrapOutputFilePath));
                errMesg = FileUtils.readFileToString(new File(bootStrapErrorFilePath));
            } catch (IOException io) {
                LOG.info("Error in reading files ", io);
            }
            scriptlog = outMesg + "\n\n" + errMesg;
            LOG.info("Script log Mesg " + scriptlog);
            if (exitCode != 0) {
                stat = BSStat.ERROR;
            } else {
                stat = BSStat.SUCCESS;
            }

            scheduler.schedule(new BSStatusCollector(), 0, TimeUnit.SECONDS);
            long startTime = System.currentTimeMillis();
            while (true) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Waiting for hosts status to be updated");
                }
                boolean pendingHosts = false;
                BootStrapStatus tmpStatus = bsImpl.getStatus(requestId);
                List<BSHostStatus> hostStatusList = tmpStatus.getHostsStatus();
                if (hostStatusList != null) {
                    for (BSHostStatus status : hostStatusList) {
                        if (status.getStatus().equals("RUNNING")) {
                            pendingHosts = true;
                        }
                    }
                } else {
                    //Failed to get host status, waiting for hosts status to be updated
                    pendingHosts = true;
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Whether hosts status yet to be updated, pending=" + pendingHosts);
                }
                if (!pendingHosts) {
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // continue
                }
                long now = System.currentTimeMillis();
                if (now >= (startTime + 15000)) {
                    LOG.warn("Gave up waiting for hosts status to be updated");
                    break;
                }
            }
        } catch (InterruptedException e) {
            throw new IOException(e);
        } finally {
            handle.cancel(true);
            /* schedule a last update */
            scheduler.schedule(new BSStatusCollector(), 0, TimeUnit.SECONDS);
            scheduler.shutdownNow();
            try {
                scheduler.awaitTermination(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                LOG.info("Interruped while waiting for scheduler");
            }
            process.destroy();
        }
    } catch (IOException io) {
        LOG.info("Error executing bootstrap " + io.getMessage());
        stat = BSStat.ERROR;
    } finally {
        /* get the bstatus */
        BootStrapStatus tmpStatus = bsImpl.getStatus(requestId);
        List<BSHostStatus> hostStatusList = tmpStatus.getHostsStatus();
        if (hostStatusList != null) {
            for (BSHostStatus hostStatus : hostStatusList) {
                if ("FAILED".equals(hostStatus.getStatus())) {
                    stat = BSStat.ERROR;
                    break;
                }
            }
        } else {
            stat = BSStat.ERROR;
        }
        tmpStatus.setLog(scriptlog);
        tmpStatus.setStatus(stat);
        bsImpl.updateStatus(requestId, tmpStatus);
        bsImpl.reset();
        // Remove private ssh key after bootstrap is complete
        try {
            FileUtils.forceDelete(sshKeyFile);
        } catch (IOException io) {
            LOG.warn(io.getMessage());
        }
        if (passwordFile != null) {
            // Remove password file after bootstrap is complete
            try {
                FileUtils.forceDelete(passwordFile);
            } catch (IOException io) {
                LOG.warn(io.getMessage());
            }
        }
        finished();
    }
}