com.netflix.governator.guice.BootstrapModule Java Examples

The following examples show how to use com.netflix.governator.guice.BootstrapModule. 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: TestAsyncSuroSender.java    From suro with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
    servers = TestConnectionPool.startServers(3);

    final Properties props = new Properties();
    props.setProperty(ClientConfig.LB_SERVER, TestConnectionPool.createConnectionString(servers));
    props.setProperty(ClientConfig.MINIMUM_RECONNECT_TIME_INTERVAL, "1");
    props.setProperty(ClientConfig.RECONNECT_INTERVAL, "1");
    props.setProperty(ClientConfig.RECONNECT_TIME_INTERVAL, "1");
    props.setProperty(ClientConfig.APP, "app");

    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();
}
 
Example #2
Source File: TestSyncSuroClient.java    From suro with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
    servers = TestConnectionPool.startServers(3);

    final Properties props = new Properties();
    props.setProperty(ClientConfig.LB_SERVER, TestConnectionPool.createConnectionString(servers));
    props.setProperty(ClientConfig.MINIMUM_RECONNECT_TIME_INTERVAL, "1");
    props.setProperty(ClientConfig.RECONNECT_INTERVAL, "1");
    props.setProperty(ClientConfig.RECONNECT_TIME_INTERVAL, "1");
    props.setProperty(ClientConfig.APP, "app");

    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();
}
 
Example #3
Source File: SuroServer.java    From suro with Apache License 2.0 6 votes vote down vote up
public static void create(AtomicReference<Injector> injector, final Properties properties, Module... modules) throws Exception {
    // Create the injector
    injector.set(LifecycleInjector.builder()
            .withBootstrapModule(
                    new BootstrapModule() {
                        @Override
                        public void configure(BootstrapBinder binder) {
                            binder.bindConfigurationProvider().toInstance(
                                    new PropertiesConfigurationProvider(properties));
                        }
                    }
            )
            .withModules(
                    new RoutingPlugin(),
                    new ServerSinkPlugin(),
                    new SuroInputPlugin(),
                    new SuroDynamicPropertyModule(),
                    new SuroModule(),
                    StatusServer.createJerseyServletModule()
            )
            .withAdditionalModules(modules)
            .build().createInjector());
}
 
Example #4
Source File: SuroClient.java    From suro with Apache License 2.0 6 votes vote down vote up
private Injector createInjector(final Properties properties) {
    injector = LifecycleInjector
            .builder()
            .withBootstrapModule(
                new BootstrapModule() {
                    @Override
                    public void configure(BootstrapBinder binder) {
                        binder.bindConfigurationProvider().toInstance(
                                new PropertiesConfigurationProvider(properties));
                    }
                }
            )
            .withModules(new SuroClientModule())
            .build().createInjector();
    LifecycleManager manager = injector.getInstance(LifecycleManager.class);

    try {
        manager.start();
    } catch (Exception e) {
        throw new RuntimeException("LifecycleManager cannot start with an exception: " + e.getMessage(), e);
    }
    return injector;
}
 
Example #5
Source File: TestAsyncSuroClientWithNonExistentFilePath.java    From suro with Apache License 2.0 6 votes vote down vote up
private void setupFile(final Properties props, String filePath) throws Exception {
    servers = TestConnectionPool.startServers(3);

    props.put(ClientConfig.LB_SERVER, TestConnectionPool.createConnectionString(servers));

    props.put(ClientConfig.ASYNC_FILEQUEUE_PATH, filePath);
    props.put(ClientConfig.ASYNC_QUEUE_TYPE, "file");

    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();
}
 
Example #6
Source File: Bootstrap.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public Injector bootstrap() {
   try {
      // Construct the injector
      LifecycleInjectorBuilder builder = LifecycleInjector.builder()
         .withBootstrapModule(new BootstrapModule() {
            @Override
            public void configure(@Nullable BootstrapBinder binder) {
               Preconditions.checkNotNull(binder);
               binder.bindConfigurationProvider().to(AgentConfigurationProvider.class);
            }
         })
         .withAdditionalBootstrapModules(boostrapModules);

      builder.withAdditionalModuleClasses(moduleClasses);
      if(!modules.isEmpty()) {
         builder.withAdditionalModules(modules);
      }

      LifecycleInjector inj = builder.build();
      Injector result = inj.createInjector();
      LifeCycleService.setState(LifeCycle.STARTING_UP, LifeCycle.STARTED);
      return result;
   } catch(Exception ex) {
      throw new BootException("error bootstrapping", ex);
   }
}
 
