Search in sources :

Example 1 with EmbeddedCounterManager

use of org.infinispan.counter.impl.manager.EmbeddedCounterManager in project infinispan by infinispan.

the class CounterResource method getCounter.

private CompletionStage<RestResponse> getCounter(RestRequest request) throws RestResponseException {
    String counterName = request.variables().get("counterName");
    String accept = request.getAcceptHeader();
    MediaType contentType = accept == null ? MediaType.TEXT_PLAIN : negotiateMediaType(accept);
    EmbeddedCounterManager counterManager = invocationHelper.getCounterManager();
    return counterManager.getConfigurationAsync(counterName).thenCompose(configuration -> {
        if (configuration == null)
            return notFoundResponseFuture();
        NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder().contentType(contentType).header(CACHE_CONTROL.toString(), CacheControl.noCache());
        CompletionStage<Long> response;
        if (configuration.type() == CounterType.WEAK) {
            response = getWeakCounter(counterName, counterManager).thenApply(WeakCounter::getValue);
        } else {
            response = getStrongCounter(counterName, counterManager).thenCompose(StrongCounter::getValue);
        }
        return response.thenApply(v -> responseBuilder.entity(Long.toString(v)).build());
    });
}
Also used : CounterManagerConfigurationBuilder(org.infinispan.counter.configuration.CounterManagerConfigurationBuilder) MediaType(org.infinispan.commons.dataconversion.MediaType) EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) NettyRestResponse(org.infinispan.rest.NettyRestResponse)

Example 2 with EmbeddedCounterManager

use of org.infinispan.counter.impl.manager.EmbeddedCounterManager in project infinispan by infinispan.

the class CounterResource method deleteCounter.

private CompletionStage<RestResponse> deleteCounter(RestRequest request) {
    String counterName = request.variables().get("counterName");
    EmbeddedCounterManager counterManager = invocationHelper.getCounterManager();
    return counterManager.getConfigurationAsync(counterName).thenCompose(configuration -> {
        if (configuration == null)
            return notFoundResponseFuture();
        return CompletableFuture.runAsync(() -> counterManager.undefineCounter(counterName), invocationHelper.getExecutor()).thenApply(ignore -> new NettyRestResponse.Builder().status(NO_CONTENT).build());
    });
}
Also used : EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) NettyRestResponse(org.infinispan.rest.NettyRestResponse)

Example 3 with EmbeddedCounterManager

use of org.infinispan.counter.impl.manager.EmbeddedCounterManager in project infinispan by infinispan.

the class CounterModuleLifecycle method registerCounterManager.

private static void registerCounterManager(EmbeddedCacheManager cacheManager, BasicComponentRegistry registry, GlobalConfiguration globalConfiguration) {
    if (log.isTraceEnabled())
        log.tracef("Registering counter manager.");
    EmbeddedCounterManager counterManager = new EmbeddedCounterManager(cacheManager);
    // This must happen before CacheManagerJmxRegistration starts
    registry.registerComponent(CounterManager.class, counterManager, true);
    if (globalConfiguration.jmx().enabled()) {
        try {
            CacheManagerJmxRegistration jmxRegistration = registry.getComponent(CacheManagerJmxRegistration.class).running();
            jmxRegistration.registerMBean(counterManager);
        } catch (Exception e) {
            throw log.jmxRegistrationFailed(e);
        }
    }
}
Also used : EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) CacheManagerJmxRegistration(org.infinispan.jmx.CacheManagerJmxRegistration)

Example 4 with EmbeddedCounterManager

use of org.infinispan.counter.impl.manager.EmbeddedCounterManager in project infinispan by infinispan.

the class CounterRequestProcessor method applyCounter.

private void applyCounter(HotRodHeader header, String counterName, BiConsumer<HotRodHeader, StrongCounter> applyStrong, BiConsumer<HotRodHeader, WeakCounter> applyWeak) {
    EmbeddedCounterManager counterManager = counterManager(header);
    CounterConfiguration config = counterManager.getConfiguration(counterName);
    if (config == null) {
        writeResponse(header, missingCounterResponse(header));
        return;
    }
    switch(config.type()) {
        case UNBOUNDED_STRONG:
        case BOUNDED_STRONG:
            applyStrong.accept(header, counterManager.getStrongCounter(counterName));
            break;
        case WEAK:
            applyWeak.accept(header, counterManager.getWeakCounter(counterName));
            break;
    }
}
Also used : EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration)

Example 5 with EmbeddedCounterManager

use of org.infinispan.counter.impl.manager.EmbeddedCounterManager in project infinispan by infinispan.

the class CounterResource method resetCounter.

private CompletionStage<RestResponse> resetCounter(RestRequest request) throws RestResponseException {
    String counterName = request.variables().get("counterName");
    EmbeddedCounterManager counterManager = invocationHelper.getCounterManager();
    return counterManager.getConfigurationAsync(counterName).thenCompose(configuration -> {
        if (configuration == null)
            return notFoundResponseFuture();
        CompletionStage<Void> result = configuration.type() == CounterType.WEAK ? counterManager.getWeakCounter(counterName).reset() : counterManager.getStrongCounter(counterName).reset();
        return result.thenApply(v -> new NettyRestResponse.Builder().status(NO_CONTENT).build());
    });
}
Also used : EmbeddedCounterManager(org.infinispan.counter.impl.manager.EmbeddedCounterManager) NettyRestResponse(org.infinispan.rest.NettyRestResponse)

Aggregations

EmbeddedCounterManager (org.infinispan.counter.impl.manager.EmbeddedCounterManager)6 NettyRestResponse (org.infinispan.rest.NettyRestResponse)3 Channel (io.netty.channel.Channel)1 ChannelInboundHandler (io.netty.channel.ChannelInboundHandler)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelOutboundHandler (io.netty.channel.ChannelOutboundHandler)1 ChannelMatcher (io.netty.channel.group.ChannelMatcher)1 CorsConfig (io.netty.handler.codec.http.cors.CorsConfig)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MediaType (org.infinispan.commons.dataconversion.MediaType)1 LogFactory (org.infinispan.commons.logging.LogFactory)1 EmbeddedCounterManagerFactory (org.infinispan.counter.EmbeddedCounterManagerFactory)1 CounterConfiguration (org.infinispan.counter.api.CounterConfiguration)1 CounterManagerConfigurationBuilder (org.infinispan.counter.configuration.CounterManagerConfigurationBuilder)1 CacheManagerJmxRegistration (org.infinispan.jmx.CacheManagerJmxRegistration)1 MAX_HEADER_SIZE (org.infinispan.rest.RestChannelInitializer.MAX_HEADER_SIZE)1 MAX_INITIAL_LINE_SIZE (org.infinispan.rest.RestChannelInitializer.MAX_INITIAL_LINE_SIZE)1