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);
}
};
}
}
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();
}
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();
}
}
Aggregations