mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-06 17:08:40 +08:00
添加了延迟短信的基础支持
This commit is contained in:
parent
d874a4b633
commit
2ffe43a4b6
@ -12,6 +12,7 @@ import kim.wind.sms.aliyun.config.AlibabaSmsConfig;
|
||||
import kim.wind.sms.api.SmsBlend;
|
||||
import kim.wind.sms.api.callback.CallBack;
|
||||
import kim.wind.sms.comm.annotation.Restricted;
|
||||
import kim.wind.sms.comm.delayedTime.DelayedTime;
|
||||
import kim.wind.sms.comm.entity.SmsResponse;
|
||||
import kim.wind.sms.comm.exception.SmsBlendException;
|
||||
import kim.wind.sms.comm.utils.HTTPUtils;
|
||||
@ -19,11 +20,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@EnableConfigurationProperties({AlibabaSmsConfig.class})
|
||||
@ -39,6 +39,9 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
@Qualifier("smsExecutor")
|
||||
private Executor pool;
|
||||
|
||||
@Autowired
|
||||
private DelayedTime delayed;
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public SmsResponse sendMessage(String phone, String message) {
|
||||
@ -131,10 +134,32 @@ public class AlibabaSmsImpl implements SmsBlend {
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public void sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
||||
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
||||
pool.execute(()->{
|
||||
SmsResponse smsResponse = sendMessage(phone,templateId,messages);
|
||||
callBack.callBack(smsResponse);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public void delayedMessage(String phone, String message, Long delayedTime) {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessage(phone,message);
|
||||
}
|
||||
},delayedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Restricted
|
||||
public void delayedMessage(String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
|
||||
this.delayed.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendMessage(phone,templateId,messages);
|
||||
}
|
||||
},delayedTime);
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,5 +68,26 @@ public interface SmsBlend {
|
||||
* @author :Wind
|
||||
*/
|
||||
|
||||
void sendMessage(String phone, String templateId, LinkedHashMap<String,String> messages, CallBack callBack);
|
||||
void sendMessageAsync(String phone, String templateId, LinkedHashMap<String,String> messages, CallBack callBack);
|
||||
|
||||
/**
|
||||
* <p>说明:
|
||||
* delayedMessage
|
||||
* @param phone 接收短信的手机号
|
||||
* @param message 要发送的短信
|
||||
* @param delayedTime 延迟时间
|
||||
* @author :Wind
|
||||
*/
|
||||
void delayedMessage(String phone ,String message,Long delayedTime);
|
||||
|
||||
/**
|
||||
* <p>说明:使用自定义模板发送定时短信 sendMessage
|
||||
* delayedMessage
|
||||
* @param templateId 模板id
|
||||
* @param messages key为模板变量名称 value为模板变量值
|
||||
* @param phone 要发送的手机号
|
||||
* @param delayedTime 延迟的时间
|
||||
* @author :Wind
|
||||
*/
|
||||
void delayedMessage(String phone ,String templateId, LinkedHashMap<String,String> messages,Long delayedTime);
|
||||
}
|
||||
|
||||
@ -0,0 +1,49 @@
|
||||
package kim.wind.sms.comm.delayedTime;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
|
||||
/**
|
||||
* <p>类名: DelayedTime
|
||||
* <p>说明: 定时器队列
|
||||
*
|
||||
* @author :Wind
|
||||
* 2023/3/25 21:22
|
||||
**/
|
||||
public class DelayedTime {
|
||||
|
||||
private final BlockingQueue<Task> queue = new PriorityBlockingQueue<>();
|
||||
|
||||
|
||||
|
||||
public DelayedTime() {
|
||||
Timer timer = new Timer(true);
|
||||
Thread t = new Thread(() -> {
|
||||
while (true) try {
|
||||
Task take = queue.take();
|
||||
timer.schedule(take.getRunnable(), take.getTime());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟队列添加新任务
|
||||
*/
|
||||
public void schedule(TimerTask task, long delay) {
|
||||
try {
|
||||
Task tasks = new Task();
|
||||
tasks.setTime(delay);
|
||||
tasks.setRunnable(task);
|
||||
queue.put(tasks);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package kim.wind.sms.comm.delayedTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.TimerTask;
|
||||
|
||||
@Data
|
||||
public class Task{
|
||||
|
||||
private TimerTask runnable;//描述要执行的任务
|
||||
private long time;//什么时间执行,用时间戳来表示
|
||||
|
||||
}
|
||||
@ -1,16 +1,16 @@
|
||||
package kim.wind.sms.starter.config;
|
||||
|
||||
import kim.wind.sms.aliyun.config.AlibabaSmsConfig;
|
||||
import kim.wind.sms.aliyun.service.AlibabaSmsImpl;
|
||||
import kim.wind.sms.api.SmsBlend;
|
||||
import kim.wind.sms.comm.delayedTime.DelayedTime;
|
||||
import kim.wind.sms.comm.utils.RedisUtils;
|
||||
import kim.wind.sms.comm.utils.SpringUtil;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import kim.wind.sms.comm.utils.SpringUtil;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
@ -96,9 +96,15 @@ public class SmsMainConfig {
|
||||
return new AopAdvice();
|
||||
}
|
||||
|
||||
/** 如果启用了redis作为缓存则注入redis工具类*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "sms", name = "redisCache", havingValue = "true")
|
||||
public RedisUtils redisUtils(RedisTemplate<String, Object> redisTemplate){
|
||||
return new RedisUtils(redisTemplate);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DelayedTime delayedTime(){
|
||||
return new DelayedTime();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user