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.coinblesk.payments.WalletService.java

private void schedulePeerGroupShutdown() {
    if (!appKitInitDone) {
        return;//from   w w  w .j  av  a2 s  .c  om
    }
    scheduledPeerGroupShutdown = Executors.newScheduledThreadPool(1);
    ScheduledFuture shutdown = scheduledPeerGroupShutdown.schedule(new Runnable() {
        @Override
        public void run() {
            stopPeerGroup();
        }
    }, 15, TimeUnit.SECONDS);
}

From source file:org.wso2.carbon.core.clustering.hazelcast.HazelcastClusteringAgent.java

public void init() throws ClusteringFault {
    MemberUtils.init(parameters, configurationContext);

    primaryHazelcastConfig = new Config();
    setHazelcastProperties();//  w w  w  .  ja  v a2s .c  o m

    Parameter managementCenterURL = getParameter(HazelcastConstants.MGT_CENTER_URL);
    if (managementCenterURL != null) {
        primaryHazelcastConfig.getManagementCenterConfig().setEnabled(true)
                .setUrl((String) managementCenterURL.getValue());
    }

    Parameter licenseKey = getParameter(HazelcastConstants.LICENSE_KEY);
    if (licenseKey != null) {
        primaryHazelcastConfig.setLicenseKey((String) licenseKey.getValue());
    }

    primaryDomain = getClusterDomain();
    primaryHazelcastConfig.setInstanceName(primaryDomain + ".instance");
    log.info("Cluster domain: " + primaryDomain);
    GroupConfig groupConfig = primaryHazelcastConfig.getGroupConfig();
    groupConfig.setName(primaryDomain);
    Parameter memberPassword = getParameter(HazelcastConstants.GROUP_PASSWORD);
    if (memberPassword != null) {
        groupConfig.setPassword((String) memberPassword.getValue());
    }

    NetworkConfig nwConfig = primaryHazelcastConfig.getNetworkConfig();
    Parameter localMemberHostParam = getParameter(HazelcastConstants.LOCAL_MEMBER_HOST);
    String localMemberHost = "";
    if (localMemberHostParam != null) {
        localMemberHost = ((String) localMemberHostParam.getValue()).trim();
        if ("127.0.0.1".equals(localMemberHost) || "localhost".equals(localMemberHost)) {
            log.warn("localMemberHost is configured to use the loopback address. "
                    + "Hazelcast Clustering needs ip addresses for localMemberHost and well-known members.");
        }
    } else {
        try {
            localMemberHost = Utils.getIpAddress();
        } catch (SocketException e) {
            log.error("Could not set local member host", e);
        }
    }
    nwConfig.setPublicAddress(localMemberHost);
    int localMemberPort = 4000;
    Parameter localMemberPortParam = getParameter(HazelcastConstants.LOCAL_MEMBER_PORT);
    if (localMemberPortParam != null) {
        localMemberPort = Integer.parseInt(((String) localMemberPortParam.getValue()).trim());
    }
    nwConfig.setPort(localMemberPort);

    configureMembershipScheme(nwConfig);
    MapConfig mapConfig = new MapConfig("carbon-map-config");
    mapConfig.setEvictionPolicy(MapConfig.DEFAULT_EVICTION_POLICY);
    if (licenseKey != null) {
        mapConfig.setInMemoryFormat(InMemoryFormat.BINARY);
    }
    primaryHazelcastConfig.addMapConfig(mapConfig);
    loadCustomHazelcastSerializers();

    if (clusterManagementMode) {
        for (Map.Entry<String, Map<String, GroupManagementAgent>> entry : groupManagementAgents.entrySet()) {
            for (GroupManagementAgent agent : entry.getValue().values()) {
                if (agent instanceof HazelcastGroupManagementAgent) {
                    ((HazelcastGroupManagementAgent) agent).init(primaryHazelcastConfig, configurationContext);
                }
            }
        }
    }
    long start = System.currentTimeMillis();
    primaryHazelcastInstance = Hazelcast.newHazelcastInstance(primaryHazelcastConfig);
    log.info("Hazelcast initialized in " + (System.currentTimeMillis() - start) + "ms");
    HazelcastCarbonClusterImpl hazelcastCarbonCluster = new HazelcastCarbonClusterImpl(
            primaryHazelcastInstance);

    membershipScheme.setPrimaryHazelcastInstance(primaryHazelcastInstance);
    membershipScheme.setCarbonCluster(hazelcastCarbonCluster);

    clusteringMessageTopic = primaryHazelcastInstance.getTopic(HazelcastConstants.CLUSTERING_MESSAGE_TOPIC);
    clusteringMessageTopic.addMessageListener(
            new HazelcastClusterMessageListener(configurationContext, recdMsgsBuffer, sentMsgsBuffer));
    groupManagementTopic = primaryHazelcastInstance.getTopic(HazelcastConstants.GROUP_MGT_CMD_TOPIC);
    groupManagementTopic.addMessageListener(new GroupManagementCommandListener(configurationContext));
    ITopic<ControlCommand> controlCommandTopic = primaryHazelcastInstance
            .getTopic(HazelcastConstants.CONTROL_COMMAND_TOPIC);
    controlCommandTopic.addMessageListener(new HazelcastControlCommandListener(configurationContext));

    Member localMember = primaryHazelcastInstance.getCluster().getLocalMember();
    membershipScheme.setLocalMember(localMember);
    membershipScheme.joinGroup();
    localMember = primaryHazelcastInstance.getCluster().getLocalMember();
    localMember.getInetSocketAddress().getPort();
    final org.apache.axis2.clustering.Member carbonLocalMember = MemberUtils.getLocalMember(primaryDomain,
            localMember.getInetSocketAddress().getAddress().getHostAddress(),
            localMember.getInetSocketAddress().getPort());
    log.info("Local member: [" + localMember.getUuid() + "] - " + carbonLocalMember);

    //Create a Queue for receiving messages from others
    final ITopic<ClusterMessage> replayedMsgs = primaryHazelcastInstance
            .getTopic(HazelcastConstants.REPLAY_MESSAGE_QUEUE + localMember.getUuid());
    replayedMsgs.addMessageListener(new MessageListener<ClusterMessage>() {

        @Override
        public void onMessage(Message<ClusterMessage> clusterMessage) {
            ClusterMessage msg = clusterMessage.getMessageObject();
            // check UUID to eliminate duplicates
            if (!recdMsgsBuffer.containsKey(msg.getUuid())) {
                log.info("Received replayed message: " + msg.getUuid());
                msg.execute();
                recdMsgsBuffer.put(msg.getUuid(), System.currentTimeMillis());
            }
        }
    });

    if (carbonLocalMember.getProperties().get("subDomain") == null) {
        carbonLocalMember.getProperties().put("subDomain", "__$default"); // Set the default subDomain
    }
    MemberUtils.getMembersMap(primaryHazelcastInstance, primaryDomain).put(localMember.getUuid(),
            carbonLocalMember);

    // To receive membership events required for the leader election algorithm.
    primaryHazelcastInstance.getCluster().addMembershipListener(new CoordinatorElectionMembershipListener());

    //-- Cluster coordinator election algorithm implementation starts here.
    // Hazelcast Community confirms that the list of members is consistent across a given cluster.  Also the first
    // member of the member list you get from primaryHazelcastInstance.getCluster().getMembers() is consistent
    // across the cluster and first member usually is the oldest member.  Therefore we can safely assume first
    // member as the coordinator node.

    // Now this distributed lock is used to correctly identify the coordinator node during the member startup. The
    // node which acquires the lock checks whether it is the oldest member in the cluster. If it is the oldest
    // member then it elects itself as the coordinator node and then release the lock. If it is not the oldest
    // member them simply release the lock. This distributed lock is used to avoid any race conditions.
    ILock lock = primaryHazelcastInstance.getLock(HazelcastConstants.CLUSTER_COORDINATOR_LOCK);

    try {
        log.debug("Trying to get the CLUSTER_COORDINATOR_LOCK lock.");

        lock.lock();
        log.debug("Acquired the CLUSTER_COORDINATOR_LOCK lock.");

        Member oldestMember = primaryHazelcastInstance.getCluster().getMembers().iterator().next();
        if (oldestMember.localMember() && !isCoordinator) {
            electCoordinatorNode();
        }
    } finally {
        lock.unlock();
        log.debug("Released the CLUSTER_COORDINATOR_LOCK lock.");
    }
    //-- Coordinator election algorithm ends here.

    BundleContext bundleContext = CarbonCoreDataHolder.getInstance().getBundleContext();
    bundleContext.registerService(DistributedMapProvider.class,
            new HazelcastDistributedMapProvider(primaryHazelcastInstance), null);
    bundleContext.registerService(HazelcastInstance.class, primaryHazelcastInstance, null);
    bundleContext.registerService(CarbonCluster.class, hazelcastCarbonCluster, null);
    ScheduledExecutorService msgCleanupScheduler = Executors.newScheduledThreadPool(1);
    msgCleanupScheduler.scheduleWithFixedDelay(new ClusterMessageCleanupTask(), 2, 2, TimeUnit.MINUTES);

    log.info("Cluster initialization completed");
}

