时间统计/计时
StopWatch 任务步骤计时
出处:org.springframework.util.StopWatch
作用:统计、汇总每个步骤的用时;单机计时
官方介绍:
简单的秒表,允许为许多任务计时,公开总运行时间和每个指定任务的运行时间。 隐藏 System.currentTimeMillis() 的使用,提高了应用程序代码的可读性,减少了计算错误的可能性。 请注意,此对象不是设计为线程安全的,也不使用同步。 此类通常用于在概念验证和开发过程中验证性能,而不是作为生产应用程序的一部分。
import org.apache.commons.lang3.RandomUtils;import org.springframework.util.StopWatch;import java.util.concurrent.TimeUnit;public class StopWatchTest {public static void main(String[] args) throws InterruptedException {final StopWatch sw = new StopWatch();sw.start("A");randomSleep();sw.stop();sw.start("B");randomSleep();sw.stop();sw.start("C");randomSleep();sw.stop();System.out.println(sw.prettyPrint());}private static void randomSleep() throws InterruptedException {TimeUnit.SECONDS.sleep(RandomUtils.nextInt(0, 5));}}
输出结果如下,可以看到每一个步骤的耗时、占用百分比、运行总时间
StopWatch '': running time (millis) = 2015-----------------------------------------ms % Task name-----------------------------------------00014 001% A01000 050% B01001 050% C
List
list 分区 partition
出处:com.google.common.collect.Lists#partition
作用:将 list 按数量分区成多个 list
import com.google.common.collect.Lists;import java.util.List;import java.util.stream.Collectors;import java.util.stream.IntStream;public class Demo {public static void main(String[] args) {final List<Integer> ids = IntStream.range(1, 11).mapToObj(Integer::new).collect(Collectors.toList());final List<List<Integer>> partition = Lists.partition(ids, 3);System.out.println(partition);// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]}}
重试机制
spring-retry
简单来说,就如同下面这样来配置一个重试调用,spirng 下面这种方式依赖 aop。由于版本问题,最新使用方式,请查看文档
@Retryable(value = {RemoteServerException.class},maxAttempts = 3, // 重试次数(如果异常,总共调用三次该方法,包括异常的那一次)backoff = @Backoff(delay = 1000 * 30, // 重试间隔 30 秒maxDelay = 1000 * 50 // 最大重试间隔 50 秒))public void test() {log.info("重试中");throw new RemoteServerException("异常");}
字符串操作
StringUtils 截取等各种处理
出处:org.apache.commons.lang3.StringUtils
作用:字符串常用操作
// 拿匹配结构的后缀,返回结果 smsStringUtils.substringAfter("/code/sms", "/code/");--------------------------------------------------------------获取匹配结果的前缀// ImageCodeProcessor 类名// 结果:Image// 使用场景:只用持有者管理所有实现子类的时候,可以拿到前缀,然后根据前缀拿到相关的枚举信息StringUtils.substringBefore(getClass().getSimpleName(), "CodeProcessor");--------------------------------------------------------------提供类似与正则表达式的字符串截取// callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );// 返回 YOUR_OPENIDStringUtils.substringBetween(str,"\"openid\":", "\"}");---------------------------------------------------------------替代传统多次按等号分割,但是又只想获取值的情况// access_token=FE04*******CCE2&expires_in=7776000&refresh_token=88E4*********BE14String[] items = StringUtils.splitByWholeSeparatorPreserveAllTokens(responseStr, "&");String accessToken = StringUtils.substringAfterLast(items[0], "=");--------------------------------------------------------------
字符串分隔
出处:com.google.common.base.Splitter
作用:按指定字符分隔
场景:大量使用某一种分隔符时,会提高性能
// com.google.common.base.SplitterSplitter splitter = Splitter.on("#");List<String> strings = splitter.splitToList("12345#789");// [12345, 789]System.out.println(strings);
JSON 操作
- JSONPath 按路径操作 JSON
方法参数校验
快捷的工具类、类似断言检查
出处:com.google.common.base.Preconditions
作用:帮助方法或构造函数检查它是否被正确调用(是否满足其前提条件)的静态工具类
// 如果 topic 为 null 就抛出空指针异常Preconditions.checkNotNull(message.getTopic());
