/**
   * Asynchronously connect to rabbitmq, and retry until successful
   *
   * @param env dropwizard environment
   * @param deliveryExecutor the executor used by rabbitmq client to deliver messages
   * @param name name of rabbitmq connection
   * @param callback callback when done - which may be after application start
   */
  public void buildRetryInitialConnect(
      final Environment env,
      final ExecutorService deliveryExecutor,
      final String name,
      final ConnectedCallback callback) {
    final com.rabbitmq.client.ConnectionFactory connectionFactory = makeConnectionFactory();
    final ScheduledExecutorService initialConnectExecutor =
        env.lifecycle()
            .scheduledExecutorService(name + "-initial-connect-thread")
            .threads(1)
            .build();

    final ConnectionMetrics connectionMetrics =
        Optional.fromNullable(metrics).or(() -> new DefaultConnectionMetrics(name, env.metrics()));
    final WrappedConnectionMetrics connectionMetricsWrapper =
        new WrappedConnectionMetrics(connectionMetrics);
    final ConnectedCallback callbackWithMetrics =
        connection -> {
          final Connection metricsConnection = connectionMetricsWrapper.wrap(connection);
          callback.connected(metricsConnection);
        };
    final ConnectAsync connectAsync =
        new ConnectAsync(
            connectionFactory, deliveryExecutor, name, initialConnectExecutor, callbackWithMetrics);
    registerWithEnvironment(env.healthChecks(), env.lifecycle(), connectAsync::getConnection, name);
    connectAsync.run();
  }
 @SuppressWarnings("unchecked")
 private void toggleManagedObjects(boolean start) throws Exception {
   Field managedObjectsField =
       environment.lifecycle().getClass().getDeclaredField("managedObjects");
   managedObjectsField.setAccessible(true);
   List<LifeCycle> managedObjects =
       (List<LifeCycle>) managedObjectsField.get(environment.lifecycle());
   for (LifeCycle managedObject : managedObjects) {
     if (start) {
       managedObject.start();
     } else {
       managedObject.stop();
     }
   }
 }
 private <T> void internalRegisterReceiver(
     String destination, ActiveMQReceiverHandler<T> handler) {
   environment.lifecycle().manage(handler);
   environment
       .healthChecks()
       .register("ActiveMQ receiver for " + destination, handler.getHealthCheck());
 }
  @Override
  public void run(ActiveMQConfigHolder configuration, Environment environment) throws Exception {
    this.environment = environment;
    final String brokerUrl = configuration.getActiveMQ().brokerUrl;
    final int configuredTTL = configuration.getActiveMQ().timeToLiveInSeconds;
    defaultTimeToLiveInSeconds = Optional.ofNullable(configuredTTL > 0 ? configuredTTL : null);

    log.info("Setting up activeMq with brokerUrl {}", brokerUrl);

    log.debug("All activeMQ config: " + configuration.getActiveMQ());

    realConnectionFactory = new ActiveMQConnectionFactory(brokerUrl);
    connectionFactory = new PooledConnectionFactory();
    connectionFactory.setConnectionFactory(realConnectionFactory);

    configurePool(configuration.getActiveMQ().pool);

    objectMapper = environment.getObjectMapper();

    environment.lifecycle().manage(this);

    // Must use realConnectionFactory instead of (pooled) connectionFactory for the healthCheck
    // Is needs its own connection since it is both sending and receiving.
    // If using pool, then it might block since no one is available..
    environment
        .healthChecks()
        .register(
            "ActiveMQ",
            new ActiveMQHealthCheck(
                realConnectionFactory, configuration.getActiveMQ().healthCheckMillisecondsToWait));
    this.shutdownWaitInSeconds = configuration.getActiveMQ().shutdownWaitInSeconds;
  }
