io.dropwizard.server.ServerFactory Java Examples

The following examples show how to use io.dropwizard.server.ServerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServerUtils.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
public static int port(ServerFactory serverFactory) {
    if(serverFactory instanceof SimpleServerFactory) {
        SimpleServerFactory simpleServerFactory = (SimpleServerFactory)serverFactory;
        return getPortFromConnector(simpleServerFactory.getConnector());

    }
    if(serverFactory instanceof DefaultServerFactory) {
        DefaultServerFactory defaultServerFactory = (DefaultServerFactory)serverFactory;
        for(ConnectorFactory connectorFactory : defaultServerFactory.getApplicationConnectors()) {
            if(connectorFactory instanceof HttpConnectorFactory) {
                return getPortFromConnector(connectorFactory);
            }
        }

    }
    throw new PortExtractionException("Cannot extract port from connector");
}
 
Example #2
Source File: ClusterManager.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Inject
public ClusterManager(HazelcastConnection connection, List<HealthCheck> healthChecks, ServerFactory serverFactory) throws IOException {
    this.hazelcastConnection = connection;
    this.healthChecks = healthChecks;
    MapConfig mapConfig = new MapConfig(MAP_NAME);
    mapConfig.setTimeToLiveSeconds(MAP_REFRESH_TIME + 2); //Reduce jitter
    mapConfig.setBackupCount(1);
    mapConfig.setAsyncBackupCount(2);
    mapConfig.setEvictionPolicy(EvictionPolicy.NONE);
    hazelcastConnection.getHazelcastConfig()
            .getMapConfigs()
            .put(MAP_NAME, mapConfig);
    String hostname = Inet4Address.getLocalHost()
            .getCanonicalHostName();
    //Auto detect marathon environment and query for host environment variable
    if(!Strings.isNullOrEmpty(System.getenv("HOST")))
        hostname = System.getenv("HOST");
    Preconditions.checkNotNull(hostname, "Could not retrieve hostname, cannot proceed");
    int port = ServerUtils.port(serverFactory);
    //Auto detect marathon environment and query for host environment variable
    if(!Strings.isNullOrEmpty(System.getenv("PORT_" + port)))
        port = Integer.parseInt(System.getenv("PORT_" + port));
    executor = Executors.newScheduledThreadPool(1);
    clusterMember = new ClusterMember(hostname, port);
}
 
Example #3
Source File: EmoModule.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure() {
    bind(String.class).annotatedWith(SystemTablePlacement.class).toInstance(_configuration.getSystemTablePlacement());
    install(new SettingsModule());
    bind(Environment.class).toInstance(_environment);
    bind(HealthCheckRegistry.class).to(DropwizardHealthCheckRegistry.class).asEagerSingleton();
    bind(LifeCycleRegistry.class).to(DropwizardLifeCycleRegistry.class).asEagerSingleton();
    bind(ZooKeeperConfiguration.class).toInstance(_configuration.getZooKeeperConfiguration());
    bind(String.class).annotatedWith(ServerCluster.class).toInstance(_configuration.getCluster());
    bind(MetricRegistry.class).toInstance(_environment.metrics());
    bind(ServerFactory.class).toInstance(_configuration.getServerFactory());
    bind(DataCenterConfiguration.class).toInstance(_configuration.getDataCenterConfiguration());
    bind(CqlDriverConfiguration.class).toInstance(_configuration.getCqlDriverConfiguration());
    bind(Clock.class).toInstance(Clock.systemUTC());
}
 
Example #4
Source File: ScanUploadTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
private static void updatePortsToAvoidCollision(ServerFactory serverFactory) {
    if (serverFactory instanceof DefaultServerFactory) {
        DefaultServerFactory defaultServerFactory = (DefaultServerFactory)serverFactory;
        updatePortsToAvoidCollision(defaultServerFactory.getApplicationConnectors());
        updatePortsToAvoidCollision(defaultServerFactory.getAdminConnectors());
    } else if (serverFactory instanceof SimpleServerFactory) {
        SimpleServerFactory simpleServerFactory = (SimpleServerFactory)serverFactory;
        updatePortsToAvoidCollision(Collections.singleton(simpleServerFactory.getConnector()));
    } else {
        throw new IllegalStateException("Encountered an unexpected ServerFactory type");
    }
}
 
