All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.scheduling.instrument.ExecutorServiceInstrumenter Maven / Gradle / Ivy

There is a newer version: 4.8.7
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.scheduling.instrument;

import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.core.annotation.Internal;

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Wraps {@link ExecutorService} to instrument {@link Callable} and {@link Runnable}.
 *
 * @author Denis Stepanov
 * @since 1.3
 */
@Singleton
@Internal
final class ExecutorServiceInstrumenter implements BeanCreatedEventListener {

    private final List invocationInstrumenterFactories;

    /**
     * Creates new instance.
     *
     * @param invocationInstrumenterFactories invocation instrumentation factories.
     */
    ExecutorServiceInstrumenter(List invocationInstrumenterFactories) {
        this.invocationInstrumenterFactories = invocationInstrumenterFactories;
    }

    /**
     * Wraps {@link ExecutorService}.
     *
     * @param event The bean created event
     * @return wrapped instance
     */
    @Override
    public ExecutorService onCreated(BeanCreatedEvent event) {
        if (invocationInstrumenterFactories.isEmpty()) {
            return event.getBean();
        }
        Class beanType = event.getBeanDefinition().getBeanType();
        if (beanType == ExecutorService.class) {
            ExecutorService executorService = event.getBean();
            if (executorService instanceof ScheduledExecutorService) {
                return new InstrumentedScheduledExecutorService() {
                    @Override
                    public ScheduledExecutorService getTarget() {
                        return (ScheduledExecutorService) executorService;
                    }

                    @Override
                    public  Callable instrument(Callable task) {
                        return instrumentInvocation(task);
                    }

                    @Override
                    public Runnable instrument(Runnable command) {
                        return instrumentInvocation(command);
                    }
                };
            } else {
                return new InstrumentedExecutorService() {
                    @Override
                    public ExecutorService getTarget() {
                        return executorService;
                    }

                    @Override
                    public  Callable instrument(Callable task) {
                        return instrumentInvocation(task);
                    }

                    @Override
                    public Runnable instrument(Runnable command) {
                        return instrumentInvocation(command);
                    }
                };
            }
        } else {
            return event.getBean();
        }
    }

    private Runnable instrumentInvocation(Runnable runnable) {
        return InvocationInstrumenter.instrument(runnable, getInvocationInstrumenter());
    }

    private  Callable instrumentInvocation(Callable callable) {
        return InvocationInstrumenter.instrument(callable, getInvocationInstrumenter());
    }

    private List getInvocationInstrumenter() {
        List instrumenters = new ArrayList<>(invocationInstrumenterFactories.size());
        for (InvocationInstrumenterFactory instrumenterFactory : invocationInstrumenterFactories) {
            final InvocationInstrumenter instrumenter = instrumenterFactory.newInvocationInstrumenter();
            if (instrumenter != null) {
                instrumenters.add(instrumenter);
            }
        }
        return instrumenters;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy