thingsboard 话题讨论区:https://www.iotschool.com/topics/node8
欢迎大家加入 thingsboard 二次开发讨论群:121202538
jdk1.8
注解
protobuf
guava
lambda 表达式
方法引用
方法引用的唯一用途是支持 lambda 表达式的简写,调用方法的时候使用::
, 对于一些单个参数,可以自动推断;
consumer
的作用是给定义一个参数,对其进行 (消费) 处理,处理的方式可以是任意操作,无返回值;
断言接口,根据传入的 lambda 表达式返回 boolean;
根据提供的 lambda 表达式返回需要的对象;
函数式编程接口,根据提供的 lambda 表达式进行相应的操作并返回结果;
@postconstruct
注解并非为 spring 提供, 该注解用来修饰非静态void()
方法,该注解的执行时机为在对象加载完依赖注入后执行,即constructor
> @autowired
> @postconstruct
;
由 spring 提供,spring 为我们提供了事件的监听与实现,内部的实现原理是观察者设计模式,实现了系统解耦,时间发布者不需要考虑谁在监听,发布者只关心自己消息的发布;
applicationreadyevent
应用已经就绪处理请求,将会发布该事件;(.)
spring 提供,对满足条件进行注入;
lombok 插件提供,目的是简化构造者模式的代码。builder pattern 可轻松创建复杂对象;
###protobuf
是一种语言无关、平台无关、可扩展的序列化结构数据的方法,它可用于(数据)通信协议、数据存储等。相比 json,xml 占用内存小,解析速度更快。在使用过程中,首先创建 xx.proto 文件,通过 protobuf-maven-plugin 创建相应的类。
是一组来自谷歌的核心 java 库,其中包括新的集合类型、不可变集合、一个图库,以及用于并发、i/o、散列、缓存、原语、字符串等的实用工具。
listenalbefuture 是对 jdk 的 future 进行增强,可以监听任务的执行状况:
使用 moreexecutors 创建线程池
listeningexecutorservice executorservice = moreexecutors.listeningdecorator(executors.newcachedthreadpool());
提交任务
final listenablefuture<integer> listenablefuture = executorservice.submit(new callable<integer>() {
public integer call() throws exception {
system.out.println("call execute..");
timeunit.seconds.sleep(3);
return 7;
}
});
添加监听任务执行状态①
listenablefuture.addlistener(()->{
try {
system.out.println(listenablefuture.get());
} catch (interruptedexception e) {
e.printstacktrace();
} catch (executionexception e) {
e.printstacktrace();
}
},executorservice);
添加监听任务执行状态②
futures.addcallback(listenablefuture, new futurecallback<integer>() {
@override
public void onsuccess(@nullable integer integer) {
//返回future的执行结果
}
@override
public void onfailure(throwable throwable) {
}
}, executorservice);
如果需要对返回值做处理,可以使用 futures.transform 方法,它是同步方法,另外还有一个异步方法 futures.transformasync:
listenablefuture<string> future = executorservice.submit(() -> "hello, future");
listenablefuture<integer> future2 = futures.transform(future2, string::length, executorservice);
//future2返回的是’hello, future‘的长度
settablefuture
可以认为是一种异步转同步工具,可以它在指定时间内获取listenablefuture
的计算结果:
settablefuture<integer> settablefuture = settablefuture.create();
listenablefuture<integer> future11 = executorservice.submit(() -> {
int sum = 5 6;
settablefuture.set(sum);
return sum;
});
// get设置超时时间
system.out.println(settablefuture.get(2, timeunit.seconds));