Skip to content

Commit

Permalink
🔥 feat: 添加mq
Browse files Browse the repository at this point in the history
  • Loading branch information
cokie committed Jan 22, 2024
1 parent 96a4c48 commit 4346745
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 49 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ subprojects {
dependencies {
implementation(platform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
implementation("io.quarkus:quarkus-smallrye-openapi")
implementation("io.quarkus:quarkus-smallrye-reactive-messaging-rabbitmq")
implementation("io.quarkus:quarkus-resteasy-reactive-jaxb")
implementation("io.quarkus:quarkus-rest-client-reactive-kotlin-serialization")
implementation("io.quarkus:quarkus-resteasy-reactive")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.dict;
package io.micro.api.http.dict;

import io.micro.api.dict.converter.SystemDictConverter;
import io.micro.api.dict.dto.QuerySystemDictDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.function;
package io.micro.api.http.function;

import io.micro.api.function.converter.FunctionConverter;
import io.micro.api.function.dto.QueryFunctionDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.robot;
package io.micro.api.http.robot;

import io.micro.core.annotation.InitAuthContext;
import io.micro.server.robot.domain.service.RobotManagerService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.robot;
package io.micro.api.http.robot;

import io.micro.api.robot.converter.RobotManagerConverter;
import io.micro.api.robot.converter.SwitchConverter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.user;
package io.micro.api.http.user;

import io.micro.api.user.converter.AuthorityConverter;
import io.micro.api.user.dto.OperateAuthorityDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.user;
package io.micro.api.http.user;

import io.micro.api.user.converter.UserConverter;
import io.micro.api.user.dto.OperateUserDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micro.api.wx;
package io.micro.api.http.wx;

import io.micro.api.wx.convert.WxMessageConvert;
import io.micro.api.wx.dto.AccessInfoDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.micro.api.mq.robot;

import io.micro.server.robot.infra.event.dto.RobotDTO;
import io.micro.server.robot.infra.event.sdk.RobotConstant;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Message;

import java.util.concurrent.CompletionStage;

@ApplicationScoped
public class RobotCustomer {

@Incoming(RobotConstant.Queue.ROBOT_LOGIN_SUCCESS)
public CompletionStage<Void> robotLoginSuccess(Message<RobotDTO> msg) {
RobotDTO robot = msg.getPayload();
System.out.println("==========");
System.out.println(robot.getId());
System.out.println("==========");
return msg.ack();
}

}
2 changes: 1 addition & 1 deletion tokisaki-core/src/main/kotlin/io/micro/core/robot/Robot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface Robot {
*/
fun close()

fun setStateChangeListener(block: (event: Event) -> Unit)
fun addStateChangeListener(block: suspend (event: Event) -> Unit)

interface LifeCycle {

Expand Down
69 changes: 35 additions & 34 deletions tokisaki-core/src/main/kotlin/io/micro/core/robot/qq/QQRobot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class QQRobot(private val id: Long, private val account: String) : Robot, Robot.
*/
var onGroupMessage: (suspend (GroupMessageEvent) -> Unit)? = null

private var onStateChange: ((Robot.Event) -> Unit)? = null
private var onStateChangeList: MutableList<suspend (Robot.Event) -> Unit> = mutableListOf()

/**
* 二维码获取开始事件
Expand Down Expand Up @@ -116,8 +116,8 @@ class QQRobot(private val id: Long, private val account: String) : Robot, Robot.
bot.close()
}

override fun setStateChangeListener(block: (event: Robot.Event) -> Unit) {
onStateChange = block
override fun addStateChangeListener(block: suspend (event: Robot.Event) -> Unit) {
onStateChangeList += block
}

/**
Expand All @@ -126,43 +126,44 @@ class QQRobot(private val id: Long, private val account: String) : Robot, Robot.
private fun canLogin() = state != Robot.State.LoggingIn || state != Robot.State.Online

override fun loggingInListener(event: Robot.Event) {
val handle = onStateChange
if (handle != null) {
when (event) {
is QRCodeStartEvent -> {
try {
handle(event)
} catch (ex: Exception) {
this.state = Robot.State.LoggingFail
loggingInListener(LoginFailEvent(ex))
scope.launch {
onStateChangeList.forEach { handle ->
when (event) {
is QRCodeStartEvent -> {
try {
handle(event)
} catch (ex: Exception) {
this@QQRobot.state = Robot.State.LoggingFail
loggingInListener(LoginFailEvent(ex))
}
}
}

is LoginSuccessEvent -> {
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
is LoginSuccessEvent -> {
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
}
state = Robot.State.Online
}
state = Robot.State.Online
}

is LoginFailEvent -> {
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
is LoginFailEvent -> {
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
}
state = Robot.State.LoggingFail
}
state = Robot.State.LoggingFail
}

is LoginTimeoutEvent -> {
state = Robot.State.Closed
bot.close()
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
is LoginTimeoutEvent -> {
state = Robot.State.Closed
bot.close()
try {
handle(event)
} catch (ex: Exception) {
Log.error(ex)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.micro.server.robot.domain.event

import io.micro.server.robot.domain.model.entity.RobotDO
import io.smallrye.mutiny.Uni

interface IRobotEvent {

fun publishRobotLoginSuccess(robotDO: RobotDO): Uni<Unit>

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class RobotDO : BaseDomainEntity() {
val validTypeIds = listOf(0)
val validStateIds = listOf(0, 1, 2, 3, 4, 5, 6, 7)
const val SPACE = " "
const val BRACES = "{}"
}

fun paramVerify() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.micro.server.robot.domain.model.valobj.Switch
import io.micro.server.robot.domain.repository.IRobotManagerRepository
import io.micro.server.robot.domain.service.FunctionService
import io.micro.server.robot.domain.service.RobotManagerService
import io.micro.server.robot.infra.event.RobotEvent
import io.quarkus.hibernate.reactive.panache.common.WithSession
import io.quarkus.hibernate.reactive.panache.common.WithTransaction
import io.quarkus.logging.Log
Expand Down Expand Up @@ -60,7 +61,8 @@ class RobotManagerServiceImpl(
private val functionContext: FunctionContext,
private val sessionFactory: Mutiny.SessionFactory,
private val objectMapper: ObjectMapper,
private val authService: AuthService
private val authService: AuthService,
private val robotEvent: RobotEvent
) : RobotManagerService {

/**
Expand Down Expand Up @@ -240,7 +242,7 @@ class RobotManagerServiceImpl(
val vertxContext = Vertx.currentContext()
return Multi.createFrom().emitter<String> { em ->
// 绑定登录回调函数
qqRobotEventEmitBind(robot, em)
qqRobotEventEmitBind(robotDO, robot, em)
// 群消息处理
onGroupMessage(robot, robotId, vertxContext)
// 注册机器人并开始登录
Expand Down Expand Up @@ -324,9 +326,9 @@ class RobotManagerServiceImpl(
* @param robot QQ机器人
* @param em 事件触发器
*/
private fun qqRobotEventEmitBind(robot: QQRobot, em: MultiEmitter<in String?>) {
private fun qqRobotEventEmitBind(robotDO: RobotDO, robot: QQRobot, em: MultiEmitter<in String?>) {
val id = robot.id()
robot.setStateChangeListener { event ->
robot.addStateChangeListener { event ->
when (event) {
is QQRobot.QRCodeStartEvent -> {
val qrCode = Base64.getEncoder().encodeToString(event.qr)
Expand All @@ -339,7 +341,10 @@ class RobotManagerServiceImpl(
manager.unregisterRobot(id)
}

is QQRobot.LoginSuccessEvent -> em.emit("success#")
is QQRobot.LoginSuccessEvent -> {
robotEvent.publishRobotLoginSuccess(robotDO).awaitSuspending()
em.emit("success#")
}
is QQRobot.LoginFailEvent -> em.emit("fail#${event.ex.message}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.micro.server.robot.infra.converter

import io.micro.server.robot.domain.model.entity.FeatureFunctionDO
import io.micro.server.robot.domain.model.entity.RobotDO
import io.micro.server.robot.infra.event.dto.RobotDTO
import io.micro.server.robot.infra.po.RobotEntity
import io.micro.server.robot.infra.po.UseFunctionEntity
import org.mapstruct.*
Expand Down Expand Up @@ -46,4 +47,6 @@ interface RobotConverter {
@MappingTarget useFunctionEntity: UseFunctionEntity
)

fun robotDO2RobotDTO(robotDO: RobotDO): RobotDTO

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.micro.server.robot.infra.event

import io.micro.server.robot.domain.event.IRobotEvent
import io.micro.server.robot.domain.model.entity.RobotDO
import io.micro.server.robot.infra.converter.RobotConverter
import io.micro.server.robot.infra.event.dto.RobotDTO
import io.micro.server.robot.infra.event.sdk.RobotConstant
import io.smallrye.mutiny.Uni
import io.smallrye.reactive.messaging.rabbitmq.OutgoingRabbitMQMetadata
import jakarta.enterprise.context.ApplicationScoped
import org.eclipse.microprofile.reactive.messaging.Channel
import org.eclipse.microprofile.reactive.messaging.Emitter
import org.eclipse.microprofile.reactive.messaging.Message
import org.eclipse.microprofile.reactive.messaging.Metadata
import java.util.concurrent.CompletableFuture

@ApplicationScoped
class RobotEvent(
private val robotConverter: RobotConverter,
@Channel(RobotConstant.Exchange.ROBOT_LOGIN) private val emitter: Emitter<RobotDTO>
) : IRobotEvent {

override fun publishRobotLoginSuccess(robotDO: RobotDO): Uni<Unit> {
val rabbitMQMetadata = OutgoingRabbitMQMetadata.builder()
.withRoutingKey(RobotConstant.RotingKey.ROBOT_LOGIN_SUCCESS)
.build()
return Uni.createFrom().emitter {
emitter.send(Message.of(robotConverter.robotDO2RobotDTO(robotDO), Metadata.of(rabbitMQMetadata), {
it.complete(null)
CompletableFuture.completedFuture(null)
}, { reason ->
it.fail(reason)
CompletableFuture.completedFuture(null)
}))
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.micro.server.robot.infra.event.dto

import io.micro.core.function.sdk.Cmd
import io.micro.server.robot.domain.model.valobj.Switch
import kotlin.properties.Delegates

class FeatureFunctionDTO {

var id: Long? = null

var refId: Long? = null

var code: String? = null

var name: String? = null

var config: String? = null

var remark: String? = null

var enabled: Boolean = false

var cmdAlias: String? = null

var requireQuota: Boolean? = null

var cmd: Cmd by Delegates.notNull()

var args: MutableList<String> = mutableListOf()

var switch: Switch = Switch()

var groupId: Long by Delegates.notNull()

var memberId: Long by Delegates.notNull()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.micro.server.robot.infra.event.dto

class RobotDTO {

var id: Long? = null

var userId: Long? = null

var name: String? = null

var account: String? = null

var password: String? = null

var type: Int? = null

var state: Int? = null

var remark: String? = null

val functions: MutableList<FeatureFunctionDTO> = mutableListOf()

var cmdPrefix: String? = null

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.micro.server.robot.infra.event.sdk

object RobotConstant {

object Exchange {
const val ROBOT_LOGIN = "robot-login"
}

object RotingKey {
const val ROBOT_LOGIN_SUCCESS = "robot.login.success"
}

object Queue {
const val ROBOT_LOGIN_SUCCESS = "robot-login-success"
}

}
Loading

0 comments on commit 4346745

Please sign in to comment.