Пример #5
0
  private Client build(
      String name, ExecutorService threadPool, ObjectMapper objectMapper, Validator validator) {
    final Client client =
        ClientBuilder.newClient(buildConfig(name, threadPool, objectMapper, validator));
    client.register(new JerseyIgnoreRequestUserAgentHeaderFilter());

    // Tie the client to server lifecycle
    if (environment != null) {
      environment
          .lifecycle()
          .manage(
              new Managed() {
                @Override
                public void start() throws Exception {}

                @Override
                public void stop() throws Exception {
                  client.close();
                }
              });
    }
    if (configuration.isGzipEnabled()) {
      client.register(new GZipDecoder());
      client.register(new ConfiguredGZipEncoder(configuration.isGzipEnabledForRequests()));
    }

    return client;
  }
Пример #6
0
  /**
   * Builds the {@link Client} instance.
   *
   * @return a fully-configured {@link Client}
   */
  public Client build(String name) {
    if ((environment == null) && ((executorService == null) || (objectMapper == null))) {
      throw new IllegalStateException(
          "Must have either an environment or both " + "an executor service and an object mapper");
    }

    if (executorService == null && environment != null) {
      executorService =
          environment
              .lifecycle()
              .executorService("jersey-client-" + name + "-%d")
              .minThreads(configuration.getMinThreads())
              .maxThreads(configuration.getMaxThreads())
              .workQueue(new ArrayBlockingQueue<Runnable>(configuration.getWorkQueueSize()))
              .build();
    }

    if (objectMapper == null && environment != null) {
      objectMapper = environment.getObjectMapper();
    }

    if (environment != null) {
      validator = environment.getValidator();
    }

    return build(name, executorService, objectMapper, validator);
  }
 @Override
 public ThreadPoolExecutor getThreadPool(
     HystrixThreadPoolKey threadPoolKey,
     HystrixProperty<Integer> corePoolSize,
     HystrixProperty<Integer> maximumPoolSize,
     HystrixProperty<Integer> keepAliveTime,
     TimeUnit unit,
     BlockingQueue<Runnable> workQueue) {
   final String nameFormat =
       Joiner.on('-').join(ImmutableList.of("hystrix"), threadPoolKey.name(), "%d");
   final ThreadFactory threadFactory =
       new ThreadFactoryBuilder().setNameFormat(nameFormat).build();
   final String key = threadPoolKey.name();
   final ThreadPoolExecutor existing =
       executors.putIfAbsent(
           key,
           new ThreadPoolExecutor(
               corePoolSize.get(),
               maximumPoolSize.get(),
               keepAliveTime.get(),
               unit,
               workQueue,
               threadFactory));
   final ThreadPoolExecutor threadPoolExecutor = executors.get(key);
   if (existing == null) {
     environment
         .lifecycle()
         .manage(new ExecutorServiceManager(threadPoolExecutor, Duration.seconds(5), nameFormat));
   }
   return threadPoolExecutor;
 }
 @After
 public void tearDown() throws Exception {
   for (LifeCycle lifeCycle : environment.lifecycle().getManagedObjects()) {
     lifeCycle.stop();
   }
   assertThat(client.isClosed()).isTrue();
 }
 /**
  * Synchronously connect to rabbitmq, will cause application to fail if initial connection is
  * unsuccessful.
  *
  * @param env dropwizard environment
  * @param deliveryExecutor executor
  * @param name name of rabbitmq connection
  * @return Connection connection
  * @throws IOException if we cannot connect
  * @throws TimeoutException if we timeout while trying to connect
  */
 public Connection build(
     final Environment env, final ExecutorService deliveryExecutor, final String name)
     throws IOException, TimeoutException {
   final HealthCheckRegistry healthChecks = env.healthChecks();
   final LifecycleEnvironment lifecycle = env.lifecycle();
   final MetricRegistry metrics = env.metrics();
   return build(healthChecks, lifecycle, metrics, deliveryExecutor, name);
 }
