All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.hedou.abp.common.RemoteCommonService.kt Maven / Gradle / Ivy

Go to download

When the functions of ABP cannot meet service requirements, the Spring Boot framework can be used to expand its own services to make use of abundant Java frameworks on the market.

There is a newer version: 1.0.1
Show newest version
package cloud.hedou.abp.common

import cloud.hedou.abp.remote.HttpClient
import cloud.hedou.abp.remote.HttpClient.Companion.create
import org.slf4j.LoggerFactory
import org.springframework.amqp.rabbit.annotation.Exchange
import org.springframework.amqp.rabbit.annotation.Queue
import org.springframework.amqp.rabbit.annotation.QueueBinding
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Repository
import retrofit2.HttpException

@Repository
class RemoteCommonService(httpClient: HttpClient) {

    private val logger = LoggerFactory.getLogger(RemoteCommonService::class.java)

    private val remoteCommonApi: RemoteCommonApi = httpClient.create()

    @Autowired
    private lateinit var cacheManager: CacheManager

    @Cacheable(value = [CACHE_NAME_COMMON_CATEGORY], unless = "#result.size <= 0")
    fun getCommonData(category: String): List {
        val payload = CommonDataPayload(skipCount = 0, maxResultCount = 100, category = category)
        var response = remoteCommonApi.getCommonData(payload).execute()
        if (!response.isSuccessful) throw HttpException(response)
        val pagedList = response.body()!!
        val mutableList = pagedList.items.toMutableList()
        for (skip in 100..pagedList.totalCount step 100) {
            response = remoteCommonApi.getCommonData(payload.copy(skipCount = skip)).execute()
            if (!response.isSuccessful) throw HttpException(response)
            mutableList.addAll(response.body()!!.items)
        }
        return mutableList
    }

    @Cacheable(value = [CACHE_NAME_COMMON_VALUE], unless = "#result == null")
    fun getCommonDataByValue(category: String, value: String): CommonData? {
        return getCommonData(category)
            .find {
                it.value == value
            }
    }

    @RabbitListener(
        bindings = [
            QueueBinding(
                value = Queue("xh_tenants_common_01"),
                exchange = Exchange("xh_tenants_eventbus"),
                key = ["Hd.Common.Abstractions.Dictionaries.DictionaryData"]
            )
        ]
    )
    fun onCommonDataChanged(value: String) {
        cacheManager.getCache(CACHE_NAME_COMMON_VALUE)?.clear()
        cacheManager.getCache(CACHE_NAME_COMMON_CATEGORY)?.clear()
    }

    companion object {
        private const val CACHE_NAME_COMMON_VALUE = "common-data-by-value"
        private const val CACHE_NAME_COMMON_CATEGORY = "common-data-by-category"
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy