Skip to content

Instantly share code, notes, and snippets.

View Jian-Min-Huang's full-sized avatar
🔥
Go Go Go !

黃健旻 Vincent Huang Jian-Min-Huang

🔥
Go Go Go !
View GitHub Profile
fun flux() =
Flux.mergeSequential(getMonoResponse(100), getMonoResponse(200))
.collectList()
.flatMap { v ->
return@flatMap getMonoResponse(300)
.map { v2 ->
v.add(v2)
return@map v
}
}.flatMap { v ->
fun async() =
arrayOf(getFutureResponse(100), getFutureResponse(200))
.let {
allOf(*it)
.thenApply { v ->
return@thenApply it.map { v1 -> v1.get() }.toMutableList()
}.thenApply { v ->
val delay300res = getFutureResponse(v[0]["totalTimeMillis"].parseLong() + v[1]["totalTimeMillis"].parseLong())
delay300res.join()
v.add(delay300res.get())
suspend fun coroutine() =
coroutineScope {
// concurrent call action 1 and 2, cost 200ms
val delay100res = async(Dispatchers.IO) { getAwaitResponse(100) }
val delay200res = async(Dispatchers.IO) { getAwaitResponse(200) }
// blocking call, need return value from action 1 and 2, cost 300ms
val period1 = delay100res.await()["totalTimeMillis"]!! + delay200res.await()["totalTimeMillis"]!!
val delay300res = async(Dispatchers.IO) { getAwaitResponse(period1) }
fun blockingMvc() =
run {
// action 1, cost 100ms
val delay100res = getBlockingResponse(100)!!["totalTimeMillis"].parseLong()
// action 2, cost 200ms
val delay200res = getBlockingResponse(200)!!["totalTimeMillis"].parseLong()
// action 3, need return value from action 1 and 2, cost 300ms
val delay300req = getBlockingResponse(delay100res + delay200res)!!["totalTimeMillis"].parseLong()
String aesDecrypt(String cipherText, String key, String iv) {
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"),
new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
return new String(cipher.doFinal(hexString2ByteArray(cipherText)));
}
fun String.aesDecrypt(transformation: String = "AES/CBC/PKCS7PADDING", key: String, iv: String) =
also {
Security.addProvider(BouncyCastleProvider())
}.let {
Cipher.getInstance(transformation)
}.also {
it.init(
Cipher.DECRYPT_MODE,
SecretKeySpec(key.toByteArray(StandardCharsets.UTF_8), "AES"),
IvParameterSpec(iv.toByteArray(StandardCharsets.UTF_8))
fun String.md5() =
MessageDigest.getInstance("MD5")
.also {
it.update(this.toByteArray(StandardCharsets.UTF_8))
}.run {
this.digest().toHexString()
}
println("abc".md5())
@Data
@AllArgsConstructor
public class Response {
private String code;
private Map<String, Object> data;
private Long timestamp;
private String checksum;
}
data class Response(
var code: String,
var data: Map<String, Any>,
var timestamp: Long,
var checksum: String
)
fun main() {
val response = Response("T001", mapOf("a" to 100), Date().time, "317d069fe45debda190c3f5822f16484")
public class Response {
private String code;
private Map<String, Object> data;
private Long timestamp;
private String checksum;
public Response(String code, Map<String, Object> data, Long timestamp, String checksum) {
this.code = code;
this.data = data;