From source file:com.yahoo.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl.java

public SimpleLoadManagerImpl(PulsarService pulsar) {
    this.policies = new SimpleResourceAllocationPolicies(pulsar);
    this.sortedRankings.set(new TreeMap<>());
    this.currentLoadReports = new HashMap<>();
    this.resourceUnitRankings = new HashMap<>();
    this.loadBalancingMetrics.set(Lists.newArrayList());
    this.realtimeResourceQuotas.set(new HashMap<>());
    this.realtimeAvgResourceQuota = new ResourceQuota();
    placementStrategy = new WRRPlacementStrategy();
    lastLoadReport = new LoadReport(pulsar.getWebServiceAddress(), pulsar.getWebServiceAddressTls(),
            pulsar.getBrokerServiceUrl(), pulsar.getBrokerServiceUrlTls());
    if (SystemUtils.IS_OS_LINUX) {
        brokerHostUsage = new LinuxBrokerHostUsageImpl(pulsar);
    } else {/*from ww  w  .  j  av  a 2s.co m*/
        brokerHostUsage = new GenericBrokerHostUsageImpl(pulsar);
    }
    loadReportCacheZk = new ZooKeeperDataCache<LoadReport>(pulsar.getLocalZkCache()) {
        @Override
        public LoadReport deserialize(String key, byte[] content) throws Exception {
            return ObjectMapperFactory.getThreadLocal().readValue(content, LoadReport.class);
        }
    };
    loadReportCacheZk.registerListener(this);
    this.dynamicConfigurationCache = new ZooKeeperDataCache<Map<String, String>>(pulsar.getLocalZkCache()) {
        @Override
        public Map<String, String> deserialize(String key, byte[] content) throws Exception {
            return ObjectMapperFactory.getThreadLocal().readValue(content, HashMap.class);
        }
    };
    adminCache = CacheBuilder.newBuilder().removalListener(new RemovalListener<String, PulsarAdmin>() {
        public void onRemoval(RemovalNotification<String, PulsarAdmin> removal) {
            removal.getValue().close();
        }
    }).expireAfterAccess(1, TimeUnit.DAYS).build(new CacheLoader<String, PulsarAdmin>() {
        @Override
        public PulsarAdmin load(String key) throws Exception {
            // key - broker name already is valid URL, has prefix "http://"
            return new PulsarAdmin(new URL(key),
                    pulsar.getConfiguration().getBrokerClientAuthenticationPlugin(),
                    pulsar.getConfiguration().getBrokerClientAuthenticationParameters());
        }
    });
    int entryExpiryTime = (int) pulsar.getConfiguration().getLoadBalancerSheddingGracePeriodMinutes();
    unloadedHotNamespaceCache = CacheBuilder.newBuilder().expireAfterWrite(entryExpiryTime, TimeUnit.MINUTES)
            .build(new CacheLoader<String, Long>() {
                @Override
                public Long load(String key) throws Exception {
                    return System.currentTimeMillis();
                }
            });
    availableActiveBrokers = new ZooKeeperChildrenCache(pulsar.getLocalZkCache(), LOADBALANCE_BROKERS_ROOT);
    availableActiveBrokers.registerListener(new ZooKeeperCacheListener<Set<String>>() {
        @Override
        public void onUpdate(String path, Set<String> data, Stat stat) {
            if (log.isDebugEnabled()) {
                log.debug("Update Received for path {}", path);
            }
            scheduler.submit(SimpleLoadManagerImpl.this::updateRanking);
        }
    });
    scheduler = Executors.newScheduledThreadPool(1);
    this.pulsar = pulsar;
}

