Springboot项目,多线程测试使用Redis生成订单id
2018-12-19 14:57
399 查看
版权声明:原创文档,转载请注明哦~ https://blog.csdn.net/weixin_42335535/article/details/85097266
Springboot项目,多线程测试使用Redis生成订单id
springboot测试类:
import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; /** * @date 2018/12/5 * @since 1.0.0 */ @RunWith(SpringRunner.class) @SpringBootTest //@WebAppConfiguration public class BaseApplicationTests { @Before public void init() { System.out.println("begin the test ..........."); } @After public void after() { System.out.println("end the test ..........."); } }
Redis并使用多线程测试
springboot项目中的redis的配置这里就不在贴上去了。
看测试代码吧:
import org.apache.commons.lang3.time.DateFormatUtils; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import java.util.Calendar; import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * redis生成订单号 * * @date 2018/12/5 * @since 1.0.0 */ public class RedisTest extends BaseApplicationTests { private static String REDIS_MANAGE_CONTRACT_PREFIX = "yuy:manage:TEST_CONTRACT_INC_"; @Autowired private StringRedisTemplate redisTemplate; @Autowired private Executor executor; public String generateContractNo() { //合同编号规则: BNF + 当前日期 + 四位编号,编号每天从1开始递增 String currentDate = DateFormatUtils.format(new Date(), "yyyyMMdd"); String key = REDIS_MANAGE_CONTRACT_PREFIX + currentDate; long count = redisTemplate.opsForValue().increment(key, 1); if (count == 1) { redisTemplate.expireAt(key, getTimeout()); } return String.format("BNF%s-%s", currentDate, String.format("%04d", count)); } private Date getTimeout() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); return calendar.getTime(); } @Test public void test1() { Runnable task = () -> { try { System.out.println(Thread.currentThread().getName()+" 订单号:"+generateContractNo()); Thread.sleep(500); } catch (Exception e) { System.out.println("task interrupted: " + e); e.printStackTrace(); } }; ExecutorService executorService = Executors.newFixedThreadPool(5); try { for (int i = 0; i < 10; i++) { executorService.execute(task); } Thread.sleep(1000);// 当前主线程休眠 } catch (Exception e) { e.printStackTrace(); } finally { executorService.shutdown(); } /*for (int i = 0; i < 10; i++) { executorService.execute(task); } try { executorService.shutdown(); if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) { executorService.shutdownNow(); } } catch (InterruptedException e) { System.out.println("awaitTermination interrupted: " + e); executorService.shutdownNow(); }*/ } }
pu上结果:
代码写在这里,如果有看官看到并发现错误,欢迎指正,谢谢!
第一次写博客,请各位看官们见谅~~
相关文章推荐
- 使用http://start.spring.io/生成springboot项目无法引入springboot的jar包
- 使用SpringBoot搭建小型项目,集成mybatis,redis,swagger2,并部署在外部容器中。
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
- spring boot项目使用swagger-codegen生成服务间调用的jar包
- SpringBoot项目使用Redis
- 使用spring boot +Junit 测试 spring mvc 配置中心项目
- spring-boot项目在线生成工程使用(start.spring.io)
- 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存
- springboot项目中使用MockMvc 进行测试
- springboot项目使用redis数据库作缓存
- 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
- 2 Springboot中使用redis,配置redis的key value生成策略
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
- springboot项目下使用mybatis generator生成pojo类、mapper文件以及dao类
- Spring Boot 实践2 --项目中使用 Redis
- 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
- 使用Redis作为SpringBoot项目数据缓存
- Spring Boot中使用freeMarker生成word文档
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中