检查入参不为空
Preconditions.checkNotNull(args,"入参不能为空");
检查List为空
if (CollectionUtils.isEmpty(list))
创建list
List strs = Lists.newArrayList("小明","小强","小红");
创建blockingQueue(guava)
private static final int CLEAN_QUEUE_MAX_SIZE = 100;
private BlockingQueue toClean = Queues.newLinkedBlockingQueue(CLEAN_QUEUE_MAX_SIZE);
Strings判断是否为空 (guava)
Strings.isNullOrEmpty(value)
定时任务线程池
ScheduledExecutorService
executorService =
Executors.newScheduledThreadPool(1, ApolloThreadFactory.create("ConfigRefresher", true));
获取spring的全部配置
@Autowired
private ConfigurableEnvironment environment;
跟据入参复制对应的属性
public static void copyEntityProperties(Object source, Object target) {
org.springframework.beans.BeanUtils.copyProperties(source, target, COPY_IGNORED_PROPERTIES);
}
private static final String[] COPY_IGNORED_PROPERTIES = {"id", "dataChangeCreatedBy", "dataChangeCreatedTime", "dataChangeLastModifiedTime"};
MoreObjects.toStringHelper
public static void main(String[] args) {
Student st = new Student("小明",2);
String s = MoreObjects.toStringHelper(st)
.add("name", st.getName())
.add("age", st.getAge())
.toString();
System.out.println(s);
}
输出:
Student{name=小明, age=2}
@PostConstruct
//被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
@PostConstruct
private void initialize() {
cleanExecutorService.submit(() -> {
while (!cleanStopped.get() && !Thread.currentThread().isInterrupted()) {
try {
Long rm = toClean.poll(1, TimeUnit.SECONDS);
if (rm != null) {
cleanMessage(rm);
} else {
TimeUnit.SECONDS.sleep(5);
}
} catch (Throwable ex) {
Tracer.logError(ex);
}
}
});
}
spring提供的rest操作工具类 RestTemplate
ResponseEntity
原子对象引用
private AtomicReference> m_configServices;
限流算法
RateLimiter m_warnLogRateLimiter = m_warnLogRateLimiter = RateLimiter.create(0.017);
Google依赖注入框架
Injector m_injector = m_injector = Guice.createInjector(new ApolloModule())
UrlEscapers 处理url特殊字符
public static void main(String[] args) {
String str="http://www.baidu.com/api/?action=s2s_install&a={appsunion}&b=3&c=300294cf2fb33ab47691cde6fed03836";
str = UrlEscapers.urlFragmentEscaper().escape(str);
System.out.println("*********" + str);
}