From source file:com.vmware.photon.controller.core.Main.java

private static PhotonControllerXenonHost startXenonHost(PhotonControllerConfig photonControllerConfig,
        ThriftModule thriftModule, DeployerConfig deployerConfig, SSLContext sslContext) throws Throwable {
    // Values for CloudStore
    final HostClientFactory hostClientFactory = thriftModule.getHostClientFactory();
    final AgentControlClientFactory agentControlClientFactory = thriftModule.getAgentControlClientFactory();
    final NsxClientFactory nsxClientFactory = new NsxClientFactory();

    // Values for Scheduler
    final ServerSet cloudStoreServerSet = new StaticServerSet(
            new InetSocketAddress(photonControllerConfig.getXenonConfig().getRegistrationAddress(),
                    Constants.PHOTON_CONTROLLER_PORT));
    final CloudStoreHelper cloudStoreHelper = new CloudStoreHelper(cloudStoreServerSet);

    final CloseableHttpAsyncClient httpClient;
    try {/*from ww w .  ja v a 2s .  c  om*/
        SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial((chain, authtype) -> true).build();
        httpClient = HttpAsyncClientBuilder.create()
                .setHostnameVerifier(SSLIOSessionStrategy.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLContext(sslcontext)
                .build();
        httpClient.start();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }

    ServerSet apiFeServerSet = new StaticServerSet(new InetSocketAddress(
            photonControllerConfig.getXenonConfig().getRegistrationAddress(), Constants.MANAGEMENT_API_PORT));

    logger.info("Creating PhotonController Xenon Host");
    final PhotonControllerXenonHost photonControllerXenonHost = new PhotonControllerXenonHost(
            photonControllerConfig.getXenonConfig(), hostClientFactory, agentControlClientFactory,
            nsxClientFactory, cloudStoreHelper, sslContext);
    logger.info("Created PhotonController Xenon Host");

    // Set referer Uri from the xenon host, because we do not want to rely on
    // CloudStoreHelper's default mechanise to create referer based on local address,
    // because CloudStoreHelper uses InetAddress.getLocalHost() which depends on
    // /etc/hosts having a hostname entry, which is not always available.
    // This change will allow people to run this service without need to
    // update their /etc/hosts file.
    cloudStoreHelper.setRefererUri(photonControllerXenonHost.getUri());

    final ConstraintChecker checker = new CloudStoreConstraintChecker(cloudStoreHelper,
            photonControllerXenonHost);

    logger.info("Creating Cloud Store Xenon Service Group");
    CloudStoreServiceGroup cloudStoreServiceGroup = createCloudStoreServiceGroup(deployerConfig.isInstaller());
    logger.info("Created Cloud Store Xenon Service Group");

    logger.info("Registering Cloud Store Xenon Service Group");
    photonControllerXenonHost.registerCloudStore(cloudStoreServiceGroup);
    logger.info("Registered Cloud Store Xenon Service Group");

    logger.info("Creating Scheduler Xenon Service Group");
    SchedulerServiceGroup schedulerServiceGroup = createSchedulerServiceGroup(photonControllerConfig.getRoot(),
            checker);
    logger.info("Created Scheduler Xenon Service Group");

    logger.info("Registering Scheduler Xenon Service Group");
    photonControllerXenonHost.registerScheduler(schedulerServiceGroup);
    logger.info("Registered Scheduler Xenon Service Group");

    logger.info("Creating Housekeeper Xenon Service Group");
    HousekeeperServiceGroup housekeeperServiceGroup = createHousekeeperServiceGroup();
    logger.info("Created Housekeeper Xenon Service Group");

    logger.info("Registering Housekeeper Xenon Service Group");
    photonControllerXenonHost.registerHousekeeper(housekeeperServiceGroup);
    logger.info("Registered Housekeeper Xenon Service Group");

    logger.info("Creating Deployer Xenon Service Group");
    DeployerServiceGroup deployerServiceGroup = createDeployerServiceGroup(photonControllerConfig,
            deployerConfig, apiFeServerSet, cloudStoreServerSet, httpClient);
    logger.info("Created Deployer Xenon Service Group");

    logger.info("Registering Deployer Xenon Service Group");
    photonControllerXenonHost.registerDeployer(deployerServiceGroup);
    logger.info("Registered Deployer Xenon Service Group");

    DeployerContext deployerContext = deployerConfig.getDeployerContext();
    if (deployerContext.isAuthEnabled()) {
        ServiceClient serviceClient = NettyHttpServiceClient.create(Main.class.getSimpleName(),
                Executors.newFixedThreadPool(Utils.DEFAULT_THREAD_COUNT),
                Executors.newScheduledThreadPool(Utils.DEFAULT_IO_THREAD_COUNT), photonControllerXenonHost);

        /*
        To make sure that Xenon uses only TLSv1.2 and disallows SSLv3, TLSv1,
        TLSv1.1 the Docker file for the photon-controller-core container is edited.
        The java.security file located inside the container at the location
        /var/opt/OpenJDK-* /jre/lib/security has the information under the
        jdk.tls.disabledAlgorithms
        */

        SSLContext clientContext = SSLContext.getInstance(ServiceClient.TLS_PROTOCOL_NAME);
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        KeyStore keyStore = KeyStore.getInstance("JKS");
        try (FileInputStream fis = new FileInputStream(deployerContext.getKeyStorePath())) {
            keyStore.load(fis, deployerContext.getKeyStorePassword().toCharArray());
        }
        keyManagerFactory.init(keyStore, deployerContext.getKeyStorePassword().toCharArray());
        clientContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
        serviceClient.setSSLContext(clientContext);
        photonControllerXenonHost.setClient(serviceClient);
    }

    logger.info("Starting PhotonController Xenon Host");
    photonControllerXenonHost.start();
    logger.info("Started PhotonController Xenon Host");

    logger.info("Creating SystemConfig instance");
    SystemConfig.createInstance(photonControllerXenonHost);
    logger.info("Created SystemConfig instance");
    return photonControllerXenonHost;
}

From source file:org.marketcetera.marketdata.core.webservice.impl.MarketDataServiceImpl.java

@Override
public void start() {
    if (isRunning()) {
        stop();/*from  w w w  .  j a  va2 s. c  o m*/
    }
    Validate.notNull(marketDataManager);
    Validate.notNull(serverProvider);
    reaper = Executors.newScheduledThreadPool(1);
    reaper.scheduleAtFixedRate(new Reaper(), reaperInterval, reaperInterval, TimeUnit.MILLISECONDS);
    remoteService = serverProvider.getServer().publish(this, MarketDataService.class);
    running.set(true);
}

From source file:io.gromit.geolite2.GeoLocation.java

/**
 * Start./*  w w w  . j a va 2  s  . com*/
 *
 * @return the scheduled database reader
 * @throws IllegalStateException the illegal state exception
 */
public GeoLocation start() throws IllegalStateException {
    if (scheduledExecutorService != null) {
        throw new IllegalStateException("it is already started");
    }
    readDatabase();
    scheduledExecutorService = Executors.newScheduledThreadPool(1);
    scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            readDatabase();
        }
    }, 1, 1, TimeUnit.DAYS);
    return this;
}

