List of usage examples for com.google.common.util.concurrent ServiceManager ServiceManager
public ServiceManager(Iterable<? extends Service> services)
From source file:gobblin.cluster.GobblinTaskRunner.java
public GobblinTaskRunner(String applicationName, String helixInstanceName, String applicationId, String taskRunnerId, Config config, Optional<Path> appWorkDirOptional) throws Exception { this.helixInstanceName = helixInstanceName; this.config = config; this.taskRunnerId = taskRunnerId; Configuration conf = HadoopUtils.newConfiguration(); this.fs = buildFileSystem(this.config, conf); String zkConnectionString = config.getString(GobblinClusterConfigurationKeys.ZK_CONNECTION_STRING_KEY); LOGGER.info("Using ZooKeeper connection string: " + zkConnectionString); this.helixManager = HelixManagerFactory.getZKHelixManager( config.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY), helixInstanceName, InstanceType.PARTICIPANT, zkConnectionString); Properties properties = ConfigUtils.configToProperties(config); TaskExecutor taskExecutor = new TaskExecutor(properties); TaskStateTracker taskStateTracker = new GobblinHelixTaskStateTracker(properties, this.helixManager); Path appWorkDir = appWorkDirOptional.isPresent() ? appWorkDirOptional.get() : GobblinClusterUtils.getAppWorkDirPath(this.fs, applicationName, applicationId); List<Service> services = Lists.newArrayList(taskExecutor, taskStateTracker, new JMXReportingService()); services.addAll(getServices());//from www .ja v a2 s. c om this.serviceManager = new ServiceManager(services); this.containerMetrics = buildContainerMetrics(this.config, properties, applicationName, this.taskRunnerId); // Register task factory for the Helix task state model Map<String, TaskFactory> taskFactoryMap = Maps.newHashMap(); taskFactoryMap.put(GOBBLIN_TASK_FACTORY_NAME, new GobblinHelixTaskFactory(this.containerMetrics, taskExecutor, taskStateTracker, this.fs, appWorkDir, config)); this.taskStateModelFactory = new TaskStateModelFactory(this.helixManager, taskFactoryMap); this.helixManager.getStateMachineEngine().registerStateModelFactory("Task", this.taskStateModelFactory); }
From source file:gobblin.yarn.GobblinWorkUnitRunner.java
public GobblinWorkUnitRunner(String applicationName, String helixInstanceName, ContainerId containerId, Config config, Optional<Path> appWorkDirOptional) throws Exception { this.helixInstanceName = helixInstanceName; this.config = config; this.containerId = containerId; ApplicationAttemptId applicationAttemptId = this.containerId.getApplicationAttemptId(); String applicationId = applicationAttemptId.getApplicationId().toString(); Configuration conf = HadoopUtils.newConfiguration(); FileSystem fs = buildFileSystem(this.config, conf); String zkConnectionString = config.getString(GobblinYarnConfigurationKeys.ZK_CONNECTION_STRING_KEY); LOGGER.info("Using ZooKeeper connection string: " + zkConnectionString); this.helixManager = HelixManagerFactory.getZKHelixManager( config.getString(GobblinYarnConfigurationKeys.HELIX_CLUSTER_NAME_KEY), helixInstanceName, InstanceType.PARTICIPANT, zkConnectionString); Properties properties = ConfigUtils.configToProperties(config); TaskExecutor taskExecutor = new TaskExecutor(properties); TaskStateTracker taskStateTracker = new GobblinHelixTaskStateTracker(properties, this.helixManager); Path appWorkDir = appWorkDirOptional.isPresent() ? appWorkDirOptional.get() : YarnHelixUtils.getAppWorkDirPath(fs, applicationName, applicationId); List<Service> services = Lists.newArrayList(); if (isLogSourcePresent()) { services.add(buildLogCopier(this.containerId, fs, appWorkDir)); }// w ww . ja v a2 s . c o m services.add(taskExecutor); services.add(taskStateTracker); if (config.hasPath(GobblinYarnConfigurationKeys.KEYTAB_FILE_PATH)) { LOGGER.info("Adding YarnContainerSecurityManager since login is keytab based"); services.add(new YarnContainerSecurityManager(config, fs, this.eventBus)); } this.serviceManager = new ServiceManager(services); this.containerMetrics = buildContainerMetrics(this.config, properties, applicationName, containerId); // Register task factory for the Helix task state model Map<String, TaskFactory> taskFactoryMap = Maps.newHashMap(); taskFactoryMap.put(GOBBLIN_TASK_FACTORY_NAME, new GobblinHelixTaskFactory(this.containerMetrics, taskExecutor, taskStateTracker, fs, appWorkDir)); this.taskStateModelFactory = new TaskStateModelFactory(this.helixManager, taskFactoryMap); this.helixManager.getStateMachineEngine().registerStateModelFactory("Task", this.taskStateModelFactory); this.metricRegistry = new MetricRegistry(); this.jmxReporter = JmxReporter.forRegistry(this.metricRegistry).convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS).build(); }
From source file:org.apache.gobblin.cluster.GobblinTaskRunner.java
public GobblinTaskRunner(String applicationName, String helixInstanceName, String applicationId, String taskRunnerId, Config config, Optional<Path> appWorkDirOptional) throws Exception { this.helixInstanceName = helixInstanceName; this.taskRunnerId = taskRunnerId; this.applicationName = applicationName; this.applicationId = applicationId; Configuration conf = HadoopUtils.newConfiguration(); this.fs = buildFileSystem(config, conf); this.appWorkPath = initAppWorkDir(config, appWorkDirOptional); this.config = saveConfigToFile(config); this.clusterName = this.config.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY); initHelixManager();//from w w w.j a va 2 s .c o m this.containerMetrics = buildContainerMetrics(); String builderStr = ConfigUtils.getString(this.config, GobblinClusterConfigurationKeys.TASK_RUNNER_SUITE_BUILDER, TaskRunnerSuiteBase.Builder.class.getName()); TaskRunnerSuiteBase.Builder builder = GobblinConstructorUtils.<TaskRunnerSuiteBase.Builder>invokeLongestConstructor( new ClassAliasResolver(TaskRunnerSuiteBase.Builder.class).resolveClass(builderStr), this.config); TaskRunnerSuiteBase suite = builder.setAppWorkPath(this.appWorkPath) .setContainerMetrics(this.containerMetrics).setFileSystem(this.fs) .setHelixManager(this.helixManager).setApplicationId(applicationId) .setApplicationName(applicationName).build(); this.taskStateModelFactory = createTaskStateModelFactory(suite.getTaskFactoryMap()); this.metrics = suite.getTaskMetrics(); this.metricContext = suite.getMetricContext(); this.services.addAll(suite.getServices()); this.services.addAll(getServices()); if (this.services.isEmpty()) { this.serviceManager = null; } else { this.serviceManager = new ServiceManager(services); } logger.debug( "GobblinTaskRunner: applicationName {}, helixInstanceName {}, applicationId {}, taskRunnerId {}, config {}, appWorkDir {}", applicationName, helixInstanceName, applicationId, taskRunnerId, config, appWorkDirOptional); }
From source file:org.ftccommunity.robotcore.RobotService.java
private ServiceManager generateServiceManager() { List<Class<?>> classList = ClassManager.getClasses(); LinkedList<com.google.common.util.concurrent.Service> services = new LinkedList<>(); for (Class<?> klazz : classList) { if (klazz.isAnnotationPresent(org.ftccommunity.annonations.RobotService.class)) { try { final com.google.common.util.concurrent.Service serviceInstance = //getServiceInstance((Class<com.google.common.util.concurrent.Service>) klazz); (com.google.common.util.concurrent.Service) SimpleDag.create(klazz, this); final String value = klazz.getAnnotation(org.ftccommunity.annonations.RobotService.class) .value();/*from ww w .j av a 2 s. c om*/ bind(value.equals("") ? klazz.getSimpleName() : value, serviceInstance); SimpleDag.hasMethodAnnotationsOf(Subscribe.class, serviceInstance.getClass()); new EventBus().register(serviceInstance); services.add(serviceInstance); Log.d(TAG, "Loaded service: " + klazz.getSimpleName()); } catch (SimpleDag.DagInstantiationException | ClassCastException ex) { Log.e(TAG, "Cannot load service " + klazz.getSimpleName() + ", due to exception", ex); } } } Log.d(TAG, services.size() + " service(s) have been discovered"); return new ServiceManager(services); }
From source file:gobblin.aws.GobblinAWSClusterLauncher.java
/** * Launch a new Gobblin cluster on AWS./*from w ww. j a v a 2 s . co m*/ * * @throws IOException If there's something wrong launching the cluster */ public void launch() throws IOException { this.eventBus.register(this); // Create Helix cluster and connect to it HelixUtils.createGobblinHelixCluster(this.zkConnectionString, this.helixClusterName); LOGGER.info("Created Helix cluster " + this.helixClusterName); connectHelixManager(); // Start all the services List<Service> services = Lists.newArrayList(); services.add(this.awsClusterSecurityManager); this.serviceManager = Optional.of(new ServiceManager(services)); this.serviceManager.get().startAsync(); // Core logic to launch cluster this.clusterId = getClusterId(); // TODO: Add cluster monitoring }
From source file:gobblin.yarn.GobblinYarnAppLauncher.java
/** * Launch a new Gobblin instance on Yarn. * * @throws IOException if there's something wrong launching the application * @throws YarnException if there's something wrong launching the application *//* ww w . j a v a2 s. c o m*/ public void launch() throws IOException, YarnException { this.eventBus.register(this); String clusterName = this.config.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY); HelixUtils.createGobblinHelixCluster( this.config.getString(GobblinClusterConfigurationKeys.ZK_CONNECTION_STRING_KEY), clusterName); LOGGER.info("Created Helix cluster " + clusterName); connectHelixManager(); startYarnClient(); this.applicationId = getApplicationId(); this.applicationStatusMonitor.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { eventBus.post(new ApplicationReportArrivalEvent( yarnClient.getApplicationReport(applicationId.get()))); } catch (YarnException | IOException e) { LOGGER.error( "Failed to get application report for Gobblin Yarn application " + applicationId.get(), e); eventBus.post(new GetApplicationReportFailureEvent(e)); } } }, 0, this.appReportIntervalMinutes, TimeUnit.MINUTES); List<Service> services = Lists.newArrayList(); if (this.config.hasPath(GobblinYarnConfigurationKeys.KEYTAB_FILE_PATH)) { LOGGER.info("Adding YarnAppSecurityManager since login is keytab based"); services.add(buildYarnAppSecurityManager()); } if (!this.config.hasPath(GobblinYarnConfigurationKeys.LOG_COPIER_DISABLE_DRIVER_COPY) || !this.config.getBoolean(GobblinYarnConfigurationKeys.LOG_COPIER_DISABLE_DRIVER_COPY)) { services.add(buildLogCopier(this.config, new Path(this.sinkLogRootDir, this.applicationName + Path.SEPARATOR + this.applicationId.get().toString()), GobblinClusterUtils.getAppWorkDirPath(this.fs, this.applicationName, this.applicationId.get().toString()))); } if (config.getBoolean(ConfigurationKeys.JOB_EXECINFO_SERVER_ENABLED_KEY)) { LOGGER.info("Starting the job execution info server since it is enabled"); Properties properties = ConfigUtils.configToProperties(config); JobExecutionInfoServer executionInfoServer = new JobExecutionInfoServer(properties); services.add(executionInfoServer); if (config.getBoolean(ConfigurationKeys.ADMIN_SERVER_ENABLED_KEY)) { LOGGER.info("Starting the admin UI server since it is enabled"); services.add(new AdminWebServer(properties, executionInfoServer.getAdvertisedServerUri())); } } else if (config.getBoolean(ConfigurationKeys.ADMIN_SERVER_ENABLED_KEY)) { LOGGER.warn("NOT starting the admin UI because the job execution info server is NOT enabled"); } this.serviceManager = Optional.of(new ServiceManager(services)); // Start all the services running in the ApplicationMaster this.serviceManager.get().startAsync(); }
From source file:org.apache.gobblin.yarn.GobblinYarnAppLauncher.java
/** * Launch a new Gobblin instance on Yarn. * * @throws IOException if there's something wrong launching the application * @throws YarnException if there's something wrong launching the application *///from ww w. jav a2s .c om public void launch() throws IOException, YarnException { this.eventBus.register(this); String clusterName = this.config.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY); HelixUtils.createGobblinHelixCluster( this.config.getString(GobblinClusterConfigurationKeys.ZK_CONNECTION_STRING_KEY), clusterName); LOGGER.info("Created Helix cluster " + clusterName); connectHelixManager(); startYarnClient(); this.applicationId = getApplicationId(); this.applicationStatusMonitor.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { eventBus.post(new ApplicationReportArrivalEvent( yarnClient.getApplicationReport(applicationId.get()))); } catch (YarnException | IOException e) { LOGGER.error( "Failed to get application report for Gobblin Yarn application " + applicationId.get(), e); eventBus.post(new GetApplicationReportFailureEvent(e)); } } }, 0, this.appReportIntervalMinutes, TimeUnit.MINUTES); List<Service> services = Lists.newArrayList(); if (this.config.hasPath(GobblinYarnConfigurationKeys.KEYTAB_FILE_PATH)) { LOGGER.info("Adding YarnAppSecurityManager since login is keytab based"); services.add(buildYarnAppSecurityManager()); } if (!this.config.hasPath(GobblinYarnConfigurationKeys.LOG_COPIER_DISABLE_DRIVER_COPY) || !this.config.getBoolean(GobblinYarnConfigurationKeys.LOG_COPIER_DISABLE_DRIVER_COPY)) { services.add(buildLogCopier(this.config, new Path(this.sinkLogRootDir, this.applicationName + Path.SEPARATOR + this.applicationId.get().toString()), GobblinClusterUtils.getAppWorkDirPath(this.fs, this.applicationName, this.applicationId.get().toString()))); } if (config.getBoolean(ConfigurationKeys.JOB_EXECINFO_SERVER_ENABLED_KEY)) { LOGGER.info("Starting the job execution info server since it is enabled"); Properties properties = ConfigUtils.configToProperties(config); JobExecutionInfoServer executionInfoServer = new JobExecutionInfoServer(properties); services.add(executionInfoServer); if (config.getBoolean(ConfigurationKeys.ADMIN_SERVER_ENABLED_KEY)) { LOGGER.info("Starting the admin UI server since it is enabled"); services.add(ServiceBasedAppLauncher.createAdminServer(properties, executionInfoServer.getAdvertisedServerUri())); } } else if (config.getBoolean(ConfigurationKeys.ADMIN_SERVER_ENABLED_KEY)) { LOGGER.warn("NOT starting the admin UI because the job execution info server is NOT enabled"); } this.serviceManager = Optional.of(new ServiceManager(services)); // Start all the services running in the ApplicationMaster this.serviceManager.get().startAsync(); }
From source file:org.jooby.scanner.Scanner.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void guavaServices(final Env env, final Binder binder, final Set<Class<Service>> serviceTypes) { Consumer<Class> guavaService = klass -> { binder.bind(klass).asEagerSingleton(); serviceTypes.add(klass);/* w w w . j a va2s . c o m*/ }; serviceTypes.forEach(guavaService); // lazy service manager AtomicReference<ServiceManager> sm = new AtomicReference<>(); Provider<ServiceManager> smProvider = sm::get; binder.bind(ServiceManager.class).toProvider(smProvider); // ask Guice for services, create ServiceManager and start services env.onStart(r -> { List<Service> services = serviceTypes.stream().map(r::require).collect(Collectors.toList()); sm.set(new ServiceManager(services)); sm.get().startAsync().awaitHealthy(); }); // stop services env.onStop(() -> sm.get().stopAsync().awaitStopped()); }
From source file:com.stackframe.sarariman.Sarariman.java
@Override public void contextInitialized(ServletContextEvent sce) { Logger.getRootLogger().addAppender(new ConsoleAppender()); final String applicationName = sce.getServletContext().getServletContextName(); logger.info("context initialized for " + applicationName + " serverInfo=" + sce.getServletContext().getServerInfo()); extensions.add(new SAICExtension()); try {/*from ww w . j a v a 2 s . co m*/ Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:comp/env"); DeploymentMode deploymentMode = DeploymentMode.valueOf((String) envContext.lookup("deploymentMode")); setupLogger(deploymentMode); Properties directoryProperties = lookupDirectoryProperties(envContext); directory = new LDAPDirectory(new InitialDirContext(directoryProperties), this); try { directorySynchronizer.synchronize(directory, getDataSource()); } catch (Exception e) { logger.error("trouble synchronizing directory with database", e); } initContext.rebind("sarariman.directory", directory); organizationHierarchy = new OrganizationHierarchyImpl(getDataSource(), directory); boolean inhibitEmail = (Boolean) envContext.lookup("inhibitEmail"); String twilioAccountSID = (String) envContext.lookup("twilioAccountSID"); String twilioAuthToken = (String) envContext.lookup("twilioAuthToken"); TwilioRestClient twilioClient = new TwilioRestClient(twilioAccountSID, twilioAuthToken); boolean inhibitSMS = (Boolean) envContext.lookup("inhibitSMS"); String SMSFrom = (String) envContext.lookup("SMSFrom"); try { SMS = new TwilioSMSGatewayImpl(twilioClient, PhoneNumberUtil.getInstance().parse(SMSFrom, "US"), inhibitSMS, backgroundDatabaseWriteExecutor, getDataSource()); } catch (NumberParseException pe) { throw new RuntimeException(pe); } emailDispatcher = new EmailDispatcher(lookupMailProperties(envContext), inhibitEmail, backgroundExecutor); logoURL = (String) envContext.lookup("logoURL"); mountPoint = (String) envContext.lookup("mountPoint"); clients = new ClientsImpl(getDataSource(), mountPoint); projects = new ProjectsImpl(getDataSource(), organizationHierarchy, directory, this); tasks = new TasksImpl(getDataSource(), getMountPoint(), projects); timesheetEntries = new TimesheetEntriesImpl(getDataSource(), directory, tasks, mountPoint); tickets = new TicketsImpl(getDataSource(), mountPoint); events = new EventsImpl(getDataSource(), mountPoint); vacations = new VacationsImpl(getDataSource(), directory); outOfOffice = new OutOfOfficeEntriesImpl(getDataSource(), directory); contacts = new ContactsImpl(getDataSource(), mountPoint); timesheets = new TimesheetsImpl(this, mountPoint); errors = new ErrorsImpl(getDataSource(), mountPoint, directory); accessLog = new AccessLogImpl(getDataSource(), directory); workdays = new WorkdaysImpl(holidays); paidTimeOff = new PaidTimeOff(tasks); credits = new CreditsImpl(getDataSource()); invoices = new InvoicesImpl(getDataSource(), credits); laborProjections = new LaborProjectionsImpl(getDataSource(), directory, tasks, mountPoint); taskAssignments = new TaskAssignmentsImpl(directory, getDataSource(), mountPoint); defaultTaskAssignments = new DefaultTaskAssignmentsImpl(getDataSource(), tasks); loginCookies = new LoginCookies(getDataSource(), backgroundExecutor); locationLog = new LocationLogImpl(getDataSource(), directory, backgroundDatabaseWriteExecutor); String keyStorePath = (String) envContext.lookup("keyStorePath"); String keyStorePassword = (String) envContext.lookup("keyStorePassword"); xmpp = new VysperXMPPServer("stackframe.com", directory, new File(keyStorePath), keyStorePassword, backgroundExecutor, getDataSource(), backgroundDatabaseWriteExecutor); conferences = new ConferencesImpl(xmpp); services.add(xmpp); SMSXMPPGateway gateway = new SMSXMPPGateway(SMS, xmpp, directory, backgroundExecutor, getDataSource(), backgroundDatabaseWriteExecutor); services.add(gateway); } catch (NamingException ne) { throw new RuntimeException(ne); // FIXME: Is this the best thing to throw here? } serviceManager = new ServiceManager(services); serviceManager.startAsync(); cronJobs = new CronJobs(backgroundExecutor, this, directory, emailDispatcher); ServletContext servletContext = sce.getServletContext(); servletContext.setAttribute("sarariman", this); servletContext.setAttribute("directory", directory); cronJobs.start(); final String hostname = getHostname(); Runnable sendStartupEmailNotification = () -> { try { for (Employee employee : getAdministrators()) { String message = String.format("%s version %s has been started on %s at %s.", applicationName, getVersion(), hostname, mountPoint); emailDispatcher.send(employee.getEmail(), null, "sarariman started", message); SMS.send(employee.getMobile(), "Sarariman has been started."); } } catch (Exception e) { logger.error("trouble sending startup notification", e); } }; backgroundExecutor.execute(sendStartupEmailNotification); }
From source file:org.apache.ambari.server.controller.ControllerModule.java
/** * Initializes specially-marked interfaces that require injection. * <p/>/*from w ww . ja v a2s .co m*/ * An example of where this is needed is with a singleton that is headless; in * other words, it doesn't have any injections but still needs to be part of * the Guice framework. * <p/> * A second example of where this is needed is when classes require static * members that are available via injection. * <p/> * If {@code beanDefinitions} is empty or null this will scan * {@code org.apache.ambari.server} (currently) for any {@link EagerSingleton} * or {@link StaticallyInject} or {@link AmbariService} instances. * * @param beanDefinitions the set of bean definitions. If it is empty or * {@code null} scan will occur. * * @return the set of bean definitions that was found during scan if * {@code beanDefinitions} was null or empty. Else original * {@code beanDefinitions} will be returned. * */ // Method is protected and returns a set of bean definitions for testing convenience. @SuppressWarnings("unchecked") protected Set<BeanDefinition> bindByAnnotation(Set<BeanDefinition> beanDefinitions) { List<Class<? extends Annotation>> classes = Arrays.asList(EagerSingleton.class, StaticallyInject.class, AmbariService.class); if (null == beanDefinitions || beanDefinitions.size() == 0) { ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider( false); // match only singletons that are eager listeners for (Class<? extends Annotation> cls : classes) { scanner.addIncludeFilter(new AnnotationTypeFilter(cls)); } beanDefinitions = scanner.findCandidateComponents("org.apache.ambari.server"); } if (null == beanDefinitions || beanDefinitions.size() == 0) { LOG.warn("No instances of {} found to register", classes); return beanDefinitions; } Set<com.google.common.util.concurrent.Service> services = new HashSet<com.google.common.util.concurrent.Service>(); for (BeanDefinition beanDefinition : beanDefinitions) { String className = beanDefinition.getBeanClassName(); Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); if (null != clazz.getAnnotation(EagerSingleton.class)) { bind(clazz).asEagerSingleton(); LOG.debug("Binding singleton {} eagerly", clazz); } if (null != clazz.getAnnotation(StaticallyInject.class)) { requestStaticInjection(clazz); LOG.debug("Statically injecting {} ", clazz); } // Ambari services are registered with Guava if (null != clazz.getAnnotation(AmbariService.class)) { // safety check to ensure it's actually a Guava service if (!AbstractScheduledService.class.isAssignableFrom(clazz)) { String message = MessageFormat.format( "Unable to register service {0} because it is not an AbstractScheduledService", clazz); LOG.warn(message); throw new RuntimeException(message); } // instantiate the service, register as singleton via toInstance() AbstractScheduledService service = null; try { service = (AbstractScheduledService) clazz.newInstance(); bind((Class<AbstractScheduledService>) clazz).toInstance(service); services.add(service); LOG.debug("Registering service {} ", clazz); } catch (Exception exception) { LOG.error("Unable to register {} as a service", clazz, exception); throw new RuntimeException(exception); } } } ServiceManager manager = new ServiceManager(services); bind(ServiceManager.class).toInstance(manager); return beanDefinitions; }