Search in sources :

Example 1 with Metrics

use of net.socialgamer.cah.metrics.Metrics in project PretendYoureXyzzy by ajanata.

the class CahModule method configure.

@Override
protected void configure() {
    bind(Integer.class).annotatedWith(GameId.class).toProvider(GameManager.class);
    /*
     * A mutable Set of IP addresses (in String format) which are banned. This Set is
     * thread-safe.
     */
    bind(new TypeLiteral<Set<String>>() {
    }).annotatedWith(BanList.class).toInstance(Collections.synchronizedSet(new HashSet<String>()));
    bind(Properties.class).toInstance(properties);
    // this is only so injected constructors can log
    StartupUtils.reconfigureLogging(context);
    // FIXME huge hack.
    StartupUtils.reloadProperties(context, properties);
    final String metricsClassName = properties.getProperty("pyx.metrics.impl");
    try {
        @SuppressWarnings("unchecked") final Class<? extends Metrics> metricsClass = (Class<? extends Metrics>) Class.forName(metricsClassName);
        bind(Metrics.class).to(metricsClass);
    } catch (final ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    bind(Date.class).annotatedWith(ServerStarted.class).toInstance(new Date());
    bind(String.class).annotatedWith(UniqueId.class).toProvider(UniqueIds.class);
    install(new FactoryModuleBuilder().build(User.Factory.class));
    final ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(2 * Runtime.getRuntime().availableProcessors(), new ThreadFactory() {

        final AtomicInteger threadCount = new AtomicInteger();

        @Override
        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r);
            t.setDaemon(true);
            t.setName("timer-task-" + threadCount.incrementAndGet());
            return t;
        }
    });
    threadPool.setRemoveOnCancelPolicy(true);
    bind(ScheduledThreadPoolExecutor.class).toInstance(threadPool);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) FactoryModuleBuilder(com.google.inject.assistedinject.FactoryModuleBuilder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ThreadFactory(java.util.concurrent.ThreadFactory) Properties(java.util.Properties) Date(java.util.Date) Metrics(net.socialgamer.cah.metrics.Metrics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GameId(net.socialgamer.cah.data.GameManager.GameId) HashSet(java.util.HashSet)

Example 2 with Metrics

use of net.socialgamer.cah.metrics.Metrics in project PretendYoureXyzzy by ajanata.

the class StartupUtils method contextInitialized.

@Override
public void contextInitialized(final ServletContextEvent contextEvent) {
    final ServletContext context = contextEvent.getServletContext();
    reconfigureLogging(context);
    final Injector injector = getInjector(context);
    final ScheduledThreadPoolExecutor timer = injector.getInstance(ScheduledThreadPoolExecutor.class);
    final UserPingTask ping = injector.getInstance(UserPingTask.class);
    timer.scheduleAtFixedRate(ping, PING_START_DELAY, PING_CHECK_DELAY, TimeUnit.MILLISECONDS);
    final BroadcastGameListUpdateTask broadcastUpdate = injector.getInstance(BroadcastGameListUpdateTask.class);
    timer.scheduleAtFixedRate(broadcastUpdate, BROADCAST_UPDATE_START_DELAY, BROADCAST_UPDATE_DELAY, TimeUnit.MILLISECONDS);
    context.setAttribute(INJECTOR, injector);
    context.setAttribute(DATE_NAME, injector.getInstance(Key.get(Date.class, ServerStarted.class)));
    // this is called in the process of setting up the injector right now... ideally we wouldn't
    // need to do that there and can just do it here again.
    // reloadProperties(context);
    CardcastService.hackSslVerifier();
    // log that the server (re-)started to metrics logging (to flush all old games and users)
    injector.getInstance(Metrics.class).serverStart(injector.getInstance(Key.get(String.class, UniqueId.class)));
}
Also used : Metrics(net.socialgamer.cah.metrics.Metrics) BroadcastGameListUpdateTask(net.socialgamer.cah.task.BroadcastGameListUpdateTask) Injector(com.google.inject.Injector) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ServletContext(javax.servlet.ServletContext) UserPingTask(net.socialgamer.cah.task.UserPingTask)

Aggregations

ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 Metrics (net.socialgamer.cah.metrics.Metrics)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Injector (com.google.inject.Injector)1 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Set (java.util.Set)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServletContext (javax.servlet.ServletContext)1 GameId (net.socialgamer.cah.data.GameManager.GameId)1 BroadcastGameListUpdateTask (net.socialgamer.cah.task.BroadcastGameListUpdateTask)1 UserPingTask (net.socialgamer.cah.task.UserPingTask)1