Example #5
Source File: SelfHostAndPortModule.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Provides @Singleton @SelfHostAndPort
public HostAndPort provideSelfHostAndPort(ServerFactory serverFactory) {
    // Our method for obtaining connector factories from the server factory varies depending on the latter's type
    List<ConnectorFactory> appConnectorFactories;
    if (serverFactory instanceof DefaultServerFactory) {
        appConnectorFactories = ((DefaultServerFactory) serverFactory).getApplicationConnectors();
    } else if (serverFactory instanceof SimpleServerFactory) {
        appConnectorFactories = Collections.singletonList(((SimpleServerFactory) serverFactory).getConnector());
    } else {
        throw new IllegalStateException("Encountered an unexpected ServerFactory type");
    }

    return getHostAndPortFromConnectorFactories(appConnectorFactories);
}
 
Example #6
Source File: SelfHostAndPortModule.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Provides @Singleton @SelfAdminHostAndPort
public HostAndPort provideSelfAdminHostAndPort(ServerFactory serverFactory) {
    // Our method for obtaining connector factories from the server factory varies depending on the latter's type
    List<ConnectorFactory> adminConnectorFactories;
    if (serverFactory instanceof DefaultServerFactory) {
        adminConnectorFactories = ((DefaultServerFactory) serverFactory).getAdminConnectors();
    } else if (serverFactory instanceof SimpleServerFactory) {
        adminConnectorFactories = Collections.singletonList(((SimpleServerFactory) serverFactory).getConnector());
    } else {
        throw new IllegalStateException("Encountered an unexpected ServerFactory type");
    }

    return getHostAndPortFromConnectorFactories(adminConnectorFactories);
}
 
Example #7
Source File: RandomPortsListener.java    From dropwizard-guicey with MIT License 5 votes vote down vote up
@Override
public void onRun(final Configuration configuration,
                  final Environment environment,
                  final DropwizardTestSupport<Configuration> rule) throws Exception {
    final ServerFactory server = configuration.getServerFactory();
    if (server instanceof SimpleServerFactory) {
        ((HttpConnectorFactory) ((SimpleServerFactory) server).getConnector()).setPort(0);
    } else {
        final DefaultServerFactory dserv = (DefaultServerFactory) server;
        ((HttpConnectorFactory) dserv.getApplicationConnectors().get(0)).setPort(0);
        ((HttpConnectorFactory) dserv.getAdminConnectors().get(0)).setPort(0);
    }
}
 
Example #8
Source File: TenacityConfiguredBundle.java    From tenacity with Apache License 2.0 5 votes vote down vote up
protected void configureHystrix(T configuration, Environment environment) {
    final ServerFactory serverFactory = configuration.getServerFactory();
    final Duration shutdownGracePeriod =
            (serverFactory instanceof AbstractServerFactory)
            ? ((AbstractServerFactory) serverFactory).getShutdownGracePeriod()
            : Duration.seconds(30L);
    environment.lifecycle().manage(new ManagedHystrix(shutdownGracePeriod));
}
 