Example #7
Source File: TestConnectionOutPool.java    From suro with Apache License 2.0 6 votes vote down vote up
public void setup() throws Exception {
    servers = TestConnectionPool.startServers(1);

    props.put(ClientConfig.LB_SERVER, TestConnectionPool.createConnectionString(servers));
    props.put(ClientConfig.CONNECTION_TIMEOUT, Integer.toString(Integer.MAX_VALUE));

    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                }
            })
            .withAdditionalModules(new AbstractModule() {
                @Override
                protected void configure() {
                    bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            })
            .build().createInjector();
    injector.getInstance(LifecycleManager.class).start();

    pool = injector.getInstance(ConnectionPool.class);
    assertEquals(pool.getPoolSize(), 1);
}
 
Example #8
Source File: BootUtils.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static Injector initialize(@Nullable IrisHalInternal hal, File basePath, Collection<String> configFiles) {
   Set<File> configs = resolveConfigs(configFiles);

   if (hal != null) {
      IrisHal.start(basePath, configs, hal);
   } else {
      IrisHal.start(basePath, configs);
   }

   Collection<BootstrapModule> bmodules = new LinkedList<>();
   bmodules.addAll(IrisHal.getBootstrapModules());

   Collection<Class<? extends BootstrapModule>> bmoduleClasses = new LinkedList<>();
   bmoduleClasses.addAll(IrisHal.getBootstrapModuleClasses());

   Collection<Module> modules = new LinkedList<>();
   modules.addAll(IrisHal.getApplicationModules());

   Collection<Class<? extends Module>> moduleClasses = new LinkedList<>();
   moduleClasses.addAll(EXTRA);
   moduleClasses.addAll(IrisHal.getApplicationModuleClasses());
   moduleClasses.add(MessagesModule.class);
   moduleClasses.add(ProtocolMessagesModule.class);
   moduleClasses.add(GsonModule.class);
   moduleClasses.add(AgentModule.class);

   Bootstrap.Builder builder = Bootstrap.builder();
   builder.withBootstrapModules(bmodules);
   builder.withBootstrapModuleClasses(bmoduleClasses);
   builder.withModules(modules);
   builder.withModuleClasses(moduleClasses);

   Injector injector = builder.build().bootstrap();
   ServiceLocator.init(GuiceServiceLocator.create(injector));

   Runtime.getRuntime().addShutdownHook(new Thread(IrisShutdownHook.INSTANCE));
   return injector;
}
 
Example #9
Source File: Bootstrap.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public Injector bootstrap() throws BootstrapException {
   try {
      final Properties merged = mergeProperties();

      LifecycleInjectorBuilder builder = LifecycleInjector.builder()
         .withBootstrapModule(new BootstrapModule() {
            @Override
            public void configure(BootstrapBinder binder) {
               Names.bindProperties(binder, merged);
               binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(merged));
            }
         })
         .withAdditionalBootstrapModules(boostrapModules);

      if(merged.containsKey(PROP_MODULES)) {
         String [] classnames = StringUtils.split(merged.getProperty(PROP_MODULES));
         Set<Class<? extends Module>> modules = classnamesToClasses(Arrays.asList(classnames));
         modules.addAll(moduleClasses);
         builder.withAdditionalModuleClasses(modules);
      }
      else if(!moduleClasses.isEmpty()) {
         builder.withAdditionalModuleClasses(moduleClasses);
      }
      if(!modules.isEmpty()) {
         builder.withAdditionalModules(modules);
      }

      return builder.build().createInjector();
   } catch(Exception e) {
      throw new BootstrapException("Error bootstrapping the injection context", e);
   }
}
 
Example #10
Source File: AbstractKaryonServer.java    From karyon with Apache License 2.0 5 votes vote down vote up
public final void startWithAdditionalBootstrapModules(BootstrapModule... additionalBootstrapModules) {
    BootstrapModule[] applicableBootstrapModules = this.bootstrapModules;
    if (null != additionalBootstrapModules && additionalBootstrapModules.length != 0) {
        applicableBootstrapModules = Arrays.copyOf(bootstrapModules, bootstrapModules.length + additionalBootstrapModules.length);
        System.arraycopy(additionalBootstrapModules, 0, applicableBootstrapModules, bootstrapModules.length, additionalBootstrapModules.length);
    }

    injector = newInjector(applicableBootstrapModules);

    startLifecycleManager();
    _start();
}
 
Example #11
Source File: TestAsyncSuroClient.java    From suro with Apache License 2.0 5 votes vote down vote up
private void setupMemory(final Properties props) throws Exception {
    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();
}
 
