重构文件类型相关Service以支持国际化查询 添加findOneByLang辅助方法统一查询逻辑 实现StringConverter支持UI控件显示 优化缓存配置和查询性能 新增UnitStringConverter和CustomerCatalogStringConverter 完善文档和测试用例
118 lines
3.4 KiB
Java
118 lines
3.4 KiB
Java
package com.ecep.contract;
|
||
|
||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import java.util.Locale;
|
||
import java.util.UUID;
|
||
import java.util.concurrent.CompletableFuture;
|
||
import java.util.logging.Level;
|
||
|
||
/**
|
||
* WebSocket客户端任务接口
|
||
* 定义了所有通过WebSocket与服务器通信的任务的通用方法
|
||
* 包括任务名称、更新消息、更新标题、更新进度等操作
|
||
*/
|
||
public interface WebSocketClientTasker {
|
||
/**
|
||
* 获取任务名称
|
||
*
|
||
* @return 任务名称
|
||
*/
|
||
String getTaskName();
|
||
|
||
/**
|
||
* 更新任务执行过程中的消息
|
||
*
|
||
* @param level 消息级别
|
||
* @param message 消息内容
|
||
*/
|
||
void updateMessage(Level level, String message);
|
||
|
||
/**
|
||
* 更新任务标题
|
||
*
|
||
* @param title 任务标题
|
||
*/
|
||
void updateTitle(String title);
|
||
|
||
/**
|
||
* 更新任务进度
|
||
*
|
||
* @param current 当前进度
|
||
* @param total 总进度
|
||
*/
|
||
void updateProgress(long current, long total);
|
||
|
||
/**
|
||
* 取消任务执行
|
||
* 默认实现为空,可由具体任务重写以提供取消逻辑
|
||
*/
|
||
default void cancelTask() {
|
||
// 默认实现为空,由具体任务重写
|
||
}
|
||
|
||
/**
|
||
* 调用远程WebSocket任务
|
||
*
|
||
* @param holder 消息持有者,用于记录任务执行过程中的消息
|
||
* @param locale 语言环境
|
||
* @param args 任务参数
|
||
* @return 任务执行结果
|
||
*/
|
||
default Object callRemoteTask(MessageHolder holder, Locale locale, Object... args) {
|
||
WebSocketClientService webSocketService = SpringApp.getBean(WebSocketClientService.class);
|
||
|
||
// 检查WebSocket连接是否可用
|
||
if (!webSocketService.getOnlineProperty().get()) {
|
||
String errorMsg = "WebSocket连接不可用,请检查网络连接或服务器状态";
|
||
holder.addMessage(Level.SEVERE, errorMsg);
|
||
return null;
|
||
}
|
||
|
||
webSocketService.withSession(session -> {
|
||
try {
|
||
session.submitTask(this, locale, args);
|
||
holder.addMessage(Level.INFO, "已提交任务到服务器: " + getTaskName());
|
||
} catch (JsonProcessingException e) {
|
||
String errorMsg = "任务提交失败: " + e.getMessage();
|
||
holder.addMessage(Level.SEVERE, errorMsg);
|
||
throw new RuntimeException("任务提交失败: " + e.getMessage(), e);
|
||
}
|
||
});
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 异步调用远程WebSocket任务
|
||
*
|
||
* @param holder 消息持有者,用于记录任务执行过程中的消息
|
||
* @param locale 语言环境
|
||
* @param args 任务参数
|
||
* @return 包含任务执行结果的CompletableFuture
|
||
*/
|
||
default CompletableFuture<Object> callRemoteTaskAsync(MessageHolder holder, Locale locale, Object... args) {
|
||
CompletableFuture<Object> future = new CompletableFuture<>();
|
||
|
||
try {
|
||
// 立即执行callRemoteTask并返回结果
|
||
Object result = callRemoteTask(holder, locale, args);
|
||
future.complete(result);
|
||
} catch (Exception e) {
|
||
future.completeExceptionally(e);
|
||
}
|
||
|
||
return future;
|
||
}
|
||
|
||
/**
|
||
* 生成唯一的任务ID
|
||
*
|
||
* @return 唯一的任务ID
|
||
*/
|
||
default String generateTaskId() {
|
||
return UUID.randomUUID().toString();
|
||
}
|
||
}
|