Example #9
Source File: CasBlobStoreTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public void setup() throws Exception {
    _lifeCycle = new SimpleLifeCycleRegistry();
    _healthChecks = mock(HealthCheckRegistry.class);

    // Start test instance of ZooKeeper in the current JVM
    TestingServer testingServer = new TestingServer();
    _lifeCycle.manage(testingServer);

    // Connect to ZooKeeper
    RetryPolicy retry = new BoundedExponentialBackoffRetry(100, 1000, 5);
    final CuratorFramework curator = CuratorFrameworkFactory.newClient(testingServer.getConnectString(), retry);
    _lifeCycle.manage(curator).start();

    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            bind(LifeCycleRegistry.class).toInstance(_lifeCycle);
            bind(HealthCheckRegistry.class).toInstance(_healthChecks);
            bind(TaskRegistry.class).toInstance(mock(TaskRegistry.class));

            bind(BlobStoreConfiguration.class).toInstance(new BlobStoreConfiguration()
                    .setValidTablePlacements(ImmutableSet.of(TABLE_PLACEMENT))
                    .setCassandraClusters(ImmutableMap.of(
                            "media_global", new TestCassandraConfiguration("media_global", "ugc_blob"))));

            DataStoreConfiguration dataStoreConfiguration = new DataStoreConfiguration()
                    .setValidTablePlacements(ImmutableSet.of("app_global:sys", "ugc_global:ugc"))
                    .setCassandraClusters(ImmutableMap.of(
                            "ugc_global", new TestCassandraConfiguration("ugc_global", "ugc_delta_v2"),
                            "app_global", new TestCassandraConfiguration("app_global", "sys_delta_v2")))
                    .setHistoryTtl(Duration.ofDays(2));

            bind(DataStoreConfiguration.class).toInstance(dataStoreConfiguration);

            bind(String.class).annotatedWith(SystemTablePlacement.class).toInstance("app_global:sys");

            bind(DataStore.class).annotatedWith(SystemDataStore.class).toInstance(mock(DataStore.class));
            bind(BlobStore.class).annotatedWith(SystemBlobStore.class).toInstance(mock(BlobStore.class));
            bind(JobService.class).toInstance(mock(JobService.class));
            bind(JobHandlerRegistry.class).toInstance(mock(JobHandlerRegistry.class));

            bind(DataCenterConfiguration.class).toInstance(new DataCenterConfiguration()
                    .setCurrentDataCenter("datacenter1")
                    .setSystemDataCenter("datacenter1")
                    .setDataCenterServiceUri(URI.create("http://localhost:8080"))
                    .setDataCenterAdminUri(URI.create("http://localhost:8080")));

            bind(CqlDriverConfiguration.class).toInstance(new CqlDriverConfiguration());

            bind(String.class).annotatedWith(ServerCluster.class).toInstance("local_default");
            bind(String.class).annotatedWith(InvalidationService.class).toInstance("emodb-cachemgr");

            bind(CuratorFramework.class).annotatedWith(Global.class).toInstance(curator);
            bind(CuratorFramework.class).annotatedWith(BlobStoreZooKeeper.class)
                    .toInstance(ZKNamespaces.usingChildNamespace(curator, "applications/emodb-blob"));
            bind(CuratorFramework.class).annotatedWith(DataStoreZooKeeper.class)
                    .toInstance(ZKNamespaces.usingChildNamespace(curator, "applications/emodb-sor"));
            bind(CuratorFramework.class).annotatedWith(GlobalFullConsistencyZooKeeper.class)
                    .toInstance(ZKNamespaces.usingChildNamespace(curator, "applications/emodb-fct"));

            bind(new TypeLiteral<Supplier<Boolean>>(){}).annotatedWith(CqlForScans.class)
                    .toInstance(Suppliers.ofInstance(true));
            bind(new TypeLiteral<Supplier<Boolean>>(){}).annotatedWith(CqlForMultiGets.class)
                    .toInstance(Suppliers.ofInstance(true));

            bind(ServerFactory.class).toInstance(new SimpleServerFactory());

            bind(ServiceRegistry.class).toInstance(mock(ServiceRegistry.class));

            bind(Clock.class).toInstance(Clock.systemDefaultZone());

            bind(String.class).annotatedWith(CompControlApiKey.class).toInstance("CompControlApiKey");
            bind(CompactionControlSource.class).annotatedWith(LocalCompactionControl.class).toInstance(mock(CompactionControlSource.class));

            bind(Environment.class).toInstance(mock(Environment.class));

            EmoServiceMode serviceMode = EmoServiceMode.STANDARD_ALL;
            install(new SelfHostAndPortModule());
            install(new DataCenterModule(serviceMode));
            install(new CacheManagerModule());
            install(new DataStoreModule(serviceMode));
            install(new BlobStoreModule(serviceMode, "bv.emodb.blob", new MetricRegistry()));
        }
    });
    _store = injector.getInstance(BlobStore.class);

    _lifeCycle.start();
    TableOptions options = new TableOptionsBuilder().setPlacement(TABLE_PLACEMENT).build();
    Audit audit = new AuditBuilder().setLocalHost().build();
    _store.createTable(TABLE, options, ImmutableMap.of(), audit);
}
 