Пример #10
0
  @Before
  public void setup() {
    bundle =
        new JedisBundle<TestConfig>() {
          @Override
          public JedisFactory getJedisFactory(TestConfig configuration) {
            return jedisFactory;
          }
        };

    when(environment.healthChecks()).thenReturn(healthChecks);
    when(environment.jersey()).thenReturn(jerseyEnvironment);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);

    when(jedisFactory.build(environment)).thenReturn(pool);
  }
  /**
   * Build a new instance of a {@link CuratorFramework} and register a health check and make sure
   * it's properly managed.
   *
   * @param config {@link DiscoveryFactory}
   * @return {@link CuratorFramework}
   */
  public CuratorFramework build(@Nonnull final DiscoveryFactory config) {
    final CuratorFramework framework =
        CuratorFrameworkFactory.builder()
            .connectionTimeoutMs((int) config.getConnectionTimeout().toMilliseconds())
            .sessionTimeoutMs((int) config.getSessionTimeout().toMilliseconds())
            .retryPolicy(config.getRetryPolicy())
            .compressionProvider(config.getCompressionProvider())
            .connectString(config.getQuorumSpec())
            .canBeReadOnly(config.isReadOnly())
            .namespace(config.getNamespace())
            .build();

    environment.lifecycle().manage(new CuratorManager(framework));
    environment.healthChecks().register("curator", new CuratorHealthCheck(framework));
    return framework;
  }
