Add Callback2 interface and async callback support

This commit is contained in:
RB 2025-08-22 10:07:17 +08:00
parent dbc8bda110
commit 1ddd9309f7
2 changed files with 59 additions and 3 deletions

View file

@ -0,0 +1,23 @@
/*!
Copyright (c) REBUILD <https://getrebuild.com/> and/or its owners. All rights reserved.
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/
package com.rebuild.core.support;
/**
* @author devezhao
* @since 8/22/2025
*/
public interface Callback2 {
/**
* 回调方法
*
* @param res 可能是任何值请按需处理
*/
default void onComplete(Object res) {
}
}

View file

@ -18,6 +18,7 @@ import com.rebuild.core.Application;
import com.rebuild.core.configuration.ConfigurationException;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.privileges.UserService;
import com.rebuild.core.support.Callback2;
import com.rebuild.core.support.ConfigurationItem;
import com.rebuild.core.support.HeavyStopWatcher;
import com.rebuild.core.support.License;
@ -60,13 +61,15 @@ public class SMSender {
private static final int TYPE_SMS = 1;
private static final int TYPE_EMAIL = 2;
// -- 邮件
/**
* @param to
* @param subject
* @param content
*/
public static void sendMailAsync(String to, String subject, String content) {
sendMailAsync(to, subject, content, null);
sendMailAsync(to, subject, content, null, null);
}
/**
@ -76,11 +79,25 @@ public class SMSender {
* @param attach
*/
public static void sendMailAsync(String to, String subject, String content, File[] attach) {
sendMailAsync(to, subject, content, attach, null);
}
/**
* @param to
* @param subject
* @param content
* @param attach
* @param cb
*/
public static void sendMailAsync(String to, String subject, String content, File[] attach, Callback2 cb) {
ThreadPool.exec(() -> {
try {
sendMail(to, subject, content, attach);
String sendid = sendMail(to, subject, content, attach);
if (cb != null) cb.onComplete(sendid);
} catch (Exception ex) {
log.error("Email send error : {}, {}", to, subject, ex);
if (cb != null) cb.onComplete(ex);
}
});
}
@ -310,16 +327,30 @@ public class SMSender {
return MT_CACHE.clone();
}
// -- 短信
/**
* @param to
* @param content
*/
public static void sendSMSAsync(String to, String content) {
sendSMSAsync(to, content, null);
}
/**
* @param to
* @param content
* @param cb
*/
public static void sendSMSAsync(String to, String content, Callback2 cb) {
ThreadPool.exec(() -> {
try {
sendSMS(to, content);
String sendid = sendSMS(to, content);
if (cb != null) cb.onComplete(sendid);
} catch (Exception ex) {
log.error("SMS send error : {}, {}", to, content, ex);
if (cb != null) cb.onComplete(ex);
}
});
}
@ -387,6 +418,8 @@ public class SMSender {
return null;
}
// -- SUPPORTS
// @see com.rebuild.core.support.CommonsLog
private static void createLog(String to, String content, int type, String sentid, Object error) {
if (!Application.isReady()) return;