From source file:org.apache.hama.bsp.TestBSPTaskFaults.java

@Override
protected void setUp() throws Exception {
    super.setUp();
    conf = new HamaConfiguration();

    conf.setInt(Constants.GROOM_PING_PERIOD, 200);
    conf.setClass("bsp.work.class", FaulTestBSP.class, BSP.class);
    conf.setClass(SyncServiceFactory.SYNC_PEER_CLASS, LocalBSPRunner.LocalSyncClient.class, SyncClient.class);

    int port = BSPNetUtils.getFreePort(4321 + incrementTestNumber());
    try {//from w  ww .j a v a 2 s . c om
        InetSocketAddress inetAddress = new InetSocketAddress(port);
        groom = new MinimalGroomServer(conf);
        workerServer = RPC.getServer(groom, inetAddress.getHostName(), inetAddress.getPort(), conf);
        workerServer.start();

        LOG.info("Started RPC server");
        conf.setInt("bsp.groom.rpc.port", inetAddress.getPort());

        umbilical = (BSPPeerProtocol) RPC.getProxy(BSPPeerProtocol.class, HamaRPCProtocolVersion.versionID,
                inetAddress, conf);
        LOG.info("Started the proxy connections");

        this.testBSPTaskService = Executors.newScheduledThreadPool(1);
    } catch (BindException be) {
        LOG.info(be);
    }
}

From source file:com.jivesoftware.os.amza.sync.deployable.AmzaSyncMain.java

public void run(String[] args) throws Exception {
    ServiceStartupHealthCheck serviceStartupHealthCheck = new ServiceStartupHealthCheck();
    try {//from w  w w  . j  a  v  a  2  s . c o m
        final Deployable deployable = new Deployable(args);
        InstanceConfig instanceConfig = deployable.config(InstanceConfig.class);
        HealthFactory.initialize(deployable::config, new DeployableHealthCheckRegistry(deployable));
        deployable.addManageInjectables(HasUI.class, new HasUI(Arrays.asList(new UI("Sync", "main", "/ui"))));
        deployable.addHealthCheck(new GCPauseHealthChecker(
                deployable.config(GCPauseHealthChecker.GCPauseHealthCheckerConfig.class)));
        deployable.addHealthCheck(new GCLoadHealthChecker(
                deployable.config(GCLoadHealthChecker.GCLoadHealthCheckerConfig.class)));
        deployable.addHealthCheck(new SystemCpuHealthChecker(
                deployable.config(SystemCpuHealthChecker.SystemCpuHealthCheckerConfig.class)));
        deployable.addHealthCheck(new LoadAverageHealthChecker(
                deployable.config(LoadAverageHealthChecker.LoadAverageHealthCheckerConfig.class)));
        deployable.addHealthCheck(new FileDescriptorCountHealthChecker(deployable
                .config(FileDescriptorCountHealthChecker.FileDescriptorCountHealthCheckerConfig.class)));
        deployable.addHealthCheck(serviceStartupHealthCheck);
        deployable.addErrorHealthChecks(deployable.config(ErrorHealthCheckConfig.class));
        deployable.addManageInjectables(FullyOnlineVersion.class, (FullyOnlineVersion) () -> {
            if (serviceStartupHealthCheck.startupHasSucceeded()) {
                return instanceConfig.getVersion();
            } else {
                return null;
            }
        });
        deployable.buildManageServer().start();

        HttpDeliveryClientHealthProvider clientHealthProvider = new HttpDeliveryClientHealthProvider(
                instanceConfig.getInstanceKey(), HttpRequestHelperUtils.buildRequestHelper(false, false, null,
                        instanceConfig.getRoutesHost(), instanceConfig.getRoutesPort()),
                instanceConfig.getConnectionsHealth(), 5_000, 100);

        TenantRoutingProvider tenantRoutingProvider = deployable.getTenantRoutingProvider();

        TenantsServiceConnectionDescriptorProvider syncDescriptorProvider = tenantRoutingProvider
                .getConnections(instanceConfig.getServiceName(), "main", 10_000); // TODO config

        TenantRoutingHttpClientInitializer<String> tenantRoutingHttpClientInitializer = deployable
                .getTenantRoutingHttpClientInitializer();

        TenantAwareHttpClient<String> amzaClient = tenantRoutingHttpClientInitializer
                .builder(deployable.getTenantRoutingProvider().getConnections("amza", "main", 10_000), // TODO config
                        clientHealthProvider)
                .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).socketTimeoutInMillis(60_000).build(); // TODO expose to conf

        deployable.addHealthCheck(new TenantAwareHttpClientHealthCheck("amzaClient", amzaClient));

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        AmzaSyncConfig syncConfig = deployable.config(AmzaSyncConfig.class);

        TailAtScaleStrategy tailAtScaleStrategy = new TailAtScaleStrategy(
                deployable.newBoundedExecutor(1024, "tas"), 100, // TODO config
                95, // TODO config
                1000 // TODO config
        );

        AmzaInterner amzaInterner = new AmzaInterner();
        AmzaClientProvider<HttpClient, HttpClientException> amzaClientProvider = new AmzaClientProvider<>(
                new HttpPartitionClientFactory(),
                new HttpPartitionHostsProvider(amzaClient, tailAtScaleStrategy, mapper),
                new RingHostHttpClientProvider(amzaClient),
                deployable.newBoundedExecutor(syncConfig.getAmzaCallerThreadPoolSize(), "amza-client"),
                syncConfig.getAmzaAwaitLeaderElectionForNMillis(), -1, -1);

        TimestampedOrderIdProvider orderIdProvider = new OrderIdProviderImpl(
                new ConstantWriterIdProvider(instanceConfig.getInstanceName()), new SnowflakeIdPacker(),
                new JiveEpochTimestampProvider());
        AmzaClientAquariumProvider amzaClientAquariumProvider = new AmzaClientAquariumProvider(
                new AquariumStats(), instanceConfig.getServiceName(), amzaClientProvider, orderIdProvider,
                new Member(instanceConfig.getInstanceKey().getBytes(StandardCharsets.UTF_8)), count -> {
                    ConnectionDescriptors descriptors = syncDescriptorProvider.getConnections("");
                    int ringSize = descriptors.getConnectionDescriptors().size();
                    return count > ringSize / 2;
                }, () -> {
                    Set<Member> members = Sets.newHashSet();
                    ConnectionDescriptors descriptors = syncDescriptorProvider.getConnections("");
                    for (ConnectionDescriptor connectionDescriptor : descriptors.getConnectionDescriptors()) {
                        members.add(new Member(connectionDescriptor.getInstanceDescriptor().instanceKey
                                .getBytes(StandardCharsets.UTF_8)));
                    }
                    return members;
                }, 128, //TODO config
                128, //TODO config
                5_000L, //TODO config
                100L, //TODO config
                60_000L, //TODO config
                10_000L, //TODO config
                Executors.newSingleThreadExecutor(), 100L, //TODO config
                1_000L, //TODO config
                10_000L, //TODO config
                syncConfig.getAquariumUseSolutionLog());

        ObjectMapper miruSyncMapper = new ObjectMapper();
        miruSyncMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        miruSyncMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        AmzaMarshaller<String> stringMarshaller = new AmzaMarshaller<String>() {
            @Override
            public String fromBytes(byte[] bytes) throws Exception {
                return new String(bytes, StandardCharsets.UTF_8);
            }

            @Override
            public byte[] toBytes(String value) throws Exception {
                return value == null ? null : value.getBytes(StandardCharsets.UTF_8);
            }
        };

        AmzaMarshaller<AmzaSyncSenderConfig> amzaSyncSenderConfigMarshaller = new AmzaMarshaller<AmzaSyncSenderConfig>() {
            @Override
            public AmzaSyncSenderConfig fromBytes(byte[] bytes) throws Exception {
                return mapper.readValue(bytes, AmzaSyncSenderConfig.class);
            }

            @Override
            public byte[] toBytes(AmzaSyncSenderConfig value) throws Exception {
                return mapper.writeValueAsBytes(value);
            }
        };

        AmzaSyncSenderMap senderConfigStorage = new AmzaSyncSenderMap(amzaClientProvider,
                "amza-sync-sender-config",
                new PartitionProperties(Durability.fsync_async, 0, 0, 0, 0, 0, 0, 0, 0, false,
                        Consistency.leader_quorum, true, true, false, RowType.snappy_primary, "lab", -1, null,
                        -1, -1),
                stringMarshaller, amzaSyncSenderConfigMarshaller);

        AmzaMarshaller<AmzaSyncPartitionTuple> tupleMarshaller = new AmzaMarshaller<AmzaSyncPartitionTuple>() {
            @Override
            public AmzaSyncPartitionTuple fromBytes(byte[] bytes) throws Exception {
                return AmzaSyncPartitionTuple.fromBytes(bytes, 0, amzaInterner);
            }

            @Override
            public byte[] toBytes(AmzaSyncPartitionTuple value) throws Exception {
                return AmzaSyncPartitionTuple.toBytes(value);
            }
        };

        AmzaMarshaller<AmzaSyncPartitionConfig> partitionConfigMarshaller = new AmzaMarshaller<AmzaSyncPartitionConfig>() {
            @Override
            public AmzaSyncPartitionConfig fromBytes(byte[] bytes) throws Exception {
                return mapper.readValue(bytes, AmzaSyncPartitionConfig.class);
            }

            @Override
            public byte[] toBytes(AmzaSyncPartitionConfig value) throws Exception {
                return mapper.writeValueAsBytes(value);
            }
        };

        AmzaSyncPartitionConfigStorage syncPartitionConfigStorage = new AmzaSyncPartitionConfigStorage(
                amzaClientProvider, "amza-sync-partitions-config-",
                new PartitionProperties(Durability.fsync_async, 0, 0, 0, 0, 0, 0, 0, 0, false,
                        Consistency.leader_quorum, true, true, false, RowType.snappy_primary, "lab", -1, null,
                        -1, -1),
                tupleMarshaller, partitionConfigMarshaller);

        AmzaSyncStats stats = new AmzaSyncStats();

        AmzaSyncReceiver syncReceiver = new AmzaSyncReceiver(amzaClientProvider,
                syncConfig.getSyncReceiverUseSolutionLog());

        AmzaSyncSenders syncSenders = null;
        if (syncConfig.getSyncSenderEnabled()) {
            ScheduledExecutorService executorService = Executors
                    .newScheduledThreadPool(syncConfig.getSyncSendersThreadCount());
            syncSenders = new AmzaSyncSenders(stats, syncConfig, syncReceiver, executorService,
                    amzaClientProvider, amzaClientAquariumProvider, amzaInterner, mapper, orderIdProvider,
                    senderConfigStorage, syncPartitionConfigStorage, 30_000); // TODO config
        }

        amzaClientAquariumProvider.start();
        if (syncSenders != null) {
            syncSenders.start();
        }

        SoyRendererConfig rendererConfig = deployable.config(SoyRendererConfig.class);

        File staticResourceDir = new File(System.getProperty("user.dir"));
        System.out.println("Static resources rooted at " + staticResourceDir.getAbsolutePath());
        Resource sourceTree = new Resource(staticResourceDir)
                .addResourcePath(rendererConfig.getPathToStaticResources()).setDirectoryListingAllowed(false)
                .setContext("/ui/static");
        deployable.addResource(sourceTree);

        SoyRenderer renderer = new SoyRendererInitializer().initialize(rendererConfig);

        AmzaSyncUIService amzaSyncUIService = new AmzaSyncUIServiceInitializer().initialize(renderer,
                syncSenders, stats, syncConfig.getSyncSenderEnabled(), syncConfig.getSyncReceiverEnabled(),
                mapper);

        deployable.addEndpoints(LoadBalancerHealthCheckEndpoints.class);
        deployable.addNoAuth("/health/check");
        if (instanceConfig.getMainServiceAuthEnabled()) {
            if (syncConfig.getSyncReceiverEnabled()) {
                AmzaSyncOAuthValidatorConfig oAuthValidatorConfig = deployable
                        .config(AmzaSyncOAuthValidatorConfig.class);
                AuthValidator<OAuth1Signature, OAuth1Request> syncOAuthValidator = new AmzaSyncOAuthValidatorInitializer()
                        .initialize(oAuthValidatorConfig);
                deployable.addCustomOAuth(syncOAuthValidator, "/api/*");
            }
            deployable.addRouteOAuth("/amza/*", "/api/*");
            deployable.addSessionAuth("/ui/*", "/amza/*", "/api/*");
        } else {
            deployable.addNoAuth("/amza/*", "/api/*");
            deployable.addSessionAuth("/ui/*");
        }

        deployable.addEndpoints(AmzaSyncEndpoints.class);
        deployable.addInjectables(AmzaInterner.class, amzaInterner);
        if (syncSenders != null) {
            deployable.addInjectables(AmzaSyncSenders.class, syncSenders);
        }

        deployable.addEndpoints(AmzaSyncUIEndpoints.class);
        deployable.addInjectables(AmzaSyncUIService.class, amzaSyncUIService);

        if (syncConfig.getSyncReceiverEnabled()) {
            deployable.addEndpoints(AmzaSyncApiEndpoints.class);
            deployable.addInjectables(AmzaSyncReceiver.class, syncReceiver);
        }
        deployable.addInjectables(ObjectMapper.class, mapper);
        deployable.addInjectables(AmzaSyncSenderMap.class, senderConfigStorage);
        deployable.addInjectables(AmzaSyncPartitionConfigStorage.class, syncPartitionConfigStorage);

        deployable.buildServer().start();
        clientHealthProvider.start();
        serviceStartupHealthCheck.success();

    } catch (Throwable t) {
        serviceStartupHealthCheck.info("Encountered the following failure during startup.", t);
    }
}

From source file:org.apache.jmeter.visualizers.backend.graphite.GraphiteBackendListenerClient.java

@Override
public void setupTest(BackendListenerContext context) throws Exception {
    String graphiteMetricsSenderClass = context.getParameter(GRAPHITE_METRICS_SENDER);

    graphiteHost = context.getParameter(GRAPHITE_HOST);
    graphitePort = context.getIntParameter(GRAPHITE_PORT, DEFAULT_PLAINTEXT_PROTOCOL_PORT);
    summaryOnly = context.getBooleanParameter(SUMMARY_ONLY, true);
    samplersList = context.getParameter(SAMPLERS_LIST, "");
    useRegexpForSamplersList = context.getBooleanParameter(USE_REGEXP_FOR_SAMPLERS_LIST, false);
    rootMetricsPrefix = context.getParameter(ROOT_METRICS_PREFIX, DEFAULT_METRICS_PREFIX);
    String[] percentilesStringArray = context.getParameter(PERCENTILES, DEFAULT_METRICS_PREFIX)
            .split(SEPARATOR);//from   w w  w .ja  v a2 s .c  o  m
    okPercentiles = new HashMap<>(percentilesStringArray.length);
    koPercentiles = new HashMap<>(percentilesStringArray.length);
    allPercentiles = new HashMap<>(percentilesStringArray.length);
    DecimalFormat decimalFormat = new DecimalFormat("0.##");
    for (String percentilesString : percentilesStringArray) {
        if (!StringUtils.isEmpty(percentilesString.trim())) {
            try {
                Float percentileValue = Float.valueOf(percentilesString.trim());
                String sanitizedFormattedPercentile = AbstractGraphiteMetricsSender
                        .sanitizeString(decimalFormat.format(percentileValue));
                okPercentiles.put(METRIC_OK_PERCENTILE_PREFIX + sanitizedFormattedPercentile, percentileValue);
                koPercentiles.put(METRIC_KO_PERCENTILE_PREFIX + sanitizedFormattedPercentile, percentileValue);
                allPercentiles.put(METRIC_ALL_PERCENTILE_PREFIX + sanitizedFormattedPercentile,
                        percentileValue);

            } catch (Exception e) {
                LOGGER.error("Error parsing percentile:'" + percentilesString + "'", e);
            }
        }
    }
    Class<?> clazz = Class.forName(graphiteMetricsSenderClass);
    this.graphiteMetricsManager = (GraphiteMetricsSender) clazz.newInstance();
    graphiteMetricsManager.setup(graphiteHost, graphitePort, rootMetricsPrefix);
    if (useRegexpForSamplersList) {
        pattern = Pattern.compile(samplersList);
    } else {
        String[] samplers = samplersList.split(SEPARATOR);
        samplersToFilter = new HashSet<>();
        Collections.addAll(samplersToFilter, samplers);
    }
    scheduler = Executors.newScheduledThreadPool(MAX_POOL_SIZE);
    // Don't change this as metrics are per second
    this.timerHandle = scheduler.scheduleAtFixedRate(this, ONE_SECOND, ONE_SECOND, TimeUnit.SECONDS);
}