forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加多租户与上下文的集成
- Loading branch information
Showing
8 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...tis-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/config/AsyncConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.config; | ||
|
||
import com.alibaba.ttl.TtlRunnable; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfig { | ||
|
||
@Bean | ||
public BeanPostProcessor executorBeanPostProcessor() { | ||
return new BeanPostProcessor() { | ||
|
||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
if (!(bean instanceof ThreadPoolTaskExecutor)) { | ||
return bean; | ||
} | ||
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean; | ||
executor.setTaskDecorator(TtlRunnable::get); | ||
return executor; | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...s-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/context/TenantHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.context; | ||
|
||
import com.alibaba.ttl.TransmittableThreadLocal; | ||
|
||
public class TenantHolder { | ||
|
||
private static final ThreadLocal<Integer> TENANT_ID = new TransmittableThreadLocal<>(); | ||
// private static final ThreadLocal<Integer> TENANT_ID = new ThreadLocal<>(); | ||
// private static final ThreadLocal<Integer> TENANT_ID = new InheritableThreadLocal<>(); | ||
|
||
public static void setTenantId(Integer tenantId) { | ||
TENANT_ID.set(tenantId); | ||
} | ||
|
||
public static Integer getTenantId() { | ||
return TENANT_ID.get(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ant/src/main/java/cn/iocoder/springboot/lab12/mybatis/core/TtlThreadPoolTaskExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.core; | ||
|
||
import com.alibaba.ttl.TtlCallable; | ||
import com.alibaba.ttl.TtlRunnable; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
import org.springframework.util.concurrent.ListenableFuture; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Future; | ||
|
||
@Deprecated | ||
public class TtlThreadPoolTaskExecutor extends ThreadPoolTaskExecutor { | ||
|
||
@Override | ||
public void execute(Runnable task) { | ||
super.execute(Objects.requireNonNull(TtlRunnable.get(task))); | ||
} | ||
|
||
@Override | ||
public void execute(Runnable task, long startTimeout) { | ||
super.execute(Objects.requireNonNull(TtlRunnable.get(task)), startTimeout); | ||
} | ||
|
||
@Override | ||
public Future<?> submit(Runnable task) { | ||
return super.submit(Objects.requireNonNull(TtlRunnable.get(task))); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Callable<T> task) { | ||
return super.submit(Objects.requireNonNull(TtlCallable.get(task))); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<?> submitListenable(Runnable task) { | ||
return super.submitListenable(Objects.requireNonNull(TtlRunnable.get(task))); | ||
} | ||
|
||
@Override | ||
public <T> ListenableFuture<T> submitListenable(Callable<T> task) { | ||
return super.submitListenable(Objects.requireNonNull(TtlCallable.get(task))); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...is-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.service; | ||
|
||
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO; | ||
import cn.iocoder.springboot.lab12.mybatis.mapper.UserMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.AsyncResult; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.concurrent.Future; | ||
|
||
@Service | ||
public class UserService { | ||
|
||
private final Logger log = LoggerFactory.getLogger(UserService.class); | ||
|
||
@Resource | ||
private UserMapper userMapper; | ||
|
||
@Async | ||
public Future<UserDO> getUserAsync(Integer id) { | ||
UserDO userDO = userMapper.selectById(id); | ||
log.info("[getUserAsync][id({}) user({})]", id, userDO); | ||
return AsyncResult.forValue(userDO); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/util/TtlExecutorsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.util; | ||
|
||
import com.alibaba.ttl.spi.TtlEnhanced; | ||
import com.alibaba.ttl.threadpool.agent.TtlAgent; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* {@link com.alibaba.ttl.threadpool.TtlExecutors} 工具类 | ||
*/ | ||
@Deprecated | ||
public class TtlExecutorsUtil { | ||
|
||
public static Executor getTtlThreadPoolTaskExecutor(@Nullable Executor executor) { | ||
if (TtlAgent.isTtlAgentLoaded() || null == executor || executor instanceof TtlEnhanced) { | ||
return executor; | ||
} | ||
// return new ExecutorTtlWrapper(executor, true); | ||
return null; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...lus-tenant/src/test/java/cn/iocoder/springboot/lab12/mybatis/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.service; | ||
|
||
import cn.iocoder.springboot.lab12.mybatis.Application; | ||
import cn.iocoder.springboot.lab12.mybatis.context.TenantHolder; | ||
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
public class UserServiceTest { | ||
|
||
@Resource | ||
private UserService userService; | ||
|
||
@Test | ||
public void testGetUserAsync() throws ExecutionException, InterruptedException { | ||
TenantHolder.setTenantId(10); // TODO 芋艿:写死 | ||
Future<UserDO> future = userService.getUserAsync(9); | ||
future.get(); | ||
} | ||
|
||
} |