Skip to content

Commit 4d10ca1

Browse files
committed
NotificationServiceManager优化代码, 通过枚举获取通知服务对象,而不是字符串
1 parent 97bb754 commit 4d10ca1

2 files changed

Lines changed: 24 additions & 33 deletions

File tree

src/main/java/io/github/opensabre/sysadmin/captcha/service/impl/EmailCaptchaService.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.opensabre.sysadmin.captcha.model.po.ClientInfo;
66
import io.github.opensabre.sysadmin.captcha.model.vo.CaptchaVo;
77
import io.github.opensabre.sysadmin.notification.enums.NotificationTemplate;
8+
import io.github.opensabre.sysadmin.notification.enums.NotificationType;
89
import io.github.opensabre.sysadmin.notification.service.NotificationServiceManager;
910
import lombok.extern.slf4j.Slf4j;
1011
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,12 +18,12 @@
1718
@Service
1819
public class EmailCaptchaService extends CaptchaService {
1920

20-
@Autowired
21-
private NotificationServiceManager notificationServiceManager;
21+
private final NotificationServiceManager notificationServiceManager;
2222

2323
@Autowired
24-
public EmailCaptchaService(EmailCaptchaGenerator captchaGenerator) {
24+
public EmailCaptchaService(EmailCaptchaGenerator captchaGenerator, NotificationServiceManager notificationServiceManager) {
2525
super(captchaGenerator);
26+
this.notificationServiceManager = notificationServiceManager;
2627
}
2728

2829
@Override
@@ -36,19 +37,14 @@ protected void beforeGenerateCaptcha(String businessKey, BusinessScenario scenar
3637
@Override
3738
protected CaptchaVo afterGenerateCaptcha(CaptchaInfo captchaInfo) {
3839
// Send Email
39-
try {
40-
String messageId = notificationServiceManager.sendNotification(
41-
"EMAIL",
42-
captchaInfo.getBusinessKey(),
43-
NotificationTemplate.CAPTCHA,
44-
captchaInfo.getCode(),
40+
String messageId = notificationServiceManager.sendNotification(
41+
NotificationType.EMAIL,
42+
captchaInfo.getBusinessKey(),
43+
NotificationTemplate.CAPTCHA,
44+
captchaInfo.getCode(),
4545
captchaInfo.getBusinessScenario().getCaptchaExpireTime() / 60
46-
);
47-
log.info("Email sent successfully, messageId: {}", messageId);
48-
} catch (Exception e) {
49-
log.error("Failed to send email captcha", e);
50-
}
51-
46+
);
47+
log.info("Email sent successfully, messageId: {}", messageId);
5248
// CaptchaVo
5349
return CaptchaVo.builder()
5450
.captchaId(captchaInfo.getCaptchaId())

src/main/java/io/github/opensabre/sysadmin/notification/service/NotificationServiceManager.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import io.github.opensabre.sysadmin.notification.enums.NotificationType;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.stereotype.Component;
7+
import org.springframework.util.Assert;
78

89
import java.util.List;
910
import java.util.Map;
10-
import java.util.concurrent.ConcurrentHashMap;
1111
import java.util.stream.Collectors;
1212

1313
/**
@@ -17,12 +17,11 @@
1717
@Component
1818
public class NotificationServiceManager {
1919

20-
private final Map<String, INotificationService> notificationServices;
20+
private final Map<NotificationType, INotificationService> notificationServices;
2121

2222
@Autowired
2323
public NotificationServiceManager(List<INotificationService> services) {
24-
this.notificationServices = services.stream()
25-
.collect(Collectors.toMap(service -> service.getType().getCode().toUpperCase(), service -> service));
24+
this.notificationServices = services.stream().collect(Collectors.toMap(INotificationService::getType, service -> service));
2625
}
2726

2827
/**
@@ -34,11 +33,9 @@ public NotificationServiceManager(List<INotificationService> services) {
3433
* @param args 模板参数
3534
* @return 发送结果
3635
*/
37-
public String sendNotification(String type, String target, NotificationTemplate template, Object... args) {
38-
INotificationService service = notificationServices.get(type.toUpperCase());
39-
if (service == null) {
40-
throw new IllegalArgumentException("Unsupported notification type: " + type);
41-
}
36+
public String sendNotification(NotificationType type, String target, NotificationTemplate template, Object... args) {
37+
INotificationService service = notificationServices.get(type);
38+
Assert.notNull(service, "No notification service found for type: " + type);
4239
return service.send(target, template, args);
4340
}
4441

@@ -51,11 +48,9 @@ public String sendNotification(String type, String target, NotificationTemplate
5148
* @param args 模板参数 (Map格式)
5249
* @return 发送结果
5350
*/
54-
public String sendNotification(String type, String target, NotificationTemplate template, Map<String, String> args) {
55-
INotificationService service = notificationServices.get(type.toUpperCase());
56-
if (service == null) {
57-
throw new IllegalArgumentException("Unsupported notification type: " + type);
58-
}
51+
public String sendNotification(NotificationType type, String target, NotificationTemplate template, Map<String, String> args) {
52+
INotificationService service = notificationServices.get(type);
53+
Assert.notNull(service, "No notification service found for type: " + type);
5954
return service.send(target, template, args);
6055
}
6156

@@ -65,8 +60,8 @@ public String sendNotification(String type, String target, NotificationTemplate
6560
* @param type 通知类型
6661
* @return 通知服务实例
6762
*/
68-
public INotificationService getService(String type) {
69-
return notificationServices.get(type.toUpperCase());
63+
public INotificationService get(NotificationType type) {
64+
return notificationServices.get(type);
7065
}
7166

7267
/**
@@ -75,7 +70,7 @@ public INotificationService getService(String type) {
7570
* @param type 通知类型
7671
* @return 是否支持
7772
*/
78-
public boolean supportsType(String type) {
79-
return notificationServices.containsKey(type.toUpperCase());
73+
public boolean supportsType(NotificationType type) {
74+
return notificationServices.containsKey(type);
8075
}
8176
}

0 commit comments

Comments
 (0)