Class AbstractThreadRoutingStrategy
java.lang.Object
com.bytedesk.service.routing_strategy.AbstractThreadRoutingStrategy
- Direct Known Subclasses:
AgentThreadRoutingStrategy
,RobotThreadRoutingStrategy
,UnifiedThreadRoutingStrategy
,WorkflowThreadRoutingStrategy
,WorkgroupThreadRoutingStrategy
线程路由策略抽象基类
提供线程路由策略的通用功能和模板方法,减少具体策略实现的重复代码
主要功能: - 线程状态检查和验证 - 通用的线程操作方法 - 消息内容处理 - 异常处理和日志记录
- Since:
- 1.0.0
- Author:
- jackning 270580156@qq.com
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract MessageProtobuf
createThread
(VisitorRequest visitorRequest) 创建线程的核心方法 由具体实现类负责处理不同类型的线程创建逻辑protected <T> T
executeWithExceptionHandling
(String operation, String threadUid, Supplier<T> action) 创建操作执行模板方法protected String
generateQueueMessage
(int queueingCount) 生成排队等待消息protected ThreadEntity
getThreadByUid
(String threadUid) 根据UID获取线程实体protected abstract ThreadRestService
获取线程服务实例 由具体实现类提供protected String
getValidOfflineMessage
(String customMessage) 获取有效的离线消息protected String
getValidWelcomeMessage
(String customMessage) 获取有效的欢迎消息protected RuntimeException
handleStrategyException
(String operation, Throwable error, String threadUid) 处理策略执行异常protected boolean
isActiveThread
(ThreadEntity thread) 检查线程是否处于活跃状态(聊天中或排队中)protected boolean
isClosedThread
(ThreadEntity thread) 检查线程是否已关闭protected boolean
isMessageInCacheTime
(LocalDateTime messageTime) 检查消息是否在缓存时间内protected boolean
isNewThread
(ThreadEntity thread) 检查线程是否为新创建状态protected void
logThreadStatusChange
(ThreadEntity thread, String operation, String fromStatus, String toStatus) 记录线程状态变更protected ThreadEntity
saveThread
(ThreadEntity thread) 保存线程实体并处理异常protected void
validateThread
(ThreadEntity thread, String operation) 验证线程实体不为空protected void
validateUid
(String uid, String entityType) 验证UID格式有效性
-
Field Details
-
DEFAULT_WELCOME_MESSAGE
默认欢迎消息- See Also:
-
DEFAULT_OFFLINE_MESSAGE
默认离线消息- See Also:
-
QUEUE_NEXT_MESSAGE
排队等待消息 - 下一个- See Also:
-
QUEUE_WAITING_MESSAGE_TEMPLATE
排队等待消息模板- See Also:
-
ESTIMATED_WAIT_TIME_PER_PERSON
protected static final int ESTIMATED_WAIT_TIME_PER_PERSON每人预估等待时间(分钟)- See Also:
-
MESSAGE_CACHE_MINUTES
protected static final int MESSAGE_CACHE_MINUTES消息缓存时间(分钟) - 用于判断是否复用之前的消息- See Also:
-
-
Constructor Details
-
AbstractThreadRoutingStrategy
public AbstractThreadRoutingStrategy()
-
-
Method Details
-
createThread
创建线程的核心方法 由具体实现类负责处理不同类型的线程创建逻辑- Parameters:
visitorRequest
- 访客请求信息- Returns:
- 消息协议对象
-
getThreadRestService
获取线程服务实例 由具体实现类提供- Returns:
- ThreadRestService实例
-
validateThread
验证线程实体不为空- Parameters:
thread
- 线程实体operation
- 操作名称,用于错误日志- Throws:
IllegalArgumentException
- 如果线程为空
-
validateUid
验证UID格式有效性- Parameters:
uid
- 要验证的UIDentityType
- 实体类型名称,用于错误信息- Throws:
IllegalArgumentException
- 如果UID无效
-
getThreadByUid
根据UID获取线程实体- Parameters:
threadUid
- 线程UID- Returns:
- 线程实体
- Throws:
IllegalArgumentException
- 如果线程不存在
-
saveThread
保存线程实体并处理异常- Parameters:
thread
- 要保存的线程实体- Returns:
- 保存后的线程实体
- Throws:
RuntimeException
- 如果保存失败
-
isNewThread
检查线程是否为新创建状态- Parameters:
thread
- 线程实体- Returns:
- 如果是新状态返回true
-
isActiveThread
检查线程是否处于活跃状态(聊天中或排队中)- Parameters:
thread
- 线程实体- Returns:
- 如果处于活跃状态返回true
-
isClosedThread
检查线程是否已关闭- Parameters:
thread
- 线程实体- Returns:
- 如果已关闭返回true
-
getValidWelcomeMessage
获取有效的欢迎消息- Parameters:
customMessage
- 自定义消息- Returns:
- 有效的欢迎消息,如果自定义消息为空则返回默认消息
-
getValidOfflineMessage
获取有效的离线消息- Parameters:
customMessage
- 自定义消息- Returns:
- 有效的离线消息,如果自定义消息为空则返回默认消息
-
generateQueueMessage
生成排队等待消息- Parameters:
queueingCount
- 排队人数- Returns:
- 排队等待消息
-
isMessageInCacheTime
检查消息是否在缓存时间内- Parameters:
messageTime
- 消息时间- Returns:
- 如果在缓存时间内返回true
-
logThreadStatusChange
protected void logThreadStatusChange(ThreadEntity thread, String operation, String fromStatus, String toStatus) 记录线程状态变更- Parameters:
thread
- 线程实体operation
- 操作类型fromStatus
- 原状态toStatus
- 目标状态
-
handleStrategyException
protected RuntimeException handleStrategyException(String operation, Throwable error, String threadUid) 处理策略执行异常- Parameters:
operation
- 操作名称error
- 异常信息threadUid
- 线程UID(可选)- Returns:
- RuntimeException
-
executeWithExceptionHandling
protected <T> T executeWithExceptionHandling(String operation, String threadUid, Supplier<T> action) 创建操作执行模板方法- Parameters:
operation
- 操作名称threadUid
- 线程UIDaction
- 具体操作- Returns:
- 操作结果
-