Пример #12
0
  @Override
  public void run(ThirdEyeDashboardConfiguration config, Environment environment) throws Exception {
    final HttpClient httpClient =
        new HttpClientBuilder(environment).using(config.getHttpClient()).build(getName());

    ExecutorService queryExecutor =
        environment.lifecycle().executorService("query_executor").build();

    DataCache dataCache = new DataCache(httpClient, environment.getObjectMapper());

    QueryCache queryCache =
        new QueryCache(httpClient, environment.getObjectMapper(), queryExecutor);

    CustomDashboardResource customDashboardResource = null;
    if (config.getCustomDashboardRoot() != null) {
      File customDashboardDir = new File(config.getCustomDashboardRoot());
      customDashboardResource =
          new CustomDashboardResource(
              customDashboardDir, config.getServerUri(), queryCache, dataCache);
      environment.jersey().register(customDashboardResource);
    }

    environment
        .jersey()
        .register(
            new DashboardResource(
                config.getServerUri(),
                dataCache,
                queryCache,
                environment.getObjectMapper(),
                customDashboardResource));

    environment
        .jersey()
        .register(
            new FlotTimeSeriesResource(
                config.getServerUri(), dataCache, queryCache, environment.getObjectMapper()));

    environment.jersey().register(new MetadataResource(config.getServerUri(), dataCache));

    environment.admin().addTask(new ClearCachesTask(dataCache, queryCache));
  }
  @Before
  public void setup() throws Exception {
    JerseyClientConfiguration clientConfiguration = new JerseyClientConfiguration();
    clientConfiguration.setConnectionTimeout(Duration.milliseconds(SLEEP_TIME_IN_MILLIS / 2));
    clientConfiguration.setTimeout(Duration.milliseconds(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS));

    environment =
        new Environment(
            "test-dropwizard-apache-connector",
            Jackson.newObjectMapper(),
            Validators.newValidator(),
            new MetricRegistry(),
            getClass().getClassLoader());
    client =
        (JerseyClient)
            new JerseyClientBuilder(environment).using(clientConfiguration).build("test");
    for (LifeCycle lifeCycle : environment.lifecycle().getManagedObjects()) {
      lifeCycle.start();
    }
  }
 @Override
 public void install(final Environment environment, final Managed instance) {
   reporter.line("(%s)", FeatureUtils.getInstanceClass(instance).getName());
   environment.lifecycle().manage(instance);
 }
 /**
  * Return a managed Curator connection. This created connection will be wrapped in a {@link
  * ManagedCuratorFramework} and offered to the provided {@link Environment} parameter.
  *
  * @deprecated Use {@link #newManagedCurator(LifecycleEnvironment)} instead.
  */
 @Deprecated
 public CuratorFramework newManagedCurator(Environment env) {
   return newManagedCurator(env.lifecycle());
 }
 @Override
 public void run(Environment environment) {
   environment.lifecycle().manage(new SpringJobManager(scanURL, context));
 }
  /**
   * Creates a Jetty server for an application that can be started / stopped in-process
   *
   * @param config An application configuration instance (with properties set)
   * @param applicationClass The {@link io.dropwizard.Application} implementation class
   * @param <T> The configuration class
   * @return A Jetty server
   */
  @SuppressWarnings("unchecked")
  public static <T extends Configuration> DropWizardServer<T> createServer(
      T config, Class<? extends Application<T>> applicationClass) throws Exception {
    // Create application
    final Application<T> application = applicationClass.getConstructor().newInstance();

    // Create bootstrap
    final ServerCommand<T> serverCommand = new ServerCommand<T>(application);
    final Bootstrap<T> bootstrap = new Bootstrap<T>(application);
    bootstrap.addCommand(serverCommand);

    // Write a temporary config file
    File tmpConfigFile =
        new File(
            System.getProperty("java.io.tmpdir"),
            config.getClass().getCanonicalName() + "_" + System.currentTimeMillis());
    tmpConfigFile.deleteOnExit();
    bootstrap.getObjectMapper().writeValue(tmpConfigFile, config);

    // Parse configuration
    ConfigurationFactory<T> configurationFactory =
        bootstrap
            .getConfigurationFactoryFactory()
            .create(
                (Class<T>) config.getClass(),
                bootstrap.getValidatorFactory().getValidator(),
                bootstrap.getObjectMapper(),
                "dw");
    final T builtConfig =
        configurationFactory.build(
            bootstrap.getConfigurationSourceProvider(), tmpConfigFile.getAbsolutePath());

    // Configure logging
    builtConfig
        .getLoggingFactory()
        .configure(bootstrap.getMetricRegistry(), bootstrap.getApplication().getName());

    // Environment
    final Environment environment =
        new Environment(
            bootstrap.getApplication().getName(),
            bootstrap.getObjectMapper(),
            bootstrap.getValidatorFactory().getValidator(),
            bootstrap.getMetricRegistry(),
            bootstrap.getClassLoader());

    // Initialize environment
    builtConfig
        .getMetricsFactory()
        .configure(environment.lifecycle(), bootstrap.getMetricRegistry());

    // Server
    final Server server = builtConfig.getServerFactory().build(environment);
    server.addLifeCycleListener(
        new AbstractLifeCycle.AbstractLifeCycleListener() {
          @Override
          public void lifeCycleStopped(LifeCycle event) {
            builtConfig.getLoggingFactory().stop();
          }
        });

    return new DropWizardServer(
        builtConfig, bootstrap, application, environment, server, environment.metrics());
  }
 @Override
 public void run(Environment environment) {
   environment.lifecycle().manage(new GuiceJobManager(scanURL, injector));
 }
  @Override
  public void run(WhisperServerConfiguration config, Environment environment) throws Exception {
    SharedMetricRegistries.add(Constants.METRICS_NAME, environment.metrics());
    environment
        .getObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    DBIFactory dbiFactory = new DBIFactory();
    DBI database = dbiFactory.build(environment, config.getDataSourceFactory(), "accountdb");
    DBI messagedb =
        dbiFactory.build(environment, config.getMessageStoreConfiguration(), "messagedb");

    Accounts accounts = database.onDemand(Accounts.class);
    PendingAccounts pendingAccounts = database.onDemand(PendingAccounts.class);
    PendingDevices pendingDevices = database.onDemand(PendingDevices.class);
    Keys keys = database.onDemand(Keys.class);
    Messages messages = messagedb.onDemand(Messages.class);

    RedisClientFactory cacheClientFactory =
        new RedisClientFactory(config.getCacheConfiguration().getUrl());
    JedisPool cacheClient = cacheClientFactory.getRedisClientPool();
    JedisPool directoryClient =
        new RedisClientFactory(config.getDirectoryConfiguration().getUrl()).getRedisClientPool();
    Client httpClient = initializeHttpClient(environment, config);

    DirectoryManager directory = new DirectoryManager(directoryClient);
    PendingAccountsManager pendingAccountsManager =
        new PendingAccountsManager(pendingAccounts, cacheClient);
    PendingDevicesManager pendingDevicesManager =
        new PendingDevicesManager(pendingDevices, cacheClient);
    AccountsManager accountsManager = new AccountsManager(accounts, directory, cacheClient);
    FederatedClientManager federatedClientManager =
        new FederatedClientManager(
            environment,
            config.getJerseyClientConfiguration(),
            config.getFederationConfiguration());
    MessagesManager messagesManager = new MessagesManager(messages);
    DeadLetterHandler deadLetterHandler = new DeadLetterHandler(messagesManager);
    DispatchManager dispatchManager =
        new DispatchManager(cacheClientFactory, Optional.<DispatchChannel>of(deadLetterHandler));
    PubSubManager pubSubManager = new PubSubManager(cacheClient, dispatchManager);
    PushServiceClient pushServiceClient =
        new PushServiceClient(httpClient, config.getPushConfiguration());
    WebsocketSender websocketSender = new WebsocketSender(messagesManager, pubSubManager);
    AccountAuthenticator deviceAuthenticator = new AccountAuthenticator(accountsManager);
    FederatedPeerAuthenticator federatedPeerAuthenticator =
        new FederatedPeerAuthenticator(config.getFederationConfiguration());
    RateLimiters rateLimiters = new RateLimiters(config.getLimitsConfiguration(), cacheClient);

    ApnFallbackManager apnFallbackManager = new ApnFallbackManager(pushServiceClient);
    TwilioSmsSender twilioSmsSender = new TwilioSmsSender(config.getTwilioConfiguration());
    Optional<NexmoSmsSender> nexmoSmsSender =
        initializeNexmoSmsSender(config.getNexmoConfiguration());
    SmsSender smsSender =
        new SmsSender(
            twilioSmsSender, nexmoSmsSender, config.getTwilioConfiguration().isInternational());
    UrlSigner urlSigner = new UrlSigner(config.getS3Configuration());
    PushSender pushSender = new PushSender(apnFallbackManager, pushServiceClient, websocketSender);
    ReceiptSender receiptSender =
        new ReceiptSender(accountsManager, pushSender, federatedClientManager);
    FeedbackHandler feedbackHandler = new FeedbackHandler(pushServiceClient, accountsManager);
    Optional<byte[]> authorizationKey = config.getRedphoneConfiguration().getAuthorizationKey();

    environment.lifecycle().manage(apnFallbackManager);
    environment.lifecycle().manage(pubSubManager);
    environment.lifecycle().manage(feedbackHandler);

    AttachmentController attachmentController =
        new AttachmentController(rateLimiters, federatedClientManager, urlSigner);
    KeysControllerV1 keysControllerV1 =
        new KeysControllerV1(rateLimiters, keys, accountsManager, federatedClientManager);
    KeysControllerV2 keysControllerV2 =
        new KeysControllerV2(rateLimiters, keys, accountsManager, federatedClientManager);
    MessageController messageController =
        new MessageController(
            rateLimiters,
            pushSender,
            receiptSender,
            accountsManager,
            messagesManager,
            federatedClientManager);

    environment
        .jersey()
        .register(
            new AuthDynamicFeature(
                new BasicCredentialAuthFilter.Builder<Account>()
                    .setAuthenticator(deviceAuthenticator)
                    .setPrincipal(Account.class)
                    .buildAuthFilter(),
                new BasicCredentialAuthFilter.Builder<FederatedPeer>()
                    .setAuthenticator(federatedPeerAuthenticator)
                    .setPrincipal(FederatedPeer.class)
                    .buildAuthFilter()));
    environment.jersey().register(new AuthValueFactoryProvider.Binder());

    environment
        .jersey()
        .register(
            new AccountController(
                pendingAccountsManager,
                accountsManager,
                rateLimiters,
                smsSender,
                messagesManager,
                new TimeProvider(),
                authorizationKey,
                config.getTestDevices()));
    environment
        .jersey()
        .register(new DeviceController(pendingDevicesManager, accountsManager, rateLimiters));
    environment.jersey().register(new DirectoryController(rateLimiters, directory));
    environment
        .jersey()
        .register(
            new FederationControllerV1(
                accountsManager, attachmentController, messageController, keysControllerV1));
    environment
        .jersey()
        .register(
            new FederationControllerV2(
                accountsManager, attachmentController, messageController, keysControllerV2));
    environment.jersey().register(new ReceiptController(receiptSender));
    environment.jersey().register(new ProvisioningController(rateLimiters, pushSender));
    environment.jersey().register(attachmentController);
    environment.jersey().register(keysControllerV1);
    environment.jersey().register(keysControllerV2);
    environment.jersey().register(messageController);

    if (config.getWebsocketConfiguration().isEnabled()) {
      WebSocketEnvironment webSocketEnvironment =
          new WebSocketEnvironment(environment, config, 90000);
      webSocketEnvironment.setAuthenticator(new WebSocketAccountAuthenticator(deviceAuthenticator));
      webSocketEnvironment.setConnectListener(
          new AuthenticatedConnectListener(
              accountsManager,
              pushSender,
              receiptSender,
              messagesManager,
              pubSubManager,
              apnFallbackManager));
      webSocketEnvironment.jersey().register(new KeepAliveController(pubSubManager));

      WebSocketEnvironment provisioningEnvironment = new WebSocketEnvironment(environment, config);
      provisioningEnvironment.setConnectListener(new ProvisioningConnectListener(pubSubManager));
      provisioningEnvironment.jersey().register(new KeepAliveController(pubSubManager));

      WebSocketResourceProviderFactory webSocketServlet =
          new WebSocketResourceProviderFactory(webSocketEnvironment);
      WebSocketResourceProviderFactory provisioningServlet =
          new WebSocketResourceProviderFactory(provisioningEnvironment);

      ServletRegistration.Dynamic websocket =
          environment.servlets().addServlet("WebSocket", webSocketServlet);
      ServletRegistration.Dynamic provisioning =
          environment.servlets().addServlet("Provisioning", provisioningServlet);

      websocket.addMapping("/v1/websocket/");
      websocket.setAsyncSupported(true);

      provisioning.addMapping("/v1/websocket/provisioning/");
      provisioning.setAsyncSupported(true);

      webSocketServlet.start();
      provisioningServlet.start();

      FilterRegistration.Dynamic filter =
          environment.servlets().addFilter("CORS", CrossOriginFilter.class);
      filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
      filter.setInitParameter("allowedOrigins", "*");
      filter.setInitParameter(
          "allowedHeaders",
          "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
      filter.setInitParameter("allowedMethods", "GET,PUT,POST,DELETE,OPTIONS");
      filter.setInitParameter("preflightMaxAge", "5184000");
      filter.setInitParameter("allowCredentials", "true");
    }

    environment.healthChecks().register("directory", new RedisHealthCheck(directoryClient));
    environment.healthChecks().register("cache", new RedisHealthCheck(cacheClient));

    environment.jersey().register(new IOExceptionMapper());
    environment.jersey().register(new RateLimitExceededExceptionMapper());
    environment.jersey().register(new InvalidWebsocketAddressExceptionMapper());
    environment.jersey().register(new DeviceLimitExceededExceptionMapper());

    environment.metrics().register(name(CpuUsageGauge.class, "cpu"), new CpuUsageGauge());
    environment
        .metrics()
        .register(name(FreeMemoryGauge.class, "free_memory"), new FreeMemoryGauge());
    environment
        .metrics()
        .register(name(NetworkSentGauge.class, "bytes_sent"), new NetworkSentGauge());
    environment
        .metrics()
        .register(name(NetworkReceivedGauge.class, "bytes_received"), new NetworkReceivedGauge());

    if (config.getGraphiteConfiguration().isEnabled()) {
      GraphiteReporterFactory graphiteReporterFactory = new GraphiteReporterFactory();
      graphiteReporterFactory.setHost(config.getGraphiteConfiguration().getHost());
      graphiteReporterFactory.setPort(config.getGraphiteConfiguration().getPort());

      GraphiteReporter graphiteReporter =
          (GraphiteReporter) graphiteReporterFactory.build(environment.metrics());
      graphiteReporter.start(15, TimeUnit.SECONDS);
    }
  }