Example #12
Source File: TestAsyncSuroClient.java    From suro with Apache License 2.0 5 votes vote down vote up
private void setupFile(final Properties props) throws Exception {
    props.put(ClientConfig.LB_SERVER, TestConnectionPool.createConnectionString(servers));
    props.put(ClientConfig.ASYNC_FILEQUEUE_PATH, tempDir.newFolder().getAbsolutePath());
    props.put(ClientConfig.ASYNC_QUEUE_TYPE, "file");

    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();
}
 
Example #13
Source File: Karyon.java    From karyon with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link KaryonServer} that has a single HTTP server instance which delegates all request
 * handling to {@link RequestHandler}.
 * The {@link HttpServer} is created using {@link KaryonTransport#newHttpServer(int, HttpRequestHandler)}
 *
 * @param port Port for the server.
 * @param handler Request Handler
 * @param bootstrapModules Additional bootstrapModules if any.
 *
 * @return {@link KaryonServer} which is to be used to start the created server.
 */
public static KaryonServer forRequestHandler(int port, final RequestHandler<ByteBuf, ByteBuf> handler,
                                             BootstrapModule... bootstrapModules) {
    HttpServer<ByteBuf, ByteBuf> httpServer =
            KaryonTransport.newHttpServer(port, new RequestHandler<ByteBuf, ByteBuf>() {
                @Override
                public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                                               HttpServerResponse<ByteBuf> response) {
                    return handler.handle(request, response);
                }
            });
    return new RxNettyServerBackedServer(httpServer, bootstrapModules);
}
 
Example #14
Source File: Karyon.java    From karyon with Apache License 2.0 5 votes vote down vote up
public static BootstrapModule toBootstrapModule(final Module... modules) {
    if (null == modules) {
        return null;
    }
    return new BootstrapModule() {
        @Override
        public void configure(BootstrapBinder binder) {
            binder.includeModules(modules);
        }

    };
}
 
Example #15
Source File: Karyon.java    From karyon with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
public static BootstrapModule toBootstrapModule(final Class<? extends Module>... moduleClasses) {
    if (null == moduleClasses) {
        return null;
    }
    return new BootstrapModule() {
        @Override
        public void configure(BootstrapBinder binder) {
            binder.include(Lists.newArrayList(moduleClasses));
        }

    };
}
 
Example #16
Source File: Karyon.java    From karyon with Apache License 2.0 5 votes vote down vote up
public static BootstrapModule toBootstrapModule(final Class<? extends Module> moduleClass) {
    if (null == moduleClass) {
        return null;
    }
    return new BootstrapModule() {
        @Override
        public void configure(BootstrapBinder binder) {
            binder.include(moduleClass);
        }

    };
}
 
Example #17
Source File: TestConnectionPool.java    From suro with Apache License 2.0 5 votes vote down vote up
private void createInjector() throws Exception {
    props.put(ClientConfig.LB_SERVER, createConnectionString(servers));

    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                    binder.bind(ILoadBalancer.class).to(StaticLoadBalancer.class);
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();
}
 
Example #18
Source File: TestMessageSetProcessor.java    From suro with Apache License 2.0 5 votes vote down vote up
private void testQueue(final Properties props) throws Exception {
    injector = LifecycleInjector.builder()
            .withBootstrapModule(new BootstrapModule() {
                @Override
                public void configure(BootstrapBinder binder) {
                    binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
                }
            }).build().createInjector();
    injector.getInstance(LifecycleManager.class).start();

    MessageSetProcessor queue = injector.getInstance(MessageSetProcessor.class);

    assertEquals(queue.getQueueSize(), 0);
    assertEquals(queue.getStatus(), ServiceStatus.ALIVE);

    TMessageSet messageSet = TestConnectionPool.createMessageSet(100);
    assertEquals(queue.process(messageSet).getResultCode(), ResultCode.OK);

    assertEquals(queue.getQueueSize(), 1);
    assertEquals(queue.poll(1, TimeUnit.MILLISECONDS), messageSet);
    assertEquals(queue.getQueueSize(), 0);

    queue.stopTakingTraffic();
    assertEquals(queue.process(messageSet).getResultCode(), ResultCode.OTHER_ERROR);

    queue.startTakingTraffic();
    assertEquals(queue.getStatus(), ServiceStatus.ALIVE);

    assertEquals(queue.process(messageSet).getResultCode(), ResultCode.OK);

    injector.getInstance(LifecycleManager.class).close();
}
 
Example #19
Source File: RxNettyServerBackedServer.java    From karyon with Apache License 2.0 4 votes vote down vote up
RxNettyServerBackedServer(AbstractServer rxNettyServer, BootstrapModule... bootstrapModules) {
    super(RxNettyServerBackedServer.class, bootstrapModules);
    this.rxNettyServer = rxNettyServer;
}
 
