USVT-136 当使用 Spring 的多线程调用的时候,不应该再使用 JDK 提供的异步执行

This commit is contained in:
YuCheng Hu 2023-02-18 06:50:53 -05:00
parent 5330212b34
commit 79481d5118
1 changed files with 21 additions and 20 deletions

View File

@ -62,27 +62,27 @@ public class ChatGPTService {
String toUserName = weChatMessage.getFromUserName();
String fromUserName = weChatMessage.getToUserName();
String content = weChatMessage.getContent();
String contentGPT = StringUtils.EMPTY;
String contentGPT = getChatGPTContent(content);// StringUtils.EMPTY;
ExecutorService executor = Executors.newCachedThreadPool();
Callable<Object> task = new Callable<Object>() {
public String call() {
return getChatGPTContent(content);
}
};
Future<Object> future = executor.submit(task);
try {
Object result = future.get(3, TimeUnit.SECONDS);
contentGPT = (String) result;
} catch (TimeoutException ex) {
// handle the timeout
} catch (InterruptedException e) {
// handle the interrupts
} catch (ExecutionException e) {
// handle other exceptions
} finally {
future.cancel(true); // may or may not desire this
}
// ExecutorService executor = Executors.newCachedThreadPool();
// Callable<Object> task = new Callable<Object>() {
// public String call() {
// return getChatGPTContent(content);
// }
// };
// Future<Object> future = executor.submit(task);
// try {
// Object result = future.get(3, TimeUnit.SECONDS);
// contentGPT = (String) result;
// } catch (TimeoutException ex) {
// // handle the timeout
// } catch (InterruptedException e) {
// // handle the interrupts
// } catch (ExecutionException e) {
// // handle other exceptions
// } finally {
// future.cancel(true); // may or may not desire this
// }
weChatMessage.setToUserName(toUserName);
@ -106,6 +106,7 @@ public class ChatGPTService {
* @return
*/
private String getChatGPTContent(String content) {
log.debug(">>>>>>>> ChatGPT Content Call");
OpenAiService service = new OpenAiService("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");
CompletionRequest completionRequest = new CompletionRequestBuilder().setModel("text-davinci-003")
.setPrompt(content)