Skip to content

Commit

Permalink
增加 TTL
Browse files Browse the repository at this point in the history
增加多租户与上下文的集成
  • Loading branch information
YunaiV committed Dec 4, 2021
1 parent b96e0a1 commit 9023aa0
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lab-12-mybatis/lab-12-mybatis-plus-tenant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.12.2</version>
</dependency>

</dependencies>

</project>
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;
}

};
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.iocoder.springboot.lab12.mybatis.config;

import cn.iocoder.springboot.lab12.mybatis.context.TenantHolder;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
Expand All @@ -21,7 +22,8 @@ public MybatisPlusInterceptor mybatisPlusInterceptor() {

@Override
public Expression getTenantId() {
return new LongValue(10);
Integer tenantId = TenantHolder.getTenantId();
return new LongValue(tenantId);
}

// 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
Expand Down
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();
}

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

}
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;
}

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

}

0 comments on commit 9023aa0

Please sign in to comment.