【发布时间】:2017-09-14 09:25:18
【问题描述】:
我有一个 cronjob,
users
id company_id
1 1
2 1
...
10000 1
cron_events
id event_name company_id
1 birthday_party 1
所以这里我需要向公司 1 的所有用户发送电子邮件,
我知道如何做到这一点,
但我想通过多线程概念来做到这一点,我是线程概念的新手,任何人都可以指导我,
这是我现有的代码,
这是我的控制器类
@Scheduled(fixedDelay = 15000)
public void sendNotificationToUser() {
Future<Boolean> sendNotificationToUser = cronService.sendNotificationToUser();
if (sendNotificationToUser.isDone()) {
logger.info(
"-->Notification [Sent] to Patient ");
}
}
这是我的实现类
@Async
@Transactional
public Future<Boolean> sendNotificationToUser() {
logger.info("-->in the function of sendNotificationToUser Function");
List<User> users = cronReferralRepository.findByCompanyIdAndActive(1L, true);
for (User user : users) {
Future<Boolean> futureCampaignCreated = processUser(user);
}
return new AsyncResult<Boolean>(true);
}
@Async
private Future<Boolean> processUser(User user) {
System.out.println("Thread Name --> " + Thread.currentThread().getName());
Long userId = user.getId();
String patientFirstName = null;
String patientLastName = null;
if (user != null) {
patientFirstName = user.getFirstName();
patientLastName = user.getLastName();
}
String patientFullName = patientFirstName + " " + patientLastName;
//send mail code
...
}
我的线程仍然打印相同的线程名称,这怎么可能,但我期待不同的线程名称,
【问题讨论】:
标签: java spring multithreading spring-boot java-threads