GH-8642: Revise executors in the project (#8647)
* GH-8642: Revise executors in the project Fixes https://github.com/spring-projects/spring-integration/issues/8642 * Rework some `Executors.newSingleThreadExecutor()` to `ExecutorServiceAdapter(new SimpleAsyncTaskExecutor())` * Expose `TaskExecutor` setters; deprecate `ExecutorService`-based * Some other code clean up in the effected classes: `LogAccessor`, no `synchronized` in critical blocks * Give a meaningful prefix for default threads in the context of components, e.g. `SubscribableRedisChannel` - `getBeanName() + "-"` * * Fix `PostgresChannelMessageTableSubscriberTests` for `PostgresSubscribableChannel` initialization to let it create its internal `Executor` * Use an `AsyncTaskExecutor` injection instead of `ExecutorServiceAdapter` wrapping * Fix `LockRegistryLeaderInitiatorTests` for `taskExecutor` injection * Bring back `LockRegistryLeaderInitiator.setExecutorService()` as an accident after property auto-renaming
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,27 +17,24 @@
|
||||
package org.springframework.integration.support.leader;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
import org.springframework.core.task.support.ExecutorServiceAdapter;
|
||||
import org.springframework.core.task.support.TaskExecutorAdapter;
|
||||
import org.springframework.integration.leader.Context;
|
||||
import org.springframework.integration.leader.DefaultCandidate;
|
||||
import org.springframework.integration.leader.event.DefaultLeaderEventPublisher;
|
||||
import org.springframework.integration.leader.event.LeaderEventPublisher;
|
||||
import org.springframework.integration.support.locks.DefaultLockRegistry;
|
||||
import org.springframework.integration.support.locks.LockRegistry;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -69,7 +66,7 @@ public class LockRegistryLeaderInitiatorTests {
|
||||
private final LockRegistryLeaderInitiator initiator =
|
||||
new LockRegistryLeaderInitiator(this.registry, new DefaultCandidate());
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
this.initiator.setLeaderEventPublisher(new CountingPublisher(this.granted, this.revoked));
|
||||
}
|
||||
@@ -255,9 +252,8 @@ public class LockRegistryLeaderInitiatorTests {
|
||||
.given(lock)
|
||||
.tryLock(anyLong(), eq(TimeUnit.MILLISECONDS));
|
||||
|
||||
new DirectFieldAccessor(another).setPropertyValue("executorService",
|
||||
new ExecutorServiceAdapter(
|
||||
new SyncTaskExecutor()));
|
||||
new DirectFieldAccessor(another).setPropertyValue("taskExecutor",
|
||||
new TaskExecutorAdapter(new SyncTaskExecutor()));
|
||||
|
||||
another.start();
|
||||
|
||||
@@ -297,29 +293,6 @@ public class LockRegistryLeaderInitiatorTests {
|
||||
another.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldShutdownInternalExecutorService() {
|
||||
this.initiator.start();
|
||||
this.initiator.destroy();
|
||||
|
||||
ExecutorService executorService =
|
||||
TestUtils.getPropertyValue(this.initiator, "executorService", ExecutorService.class);
|
||||
|
||||
assertThat(executorService.isShutdown()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doNotShutdownProvidedExecutorService() {
|
||||
LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator(this.registry);
|
||||
ExecutorService executorService = Executors.newSingleThreadExecutor();
|
||||
another.setExecutorService(executorService);
|
||||
|
||||
another.start();
|
||||
another.destroy();
|
||||
|
||||
assertThat(executorService.isShutdown()).isFalse();
|
||||
}
|
||||
|
||||
private static class CountingPublisher implements LeaderEventPublisher {
|
||||
|
||||
private final CountDownLatch granted;
|
||||
|
||||
Reference in New Issue
Block a user