客户业务欠费需要发邮件给它,明明只需要发1次的邮件,发了一堆,主要使用了 ConcurrentLinkedQueue 和spring taskExecutor 还有 javamail 启了5个线程来发,请各位帮忙看看谢谢!!
2分钟一次的定时器代码如下:
//SendMailForRealTimeAction.java
@Component("sendMailForRealTimeAction")
public class SendMailForRealTimeAction
{
private static final Logger logger = Logger.getLogger("MAIL");
@Resource
private TaskExecutor taskExecutor;
@Resource
private SendMailForRealTimeService sendMailForRealTimeService;
private Queue<Long> queueOfOpen = new ConcurrentLinkedQueue<Long>();
//入口
public void sendMail()
{
try
{
List<Long> list = sendMailForRealTimeService.getIdsOfSendMailForOpen();
if (null != list)
{
for (Long id : list)
{
queueOfOpen.offer(id);
}
for (int i = 0; i < Constants.SEND_MAIL_NUMBER_OF_THREADS; i++)
{
taskExecutor.execute(new SendMailThreadForOpen(queueOfOpen));
}
}
}
catch (Exception e)
{
......
}
}
//线程类(在类内部)
class SendMailThreadForOpen implements Runnable
{
private Queue<Long> queue;
public SendMailThreadForOpen(Queue<Long> queue) {
this.queue = queue;
}
public void run()
{
sendMailForRealTimeService.sendMailForOpen(queue);
}
}
}
//具体发送类
//SendMailForRealTimeServiceImpl.java
public void sendMailForOpen(Queue<Long> queue)
{
OpenMailInfo openMailInfo = null;
while (true)
{
if (!queue.isEmpty())
{
Long id = queue.poll();
if (null != id)
{
Long success = null;
try
{
//...拼接邮件内容,具体略过
// 执行邮件发送
javaMailSender.send(msg);
}
else
{
break;
}
}
}
日志如下:
Line 2097: MAIL:<INFO>:2015-01-19 07:07:41,701[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2245: MAIL:<INFO>:2015-01-19 07:07:49,135[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2393: MAIL:<INFO>:2015-01-19 07:07:56,454[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2400: MAIL:<INFO>:2015-01-19 07:08:00,241[taskExecutor-203]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2402: MAIL:<INFO>:2015-01-19 07:08:00,279[taskExecutor-201]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2477: MAIL:<INFO>:2015-01-19 07:08:00,856[taskExecutor-199]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2553: MAIL:<INFO>:2015-01-19 07:08:04,024[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2701: MAIL:<INFO>:2015-01-19 07:08:08,828[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2849: MAIL:<INFO>:2015-01-19 07:08:16,261[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 2997: MAIL:<INFO>:2015-01-19 07:08:23,712[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 3145: MAIL:<INFO>:2015-01-19 07:08:31,220[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
Line 3293: MAIL:<INFO>:2015-01-19 07:08:38,667[taskExecutor-198]>>发信成功。标题:【欠费提醒】套餐扣费余额不足 收信人:wei1.jie@net.com
看日志,好像是每个线程都得到一份同样的数据,每条数据都发了好几次,测试也无法重现,求教啊 ,分不够再加!
------解决思路----------------------
代码应该没什么问题,你这每次发送时就id不一样,但是日志没有将id打印出来
------解决思路----------------------
线程池中的线程是复用的,所以线程的id是会重复的,我说的是队列中存放的id是否是重复的,打印出来看看