Commit c31fce4bf53dfa0de0227d68eafdb16032e1b18f
1 parent
45004dea
feat(*): 调整格式化代码
- 调整格式化代码
Showing
1 changed file
with
18 additions
and
40 deletions
fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/member/MemberRpcService.kt
... | ... | @@ -5,11 +5,8 @@ import cn.fw.hestia.common.constant.Constant |
5 | 5 | import cn.fw.hestia.rpc.member.dto.MemberUserDTO |
6 | 6 | import cn.fw.member.sdk.api.FunctionApi |
7 | 7 | import cn.fw.member.sdk.api.MemberApi |
8 | -import kotlinx.coroutines.Dispatchers | |
9 | -import kotlinx.coroutines.async | |
10 | -import kotlinx.coroutines.runBlocking | |
8 | +import kotlinx.coroutines.* | |
11 | 9 | import kotlinx.coroutines.selects.select |
12 | -import kotlinx.coroutines.supervisorScope | |
13 | 10 | import org.slf4j.LoggerFactory |
14 | 11 | import org.springframework.cache.annotation.Cacheable |
15 | 12 | import org.springframework.stereotype.Service |
... | ... | @@ -74,49 +71,30 @@ class MemberRpcService |
74 | 71 | return "" |
75 | 72 | } |
76 | 73 | return runBlocking(Dispatchers.IO) { |
74 | + val errHandle = CoroutineExceptionHandler { _, ex -> | |
75 | + logger.error("查询号码归属地失败:", ex) | |
76 | + } | |
77 | + val job = SupervisorJob() | |
77 | 78 | supervisorScope { |
78 | - val j1 = async { | |
79 | - try { | |
79 | + var att = "" | |
80 | + launch(job + errHandle) { | |
81 | + val j1 = async(job) { | |
80 | 82 | val result = MobileUtil.attribution(mobile) |
81 | 83 | result?.run { |
82 | 84 | "$province $city" |
83 | - } | |
84 | - } catch (ex: Exception) { | |
85 | - logger.error("查询号码归属地失败:", ex) | |
86 | - "" | |
87 | - } | |
88 | - } | |
89 | - val j2 = async { | |
90 | - try { | |
91 | - val msg = functionApi.queryMobileLocation(mobile) | |
92 | - if (msg.isSuccess) { | |
93 | - logger.info("调用会员系统查询手机号归属地: {}", msg.data) | |
94 | - msg.data?.run { | |
95 | - "$province $city" | |
96 | - } | |
97 | - } else "" | |
98 | - } catch (ex: Exception) { | |
99 | - logger.error("调用会员系统查询手机号归属地失败:", ex) | |
100 | - "" | |
85 | + } ?: "" | |
101 | 86 | } |
102 | - } | |
103 | - select { | |
104 | - j1.onAwait { | |
105 | - println(it) | |
106 | - if (!it.isNullOrEmpty()) { | |
107 | - j2.cancel() | |
108 | - } | |
109 | - it | |
87 | + val j2 = async(job) { | |
88 | + val msg = functionApi.queryMobileLocation("18086834114") | |
89 | + msg.takeIf { it.isSuccess }?.run { data?.run { "$province $city" } } ?: "" | |
110 | 90 | } |
111 | - j2.onAwait { | |
112 | - println(it) | |
113 | - if (!it.isNullOrEmpty()) { | |
114 | - j1.cancel() | |
115 | - } | |
116 | - it | |
91 | + att = select { | |
92 | + j1.onAwait { it } | |
93 | + j2.onAwait { it } | |
117 | 94 | } |
118 | - } | |
119 | - } ?: "" | |
95 | + }.join() | |
96 | + att | |
97 | + } | |
120 | 98 | } |
121 | 99 | } |
122 | 100 | } |
123 | 101 | \ No newline at end of file | ... | ... |