Search in sources :

Example 1 with BeanIdentifier

use of io.micronaut.inject.BeanIdentifier in project micronaut-micrometer by micronaut-projects.

the class ExecutorServiceMetricsBinder method onCreated.

@Override
public ExecutorService onCreated(BeanCreatedEvent<ExecutorService> event) {
    ExecutorService executorService = event.getBean();
    // have to unwrap any Micronaut instrumentations to get the target
    ExecutorService unwrapped = executorService;
    while (unwrapped instanceof InstrumentedExecutorService) {
        InstrumentedExecutorService ies = (InstrumentedExecutorService) unwrapped;
        unwrapped = ies.getTarget();
    }
    // Netty EventLoopGroups require separate instrumentation.
    if (unwrapped.getClass().getName().startsWith("io.netty")) {
        return unwrapped;
    }
    MeterRegistry meterRegistry = meterRegistryProvider.get();
    BeanIdentifier beanIdentifier = event.getBeanIdentifier();
    // allow tags?
    List<Tag> tags = Collections.emptyList();
    // bind the service metrics
    new ExecutorServiceMetrics(unwrapped, beanIdentifier.getName(), tags).bindTo(meterRegistry);
    // allow timing
    final Timer timer = meterRegistry.timer("executor", Tags.concat(tags, "name", beanIdentifier.getName()));
    if (executorService instanceof ScheduledExecutorService) {
        return new InstrumentedScheduledExecutorService() {

            @Override
            public ScheduledExecutorService getTarget() {
                return (ScheduledExecutorService) executorService;
            }

            @Override
            public <T> Callable<T> instrument(Callable<T> task) {
                return timer.wrap(task);
            }

            @Override
            public Runnable instrument(Runnable command) {
                return timer.wrap(command);
            }
        };
    } else {
        return new InstrumentedExecutorService() {

            @Override
            public ExecutorService getTarget() {
                return executorService;
            }

            @Override
            public <T> Callable<T> instrument(Callable<T> task) {
                return timer.wrap(task);
            }

            @Override
            public Runnable instrument(Runnable command) {
                return timer.wrap(command);
            }
        };
    }
}
Also used : InstrumentedScheduledExecutorService(io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InstrumentedScheduledExecutorService(io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService) InstrumentedExecutorService(io.micronaut.scheduling.instrument.InstrumentedExecutorService) Callable(java.util.concurrent.Callable) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) ExecutorServiceMetrics(io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics) Timer(io.micrometer.core.instrument.Timer) BeanIdentifier(io.micronaut.inject.BeanIdentifier) InstrumentedExecutorService(io.micronaut.scheduling.instrument.InstrumentedExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InstrumentedScheduledExecutorService(io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Tag(io.micrometer.core.instrument.Tag)

Example 2 with BeanIdentifier

use of io.micronaut.inject.BeanIdentifier in project micronaut-core by micronaut-projects.

the class RefreshScope method getOrCreate.

@SuppressWarnings("unchecked")
@Override
public <T> T getOrCreate(BeanCreationContext<T> creationContext) {
    final BeanIdentifier id = creationContext.id();
    CreatedBean<?> created = refreshableBeans.computeIfAbsent(id, key -> {
        CreatedBean<T> createdBean = creationContext.create();
        locks.putIfAbsent(createdBean.bean(), new ReentrantReadWriteLock());
        return createdBean;
    });
    return (T) created.bean();
}
Also used : BeanIdentifier(io.micronaut.inject.BeanIdentifier) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 3 with BeanIdentifier

use of io.micronaut.inject.BeanIdentifier in project micronaut-core by micronaut-projects.

the class AbstractConcurrentCustomScope method getOrCreate.

@SuppressWarnings("unchecked")
@Override
public final <T> T getOrCreate(BeanCreationContext<T> creationContext) {
    r.lock();
    try {
        final Map<BeanIdentifier, CreatedBean<?>> scopeMap = getScopeMap(true);
        final BeanIdentifier id = creationContext.id();
        CreatedBean<?> createdBean = scopeMap.get(id);
        if (createdBean != null) {
            return (T) createdBean.bean();
        } else {
            r.unlock();
            w.lock();
            try {
                // re-check
                createdBean = scopeMap.get(id);
                if (createdBean != null) {
                    r.lock();
                    return (T) createdBean.bean();
                } else {
                    try {
                        createdBean = doCreate(creationContext);
                        scopeMap.put(id, createdBean);
                    } finally {
                        r.lock();
                    }
                    return (T) createdBean.bean();
                }
            } finally {
                w.unlock();
            }
        }
    } finally {
        r.unlock();
    }
}
Also used : BeanIdentifier(io.micronaut.inject.BeanIdentifier)

Aggregations

BeanIdentifier (io.micronaut.inject.BeanIdentifier)3 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 Tag (io.micrometer.core.instrument.Tag)1 Timer (io.micrometer.core.instrument.Timer)1 ExecutorServiceMetrics (io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics)1 InstrumentedExecutorService (io.micronaut.scheduling.instrument.InstrumentedExecutorService)1 InstrumentedScheduledExecutorService (io.micronaut.scheduling.instrument.InstrumentedScheduledExecutorService)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1