4 changed files with 0 additions and 238 deletions
-
37src/test/java/com/gaotao/DynamicDataSourceTest.java
-
31src/test/java/com/gaotao/JwtTest.java
-
119src/test/java/com/gaotao/RedisTest.java
-
51src/test/java/com/gaotao/service/DynamicDataSourceTestService.java
@ -1,37 +0,0 @@ |
|||||
|
|
||||
|
|
||||
package com.gaotao; |
|
||||
|
|
||||
import com.gaotao.service.DynamicDataSourceTestService; |
|
||||
import org.junit.jupiter.api.Test; |
|
||||
import org.junit.jupiter.api.extension.ExtendWith; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.boot.test.context.SpringBootTest; |
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
||||
|
|
||||
/** |
|
||||
* 多数据源测试 |
|
||||
* |
|
||||
* |
|
||||
*/ |
|
||||
@ExtendWith(SpringExtension.class) |
|
||||
@SpringBootTest |
|
||||
public class DynamicDataSourceTest { |
|
||||
@Autowired |
|
||||
private DynamicDataSourceTestService dynamicDataSourceTestService; |
|
||||
|
|
||||
@Test |
|
||||
public void test(){ |
|
||||
Long id = 1L; |
|
||||
id.toString(); |
|
||||
dynamicDataSourceTestService.updateUser(id); |
|
||||
//dynamicDataSourceTestService.updateUserBySlave1(id); |
|
||||
//dynamicDataSourceTestService.updateUserBySlave2(id); |
|
||||
} |
|
||||
|
|
||||
@Test |
|
||||
public void test2(){ |
|
||||
|
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,31 +0,0 @@ |
|||||
package com.gaotao; |
|
||||
|
|
||||
import com.gaotao.modules.app.utils.JwtUtils; |
|
||||
import org.junit.jupiter.api.Test; |
|
||||
import org.junit.jupiter.api.extension.ExtendWith; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.boot.test.context.SpringBootTest; |
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
||||
import org.springframework.test.context.junit4.SpringRunner; |
|
||||
|
|
||||
|
|
||||
@ExtendWith(SpringExtension.class) |
|
||||
@SpringBootTest |
|
||||
public class JwtTest { |
|
||||
@Autowired |
|
||||
private JwtUtils jwtUtils; |
|
||||
|
|
||||
@Test |
|
||||
public void test() { |
|
||||
String token = jwtUtils.generateToken(1); |
|
||||
|
|
||||
System.out.println(token); |
|
||||
|
|
||||
|
|
||||
} |
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
} |
|
||||
@ -1,119 +0,0 @@ |
|||||
package com.gaotao; |
|
||||
|
|
||||
import com.alibaba.fastjson2.JSONObject; |
|
||||
import com.gaotao.common.utils.RedisUtils; |
|
||||
import org.junit.jupiter.api.Test; |
|
||||
import org.junit.jupiter.api.extension.ExtendWith; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.boot.test.context.SpringBootTest; |
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
|
||||
import org.springframework.test.context.junit4.SpringRunner; |
|
||||
|
|
||||
import java.util.concurrent.*; |
|
||||
|
|
||||
@ExtendWith(SpringExtension.class) |
|
||||
@SpringBootTest |
|
||||
public class RedisTest { |
|
||||
@Autowired |
|
||||
private RedisUtils redisUtils; |
|
||||
|
|
||||
@Test |
|
||||
public void thread() { |
|
||||
|
|
||||
StringBuffer s1 = new StringBuffer(); |
|
||||
StringBuffer s2 = new StringBuffer(); |
|
||||
|
|
||||
JSONObject jsonObject = new JSONObject(); |
|
||||
jsonObject.put("大头", new JSONObject().put("fdsafdas", "范德萨发大水")); |
|
||||
System.out.println(jsonObject); |
|
||||
|
|
||||
|
|
||||
/** |
|
||||
*(1)corePoolSize:核心线程数,线程池中始终存活的线程数。 |
|
||||
* (2)maximumPoolSize: 最大线程数,线程池中允许的最大线程数。 |
|
||||
* (3)keepAliveTime: 存活时间,线程没有任务执行时最多保持多久时间会终止。 |
|
||||
* (4)unit: 单位,参数keepAliveTime的时间单位,7种可选。 |
|
||||
* - TimeUnit.DAYS 天 |
|
||||
* - TimeUnit.HOURS 小时 |
|
||||
* - TimeUnit.MINUTES 分 |
|
||||
* - TimeUnit.SECONDS 秒 |
|
||||
* - TimeUnit.MILLISECONDS 毫秒 |
|
||||
* - TimeUnit.MICROSECONDS 微妙 |
|
||||
* - TimeUnit.NANOSECONDS 纳秒 |
|
||||
* (5)workQueue: 一个阻塞队列,用来存储等待执行的任务,均为线程安全,7种可选。 |
|
||||
* - ArrayBlockingQueue 一个由数组结构组成的有界阻塞队列。 |
|
||||
* - LinkedBlockingQueue 一个由链表结构组成的有界阻塞队列。 |
|
||||
* - SynchronousQueue 一个不存储元素的阻塞队列,即直接提交给线程不保持它们。 |
|
||||
* - PriorityBlockingQueue 一个支持优先级排序的无界阻塞队列。 |
|
||||
* - DelayQueue 一个使用优先级队列实现的无界阻塞队列,只有在延迟期满时才能从中提取元素。 |
|
||||
* - LinkedTransferQueue 一个由链表结构组成的无界阻塞队列。与SynchronousQueue类似,还含有非阻塞方法。 |
|
||||
* - LinkedBlockingDeque 一个由链表结构组成的双向阻塞队列。 |
|
||||
* 较常用的是LinkedBlockingQueue和Synchronous。线程池的排队策略与BlockingQueue有关。 |
|
||||
* (6)threadFactory: 线程工厂,主要用来创建线程,默及正常优先级、非守护线程。 |
|
||||
* (7)handler:拒绝策略,拒绝处理任务时的策略,4种可选,默认为AbortPolicy。 |
|
||||
* - AbortPolicy 拒绝并抛出异常。 |
|
||||
* - CallerRunsPolicy 重试提交当前的任务,即再次调用运行该任务的execute()方法。 |
|
||||
* - DiscardOldestPolicy 抛弃队列头部(最旧)的一个任务,并执行当前任务。 |
|
||||
* - DiscardPolicy 抛弃当前任务。 |
|
||||
*/ |
|
||||
ExecutorService executorService = new ThreadPoolExecutor(2, 10, |
|
||||
1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(5, true), |
|
||||
Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); |
|
||||
executorService.execute(() -> { |
|
||||
// 获取线程名称,默认格式:pool-1-thread-1 |
|
||||
synchronized (s1) { |
|
||||
s1.append("a"); |
|
||||
s2.append("1"); |
|
||||
try { |
|
||||
Thread.sleep(100); |
|
||||
} catch (InterruptedException e) { |
|
||||
e.printStackTrace(); |
|
||||
} |
|
||||
|
|
||||
synchronized (s2) { |
|
||||
System.out.println("s2执行中"); |
|
||||
s1.append("b"); |
|
||||
s2.append("2"); |
|
||||
|
|
||||
System.out.println(s1); |
|
||||
System.out.println(s2); |
|
||||
} |
|
||||
} |
|
||||
}); |
|
||||
executorService.execute(() -> { |
|
||||
// 获取线程名称,默认格式:pool-1-thread-1 |
|
||||
synchronized (s2) { |
|
||||
|
|
||||
s1.append("c"); |
|
||||
s2.append("3"); |
|
||||
|
|
||||
try { |
|
||||
Thread.sleep(100); |
|
||||
} catch (InterruptedException e) { |
|
||||
e.printStackTrace(); |
|
||||
|
|
||||
} |
|
||||
|
|
||||
synchronized (s1) { |
|
||||
s1.append("d"); |
|
||||
s2.append("4"); |
|
||||
|
|
||||
System.out.println(s1); |
|
||||
System.out.println(s2); |
|
||||
} |
|
||||
} |
|
||||
}); |
|
||||
|
|
||||
} |
|
||||
|
|
||||
|
|
||||
@Test |
|
||||
public void t6() { |
|
||||
String s = "sss\\999"; |
|
||||
String s1 = s.replaceAll("\\\\", "\\\\\\\\"); |
|
||||
System.out.println(s1); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
|
|
||||
|
|
||||
@ -1,51 +0,0 @@ |
|||||
|
|
||||
|
|
||||
package com.gaotao.service; |
|
||||
|
|
||||
import com.gaotao.datasource.annotation.DataSource; |
|
||||
import com.gaotao.modules.sys.dao.SysUserDao; |
|
||||
import com.gaotao.modules.sys.entity.SysUserEntity; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
import org.springframework.transaction.annotation.Transactional; |
|
||||
|
|
||||
/** |
|
||||
* 测试多数据源 |
|
||||
* |
|
||||
* |
|
||||
*/ |
|
||||
@Service |
|
||||
//@DataSource("slave1") |
|
||||
public class DynamicDataSourceTestService { |
|
||||
@Autowired |
|
||||
private SysUserDao sysUserDao; |
|
||||
|
|
||||
@Transactional |
|
||||
public void updateUser(Long id){ |
|
||||
SysUserEntity user = new SysUserEntity(); |
|
||||
user.setUserId(id); |
|
||||
user.setMobile("13500000000"); |
|
||||
sysUserDao.updateById(user); |
|
||||
} |
|
||||
|
|
||||
@Transactional |
|
||||
@DataSource("slave1") |
|
||||
public void updateUserBySlave1(Long id){ |
|
||||
SysUserEntity user = new SysUserEntity(); |
|
||||
user.setUserId(id); |
|
||||
user.setMobile("13500000001"); |
|
||||
sysUserDao.updateById(user); |
|
||||
} |
|
||||
|
|
||||
@DataSource("slave2") |
|
||||
@Transactional |
|
||||
public void updateUserBySlave2(Long id){ |
|
||||
SysUserEntity user = new SysUserEntity(); |
|
||||
user.setUserId(id); |
|
||||
user.setMobile("13500000002"); |
|
||||
sysUserDao.updateById(user); |
|
||||
|
|
||||
//测试事物 |
|
||||
int i = 1/0; |
|
||||
} |
|
||||
} |
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue