Search in sources :

Example 41 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project spring-framework by spring-projects.

the class OrderedMessageSendingIntegrationTests method setup.

@BeforeEach
public void setup() {
    this.blockingSession = new BlockingWebSocketSession();
    this.blockingSession.setId("1");
    this.blockingSession.setOpen(true);
    this.executor = new ThreadPoolTaskExecutor();
    this.executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
    this.executor.setAllowCoreThreadTimeOut(true);
    this.executor.afterPropertiesSet();
    this.subscribableChannel = new ExecutorSubscribableChannel(this.executor);
    OrderedMessageChannelDecorator.configureInterceptor(this.subscribableChannel, true);
    this.orderedMessageChannel = new OrderedMessageChannelDecorator(this.subscribableChannel, logger);
}
Also used : ExecutorSubscribableChannel(org.springframework.messaging.support.ExecutorSubscribableChannel) BlockingWebSocketSession(org.springframework.web.socket.handler.BlockingWebSocketSession) OrderedMessageChannelDecorator(org.springframework.messaging.simp.broker.OrderedMessageChannelDecorator) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 42 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project pinpoint by naver.

the class NodeHistogramAppenderTest method appendNodeHistogram.

@Test
public void appendNodeHistogram() throws InterruptedException {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(16);
    executor.setMaxPoolSize(16);
    executor.setQueueCapacity(1024);
    int maxCount = 100;
    CompletableFuture[] array = new CompletableFuture[maxCount];
    AtomicBoolean timeout = new AtomicBoolean(false);
    for (int i = 0; i < maxCount; i++) {
        array[i] = makeCompletableFuture(i, timeout);
    }
    CompletableFuture completableFuture = CompletableFuture.allOf(array);
    try {
        completableFuture.get(100, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        timeout.set(Boolean.TRUE);
    }
    TimeUnit.SECONDS.sleep(3);
    System.out.println("END");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Test(org.junit.Test)

Example 43 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project perun by CESNET.

the class UserSynchronizer method synchronizeUsers.

public void synchronizeUsers() {
    PerunBl perun = (PerunBl) ldapcManager.getPerunBl();
    ThreadPoolTaskExecutor syncExecutor = new ThreadPoolTaskExecutor();
    int poolIndex;
    boolean shouldWriteExceptionLog = true;
    for (poolIndex = 0; poolIndex < perunUser.length; poolIndex++) {
        perunUser[poolIndex] = context.getBean("perunUser", PerunUser.class);
    }
    try {
        log.debug("Getting list of users");
        List<User> users = perun.getUsersManagerBl().getUsers(ldapcManager.getPerunSession());
        Set<Name> presentUsers = new HashSet<Name>(users.size());
        syncExecutor.setCorePoolSize(5);
        syncExecutor.setMaxPoolSize(8);
        // syncExecutor.setQueueCapacity(30);
        syncExecutor.initialize();
        poolIndex = 0;
        taskCount = new AtomicInteger(0);
        for (User user : users) {
            presentUsers.add(perunUser[0].getEntryDN(String.valueOf(user.getId())));
            log.debug("Getting list of attributes for user {}", user.getId());
            List<Attribute> attrs = new ArrayList<Attribute>();
            List<String> attrNames = fillPerunAttributeNames(perunUser[poolIndex].getPerunAttributeNames());
            try {
                // log.debug("Getting attribute {} for user {}", attrName, user.getId());
                attrs.addAll(perun.getAttributesManagerBl().getAttributes(ldapcManager.getPerunSession(), user, attrNames));
            /* very chatty
						if(attr == null) {
							log.debug("Got null for attribute {}", attrName);
						} else if (attr.getValue() == null) {
							log.debug("Got attribute {} with null value", attrName);
						} else {
							log.debug("Got attribute {} with value {}", attrName, attr.getValue().toString());
						}
						*/
            } catch (PerunRuntimeException e) {
                log.warn("Couldn't get attributes {} for user {}: {}", attrNames, user.getId(), e.getMessage());
                shouldWriteExceptionLog = false;
                throw new InternalErrorException(e);
            }
            log.debug("Got attributes {}", attrNames.toString());
            try {
                // log.debug("Synchronizing user {} with {} attrs", user, attrs.size());
                // perunUser.synchronizeEntry(user, attrs);
                log.debug("Getting list of member groups for user {}", user.getId());
                Set<Integer> voIds = new HashSet<>();
                List<Member> members = perun.getMembersManagerBl().getMembersByUser(ldapcManager.getPerunSession(), user);
                List<Group> groups = new ArrayList<Group>();
                for (Member member : members) {
                    if (member.getStatus().equals(Status.VALID)) {
                        voIds.add(member.getVoId());
                        groups.addAll(perun.getGroupsManagerBl().getAllGroupsWhereMemberIsActive(ldapcManager.getPerunSession(), member));
                    }
                }
                // log.debug("Synchronizing user {} with {} VOs and {} groups", user.getId(), voIds.size(), groups.size());
                // perunUser.synchronizeMembership(user, voIds, groups);
                log.debug("Getting list of extSources for user {}", user.getId());
                List<UserExtSource> userExtSources = perun.getUsersManagerBl().getUserExtSources(ldapcManager.getPerunSession(), user);
                List<Group> admin_groups = perun.getUsersManagerBl().getGroupsWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
                List<Vo> admin_vos = perun.getUsersManagerBl().getVosWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
                List<Facility> admin_facilities = perun.getFacilitiesManagerBl().getFacilitiesWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
                // log.debug("Synchronizing user {} with {} extSources", user.getId(), userExtSources.size());
                // perunUser.synchronizePrincipals(user, userExtSources);
                syncExecutor.execute(new SyncUsersWorker(poolIndex, user, attrs, voIds, groups, userExtSources, admin_groups, admin_vos, admin_facilities));
                taskCount.incrementAndGet();
            } catch (PerunRuntimeException e) {
                log.error("Error synchronizing user", e);
                shouldWriteExceptionLog = false;
                throw new InternalErrorException(e);
            }
            poolIndex = (poolIndex + 1) % perunUser.length;
        }
        try {
            removeOldEntries(perunUser[0], presentUsers, log);
        } catch (InternalErrorException e) {
            log.error("Error removing old user entries", e);
            shouldWriteExceptionLog = false;
            throw new InternalErrorException(e);
        }
    } catch (PerunRuntimeException e) {
        if (shouldWriteExceptionLog) {
            log.error("Error synchronizing users", e);
        }
        throw new InternalErrorException(e);
    } finally {
        // wait for all the tasks to get executed
        while (!syncExecutor.getThreadPoolExecutor().getQueue().isEmpty()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
        // wait for all the tasks to complete (for at most 10 seconds)
        for (int i = 0; i < 10 && taskCount.get() > 0; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
        syncExecutor.shutdown();
        for (poolIndex = 0; poolIndex < perunUser.length; poolIndex++) {
            perunUser[poolIndex] = null;
        }
    }
    if (wasThreadException) {
        throw new InternalErrorException("Error synchronizing user in executed thread");
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunUser(cz.metacentrum.perun.ldapc.model.PerunUser) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) ArrayList(java.util.ArrayList) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Name(javax.naming.Name) PerunRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.PerunRuntimeException) Vo(cz.metacentrum.perun.core.api.Vo) PerunUser(cz.metacentrum.perun.ldapc.model.PerunUser) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Facility(cz.metacentrum.perun.core.api.Facility)

Example 44 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project spring-boot by spring-projects.

the class TaskExecutorMetricsAutoConfigurationTests method taskExecutorsWithCustomNamesAreInstrumented.

@Test
void taskExecutorsWithCustomNamesAreInstrumented() {
    this.contextRunner.withBean("firstTaskExecutor", Executor.class, ThreadPoolTaskExecutor::new).withBean("customName", ThreadPoolTaskExecutor.class, ThreadPoolTaskExecutor::new).run((context) -> {
        MeterRegistry registry = context.getBean(MeterRegistry.class);
        Collection<FunctionCounter> meters = registry.get("executor.completed").functionCounters();
        assertThat(meters).map((meter) -> meter.getId().getTag("name")).containsExactlyInAnyOrder("firstTaskExecutor", "customName");
    });
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) TaskExecutionAutoConfiguration(org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration) MeterNotFoundException(io.micrometer.core.instrument.search.MeterNotFoundException) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MetricsRun(org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Test(org.junit.jupiter.api.Test) TaskSchedulingAutoConfiguration(org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration) Configuration(org.springframework.context.annotation.Configuration) BDDMockito.given(org.mockito.BDDMockito.given) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) FunctionCounter(io.micrometer.core.instrument.FunctionCounter) EnableScheduling(org.springframework.scheduling.annotation.EnableScheduling) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Mockito.mock(org.mockito.Mockito.mock) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) FunctionCounter(io.micrometer.core.instrument.FunctionCounter) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 45 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project spring-boot by spring-projects.

the class TaskExecutionAutoConfigurationTests method taskExecutorBuilderShouldApplyCustomizer.

@Test
void taskExecutorBuilderShouldApplyCustomizer() {
    this.contextRunner.withUserConfiguration(TaskExecutorCustomizerConfig.class).run((context) -> {
        TaskExecutorCustomizer customizer = context.getBean(TaskExecutorCustomizer.class);
        ThreadPoolTaskExecutor executor = context.getBean(TaskExecutorBuilder.class).build();
        then(customizer).should().customize(executor);
    });
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) TaskExecutorBuilder(org.springframework.boot.task.TaskExecutorBuilder) TaskExecutorCustomizer(org.springframework.boot.task.TaskExecutorCustomizer) Test(org.junit.jupiter.api.Test)

Aggregations

ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)152 Bean (org.springframework.context.annotation.Bean)72 ExceptionHandlingAsyncTaskExecutor (io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor)19 Test (org.junit.jupiter.api.Test)19 BigInteger (java.math.BigInteger)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 RateLimiter (com.google.common.util.concurrent.RateLimiter)14 Test (org.junit.Test)14 Random (java.util.Random)12 ApplicationContext (org.springframework.context.ApplicationContext)12 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)9 Service (org.fisco.bcos.channel.client.Service)9 Credentials (org.fisco.bcos.web3j.crypto.Credentials)9 Web3j (org.fisco.bcos.web3j.protocol.Web3j)9 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)9 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)9 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)6 MDCCleanerTaskDecorator (com.sequenceiq.cloudbreak.concurrent.MDCCleanerTaskDecorator)4