This commit is contained in:
tursom 2021-04-12 14:04:58 +08:00
parent 720e7ceddf
commit 184721967c
10 changed files with 1779 additions and 125 deletions

View File

@ -22,6 +22,7 @@ include("ts-web:ts-web-coroutine")
include("ts-database")
include("ts-database:ts-mongodb")
include("ts-database:ts-mongodb:ts-mongodb-spring")
include("ts-database:ts-redis")
//include("web", "aop", "database", "utils", "utils:xml", "utils:async-http", "web:netty-web")
//include("socket", "socket:socket-async")
//include("AsyncSocket")

View File

@ -0,0 +1,41 @@
package cn.tursom.core
import java.util.concurrent.*
import java.util.concurrent.atomic.AtomicInteger
class ScheduledExecutorPool(
private val threadCount: Int = Runtime.getRuntime().availableProcessors() * 2,
private val threadFactory: ThreadFactory = Executors.defaultThreadFactory(),
) {
private val scheduledExecutorQueue = ConcurrentLinkedDeque<Pair<Thread, ScheduledExecutorService>>()
private var initCount = AtomicInteger()
init {
initOne()
}
private fun initOne() {
if (initCount.incrementAndGet() < threadCount) {
val executor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor {
threadFactory.newThread(it)
}
val countDownLatch = CountDownLatch(1)
executor.execute {
scheduledExecutorQueue.addFirst(Thread.currentThread() to executor)
countDownLatch.countDown()
}
countDownLatch.await(3, TimeUnit.SECONDS)
} else {
initCount.decrementAndGet()
}
}
fun get(): Pair<Thread, ScheduledExecutorService> {
if (initCount.get() < threadCount) {
initOne()
}
val pair = scheduledExecutorQueue.poll()
scheduledExecutorQueue.add(pair)
return pair
}
}

View File

@ -4,7 +4,7 @@ plugins {
}
dependencies {
implementation(project(":"))
implementation(project(":ts-core"))
api(group = "org.slf4j", name = "slf4j-api", version = "1.7.29")
api(group = "ch.qos.logback", name = "logback-core", version = "1.2.3")
api(group = "ch.qos.logback", name = "logback-classic", version = "1.2.3")

View File

@ -2,7 +2,7 @@ package cn.tursom.log
import org.slf4j.Logger
interface Slf4j : TrySlf4j {
interface Slf4j : TrySlf4j, Logger {
override val log: Logger
override val logger get() = log
override val sfl4j get() = log

View File

@ -1,13 +1,14 @@
package cn.tursom.log.impl
import cn.tursom.core.getCallerClassName
import cn.tursom.log.Slf4j
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import sun.reflect.Reflection
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmName
open class Slf4jImpl(
open class Slf4jImpl constructor(
@field:Transient
override val log: Logger,
) : Slf4j, Logger by log {
constructor(name: String? = null) : this(LoggerFactory.getLogger(name ?: loggerName))
@ -27,20 +28,8 @@ open class Slf4jImpl(
companion object {
private val thisClassName = listOf(this::class.java.name.dropLast(10), this::class.java.name)
private val loggerName: String
get() = getCallerClassName() ?: throw UnsupportedOperationException()
private fun getCallerClassName(): String? {
var clazz: Class<*>?
var callStackDepth = 1
do {
@Suppress("DEPRECATION")
clazz = Reflection.getCallerClass(callStackDepth++)
if (clazz?.name !in thisClassName) {
break
}
} while (clazz != null)
return clazz?.name
}
get() = getCallerClassName(thisClassName)?.substringBefore('$')
?: throw UnsupportedOperationException()
inline fun getLogger(name: String): Logger = LoggerFactory.getLogger(name)

View File

@ -79,7 +79,7 @@ object MongoUtil {
private fun Map<*, *>.convert(): Document {
val doc = Document()
forEach { any, u ->
forEach { (any, u) ->
any ?: return@forEach
doc[any.toString()] = u.convert() ?: return@forEach
}

View File

@ -0,0 +1,34 @@
plugins {
kotlin("jvm")
`maven-publish`
}
dependencies {
api(project(":"))
implementation(project(":ts-core"))
implementation(project(":ts-core:ts-log"))
api(group = "redis.clients", name = "jedis", version = "3.3.0")
}
@kotlin.Suppress("UNCHECKED_CAST")
(rootProject.ext["excludeTest"] as (Project, TaskContainer) -> Unit)(project, tasks)
tasks.register("install") {
finalizedBy(tasks["publishToMavenLocal"])
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
try {
artifact(tasks["sourcesJar"])
} catch (e: Exception) {
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +0,0 @@
package cn.tursom.utils
interface AsyncIterator<T> {
/**
* Returns the next element in the iteration.
*/
suspend operator fun next(): T
/**
* Returns `true` if the iteration has more elements.
*/
suspend operator fun hasNext(): Boolean
}
suspend inline fun <T> AsyncIterator<T>.forEach(action: (T) -> Unit) {
while (hasNext()) {
val element = next()
action(element)
}
}

View File

@ -1,86 +0,0 @@
package cn.tursom.utils
import cn.tursom.core.cast
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.internal.LinkedTreeMap
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
class GsonDataTypeAdaptor internal constructor(private val gson: Gson) : TypeAdapter<Map<String, Any>?>() {
override fun write(out: JsonWriter, value: Map<String, Any>?) {
if (value == null) {
out.nullValue()
return
}
out.beginObject()
value.forEach { (t, u) ->
out.name(t)
System.err.println(u)
gson.getAdapter(Any::class.java).write(out, u)
}
out.endObject()
}
override fun read(`in`: JsonReader): Map<String, Any> = readInternal(`in`).cast()
private fun readInternal(`in`: JsonReader): Any? {
return when (`in`.peek()) {
JsonToken.BEGIN_ARRAY -> {
val list: MutableList<Any?> = ArrayList()
`in`.beginArray()
while (`in`.hasNext()) {
list.add(readInternal(`in`))
}
`in`.endArray()
list
}
JsonToken.BEGIN_OBJECT -> {
val map: MutableMap<String, Any?> = LinkedTreeMap()
`in`.beginObject()
while (`in`.hasNext()) {
map[`in`.nextName()] = readInternal(`in`)
}
`in`.endObject()
map
}
JsonToken.STRING -> `in`.nextString()
JsonToken.NUMBER -> {
//将其作为一个字符串读取出来
val numberStr: String = `in`.nextString()
//返回的numberStr不会为null
if (numberStr.contains(".") || numberStr.contains("e")
|| numberStr.contains("E")
) {
numberStr.toDouble()
} else numberStr.toLong()
}
JsonToken.BOOLEAN -> `in`.nextBoolean()
JsonToken.NULL -> {
`in`.nextNull()
null
}
else -> throw IllegalStateException()
}
}
companion object {
val FACTORY: TypeAdapterFactory = object :
TypeAdapterFactory {
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
return if (type.rawType == Map::class.java) {
GsonDataTypeAdaptor(gson).cast()
} else null
}
}
val gson = GsonBuilder()
.registerTypeAdapterFactory(FACTORY)
.create()
}
}