Example #20
Source File: AbstractKaryonServer.java    From karyon with Apache License 2.0 4 votes vote down vote up
public AbstractKaryonServer(BootstrapModule... bootstrapModules) {
    this.bootstrapModules = bootstrapModules;
}
 
Example #21
Source File: TestInputManager.java    From suro with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    int statusPort = TestUtils.pickPort();
    int serverPort = TestUtils.pickPort();

    final Properties props = new Properties();
    props.put("SuroServer.statusServerPort", Integer.toString(statusPort));
    props.put("SuroServer.port", Integer.toString(serverPort));

    Injector injector = LifecycleInjector.builder().withBootstrapModule(new BootstrapModule() {
        @Override
        public void configure(BootstrapBinder binder) {
            binder.bindConfigurationProvider().toInstance(new PropertiesConfigurationProvider(props));
        }
    }).withModules(
            new SuroInputPlugin(),
            new AbstractModule() {
                @Override
                protected void configure() {
                    bind(ObjectMapper.class).to(DefaultObjectMapper.class);
                }
            }
    ).build().createInjector();

    LifecycleManager lifecycleManager = injector.getInstance(LifecycleManager.class);
    lifecycleManager.start();

    InputManager inputManager = new InputManager();
    List<SuroInput> inputList = injector.getInstance(ObjectMapper.class).readValue(
            inputConfig,
            new TypeReference<List<SuroInput>>() {
            });
    inputManager.set(inputList);
    assertNotNull(inputManager.getInput("thrift"));
    assertNotNull(inputManager.getInput("kafka_topic-kafka1"));

    inputList = injector.getInstance(ObjectMapper.class).readValue(
            addInputConfig,
            new TypeReference<List<SuroInput>>() {
            });
    inputManager.set(inputList);
    assertNotNull(inputManager.getInput("thrift"));
    assertNotNull(inputManager.getInput("kafka_topic-kafka1"));
    assertNotNull(inputManager.getInput("kafka_topic-kafka2"));


    inputList = injector.getInstance(ObjectMapper.class).readValue(
            inputConfig,
            new TypeReference<List<SuroInput>>() {
            });
    inputManager.set(inputList);
    assertNotNull(inputManager.getInput("thrift"));
    assertNotNull(inputManager.getInput("kafka_topic-kafka1"));
}
 
Example #22
Source File: KaryonServerBackedServer.java    From karyon with Apache License 2.0 4 votes vote down vote up
KaryonServerBackedServer(AbstractKaryonServer delegate, BootstrapModule... bootstrapModules) {
    this.delegate = delegate;
    this.bootstrapModules = bootstrapModules;
}
 
Example #23
Source File: ShutdownModule.java    From karyon with Apache License 2.0 4 votes vote down vote up
public static BootstrapModule asBootstrapModule() {
    return asBootstrapModule(7002);
}
 
Example #24
Source File: ShutdownModule.java    From karyon with Apache License 2.0 4 votes vote down vote up
public static BootstrapModule asBootstrapModule(final int port) {
    return Karyon.toBootstrapModule(new ShutdownModule(port));
}
 
Example #25
Source File: MainClassBasedServer.java    From karyon with Apache License 2.0 4 votes vote down vote up
protected MainClassBasedServer(Class<?> mainClass, BootstrapModule... bootstrapModules) {
    super(bootstrapModules);
    this.mainClass = mainClass;
}
 
Example #26
Source File: MainClassBasedServer.java    From karyon with Apache License 2.0 4 votes vote down vote up
@Override
protected Injector newInjector(BootstrapModule... applicableBootstrapModules) {
    return LifecycleInjector.bootstrap(mainClass, applicableBootstrapModules);
}
 
Example #27
Source File: JerseyBlockingTest.java    From karyon with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setUpBefore() throws Exception {
  server = Karyon.forApplication( JerseyBlockingModule.class, (BootstrapModule[])null );
    
  server.start();
}
 
Example #28
Source File: KaryonEurekaModule.java    From karyon with Apache License 2.0 4 votes vote down vote up
public static BootstrapModule asBootstrapModule() {
    return Karyon.toBootstrapModule(KaryonEurekaModule.class);
}
 
Example #29
Source File: IrisHal.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static Collection<? extends BootstrapModule> getBootstrapModules() {
   return get().getBootstrapModules();
}
 
Example #30
Source File: IrisHal.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static Collection<Class<? extends BootstrapModule>> getBootstrapModuleClasses() {
   return get().getBootstrapModuleClasses();
}