🎨 代码优化

This commit is contained in:
ronger 2023-05-13 08:52:15 +08:00
parent d66a515da0
commit 0eb8c9872e

View File

@ -10,9 +10,9 @@ import com.rymcu.forest.entity.Notification;
import com.rymcu.forest.entity.User; import com.rymcu.forest.entity.User;
import com.rymcu.forest.service.*; import com.rymcu.forest.service.*;
import javax.mail.MessagingException;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.*;
/** /**
* 消息通知工具类 * 消息通知工具类
@ -21,33 +21,25 @@ import java.util.concurrent.*;
*/ */
public class NotificationUtils { public class NotificationUtils {
private static NotificationService notificationService = SpringContextHolder.getBean(NotificationService.class); private static final NotificationService notificationService = SpringContextHolder.getBean(NotificationService.class);
private static UserService userService = SpringContextHolder.getBean(UserService.class); private static final UserService userService = SpringContextHolder.getBean(UserService.class);
private static FollowService followService = SpringContextHolder.getBean(FollowService.class); private static final FollowService followService = SpringContextHolder.getBean(FollowService.class);
private static JavaMailService mailService = SpringContextHolder.getBean(JavaMailService.class); private static final JavaMailService mailService = SpringContextHolder.getBean(JavaMailService.class);
private static final ArticleService articleService = SpringContextHolder.getBean(ArticleService.class);
private static ArticleService articleService = SpringContextHolder.getBean(ArticleService.class); private static final CommentService commentService = SpringContextHolder.getBean(CommentService.class);
private static CommentService commentService = SpringContextHolder.getBean(CommentService.class);
public static void sendAnnouncement(Long dataId, String dataType, String dataSummary) { public static void sendAnnouncement(Long dataId, String dataType, String dataSummary) {
ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
CompletableFuture.supplyAsync(() -> {
try {
List<User> users = userService.findAll(); List<User> users = userService.findAll();
users.forEach(user -> { users.forEach(user -> {
try {
saveNotification(user.getIdUser(), dataId, dataType, dataSummary); saveNotification(user.getIdUser(), dataId, dataType, dataSummary);
}); } catch (MessagingException e) {
} catch (Exception ex) { throw new RuntimeException(e);
ex.printStackTrace();
} }
return 0; });
}, executor);
} }
public static void saveNotification(Long idUser, Long dataId, String dataType, String dataSummary) { public static void saveNotification(Long idUser, Long dataId, String dataType, String dataSummary) throws MessagingException {
ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
CompletableFuture.supplyAsync(() -> {
try {
Notification notification = notificationService.findNotification(idUser, dataId, dataType); Notification notification = notificationService.findNotification(idUser, dataId, dataType);
if (notification == null || NotificationConstant.UpdateArticle.equals(dataType)) { if (notification == null || NotificationConstant.UpdateArticle.equals(dataType)) {
System.out.println("------------------- 开始执行消息通知 ------------------"); System.out.println("------------------- 开始执行消息通知 ------------------");
@ -61,19 +53,9 @@ public class NotificationUtils {
NotificationDTO notificationDTO = genNotification(notification); NotificationDTO notificationDTO = genNotification(notification);
mailService.sendNotification(notificationDTO); mailService.sendNotification(notificationDTO);
} }
} catch (Exception ex) {
// TODO 记录操作失败数据
ex.printStackTrace();
}
return 0;
}, executor);
} }
public static void sendArticlePush(Long dataId, String dataType, String dataSummary, Long articleAuthorId) { public static void sendArticlePush(Long dataId, String dataType, String dataSummary, Long articleAuthorId) {
ExecutorService executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
CompletableFuture.supplyAsync(() -> {
try {
List<Follow> follows; List<Follow> follows;
if (NotificationConstant.PostArticle.equals(dataType)) { if (NotificationConstant.PostArticle.equals(dataType)) {
// 关注用户通知 // 关注用户通知
@ -83,13 +65,12 @@ public class NotificationUtils {
follows = followService.findByFollowingId("3", articleAuthorId); follows = followService.findByFollowingId("3", articleAuthorId);
} }
follows.forEach(follow -> { follows.forEach(follow -> {
try {
saveNotification(follow.getFollowerId(), dataId, dataType, dataSummary); saveNotification(follow.getFollowerId(), dataId, dataType, dataSummary);
}); } catch (MessagingException e) {
} catch (Exception ex) { throw new RuntimeException(e);
ex.printStackTrace();
} }
return 0; });
}, executor);
} }
public static NotificationDTO genNotification(Notification notification) { public static NotificationDTO genNotification(Notification notification) {