Example #10
Source File: CasDataStoreTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public void setup() throws Exception {
    _lifeCycle = new SimpleLifeCycleRegistry();
    _healthChecks = mock(HealthCheckRegistry.class);

    // Start test instance of ZooKeeper in the current JVM
    TestingServer testingServer = new TestingServer();
    _lifeCycle.manage(testingServer);

    // Connect to ZooKeeper
    final CuratorFramework curator = CuratorFrameworkFactory.newClient(testingServer.getConnectString(),
            new BoundedExponentialBackoffRetry(100, 1000, 5));
    _lifeCycle.manage(curator).start();

    // Setup the DataStoreModule
    Injector injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            bind(LifeCycleRegistry.class).toInstance(_lifeCycle);
            bind(HealthCheckRegistry.class).toInstance(_healthChecks);
            bind(TaskRegistry.class).toInstance(mock(TaskRegistry.class));

            DataStoreConfiguration dataStoreConfiguration = new DataStoreConfiguration()
                    .setValidTablePlacements(ImmutableSet.of("app_global:sys", "ugc_global:ugc"))
                    .setCassandraClusters(ImmutableMap.<String, CassandraConfiguration>of(
                            "ugc_global", new TestCassandraConfiguration("ugc_global", "ugc_delta_v2"),
                            "app_global", new TestCassandraConfiguration("app_global", "sys_delta_v2")))
                    .setHistoryTtl(Duration.ofDays(2));

            bind(DataStoreConfiguration.class).toInstance(dataStoreConfiguration);

            bind(String.class).annotatedWith(SystemTablePlacement.class).toInstance("app_global:sys");

            bind(DataStore.class).annotatedWith(SystemDataStore.class).toInstance(mock(DataStore.class));
            bind(JobService.class).toInstance(mock(JobService.class));
            bind(JobHandlerRegistry.class).toInstance(mock(JobHandlerRegistry.class));

            bind(DataCenterConfiguration.class).toInstance(new DataCenterConfiguration()
                    .setCurrentDataCenter("datacenter1")
                    .setSystemDataCenter("datacenter1")
                    .setDataCenterServiceUri(URI.create("http://localhost:8080"))
                    .setDataCenterAdminUri(URI.create("http://localhost:8080")));

            bind(CqlDriverConfiguration.class).toInstance(new CqlDriverConfiguration());

            bind(KeyspaceDiscovery.class).annotatedWith(Names.named("blob")).toInstance(mock(KeyspaceDiscovery.class));
            bind(String.class).annotatedWith(ServerCluster.class).toInstance("local_default");

            bind(String.class).annotatedWith(ReplicationKey.class).toInstance("password");
            bind(String.class).annotatedWith(InvalidationService.class).toInstance("emodb-cachemgr");

            bind(CuratorFramework.class).annotatedWith(Global.class).toInstance(curator);
            bind(CuratorFramework.class).annotatedWith(DataStoreZooKeeper.class)
                    .toInstance(ZKNamespaces.usingChildNamespace(curator, "applications/emodb-sor"));
            bind(CuratorFramework.class).annotatedWith(GlobalFullConsistencyZooKeeper.class)
                    .toInstance(ZKNamespaces.usingChildNamespace(curator, "applications/emodb-fct"));

            bind(new TypeLiteral<Supplier<Boolean>>(){}).annotatedWith(CqlForScans.class)
                    .toInstance(Suppliers.ofInstance(true));
            bind(new TypeLiteral<Supplier<Boolean>>(){}).annotatedWith(CqlForMultiGets.class)
                    .toInstance(Suppliers.ofInstance(true));

            bind(ServerFactory.class).toInstance(new SimpleServerFactory());

            bind(ServiceRegistry.class).toInstance(mock(ServiceRegistry.class));

            bind(Clock.class).toInstance(Clock.systemDefaultZone());

            bind(String.class).annotatedWith(CompControlApiKey.class).toInstance("CompControlApiKey");
            bind(CompactionControlSource.class).annotatedWith(LocalCompactionControl.class).toInstance(mock(CompactionControlSource.class));

            bind(Environment.class).toInstance(new Environment("emodb", Jackson.newObjectMapper(),
                    Validation.buildDefaultValidatorFactory().getValidator(),
                    new MetricRegistry(), ClassLoader.getSystemClassLoader()));

            EmoServiceMode serviceMode = EmoServiceMode.STANDARD_ALL;
            install(new SelfHostAndPortModule());
            install(new DataCenterModule(serviceMode));
            install(new CacheManagerModule());
            install(new DataStoreModule(serviceMode));
        }
    });
    _store = injector.getInstance(DataStore.class);
    _lifeCycle.start();

    Map<String, Object> template = Collections.emptyMap();
    _store.createTable(TABLE, new TableOptionsBuilder().setPlacement("ugc_global:ugc").build(), template, newAudit("create table"));
}
 
Example #11
Source File: FoxtrotModule.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
public ServerFactory serverFactory(FoxtrotServerConfiguration configuration) {
    return configuration.getServerFactory();
}
 
Example #12
Source File: TimbuctooConfiguration.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
@JsonProperty("server")
@Value.Default
public ServerFactory getServerFactory() {
  return new DefaultServerFactory();
}