/**
 * $Revision: 1.0
 * $Date: 2013-5-21
 *
 * Copyright (C) 2013-2020 MNT. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.mnt.base.mail;

import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mnt.base.util.BaseConfiguration;
import com.mnt.base.util.CommonUtil;

/**
 * support async send email
 * @author Peng Peng
 *
 */
public class AsyncMailHelper implements Runnable {
	
	private static final Log log = LogFactory.getLog(AsyncMailHelper.class);
	
	private static class MailHolder {
		String mailType;
		String mailTos;
		Map<String, Object> infoMap;
		
		public MailHolder(String mailType, String mailTos,
				Map<String, Object> infoMap) {
			super();
			this.mailType = mailType;
			this.mailTos = mailTos;
			this.infoMap = infoMap;
		}

		@Override
		public String toString() {
			return "MailHolder [mailType=" + mailType + ", mailTos=" + mailTos
					+ ", infoMap=" + infoMap + "]";
		}
	}
	
	private static BlockingQueue<MailHolder> mailQueue; 
	
	private AsyncMailHelper() {
		mailQueue = new LinkedBlockingQueue<MailHolder>(); 
	}
	
	static {
		new Thread(new AsyncMailHelper()).start();
	}
	
	
	
	public static void sendMail(String mailType, Map<String, Object> infoMap) {
        String toArrs =BaseConfiguration.getProperty("mail_notification_receivers").trim();
        sendMail(mailType, toArrs, infoMap);
	}
	
	public static void sendMail(String mailType, String mailTos, Map<String, Object> infoMap) {
		
		mailQueue.offer(new MailHolder(mailType, mailTos, infoMap));
	}

	@Override
	public void run() {
		MailHolder mh = null;
		
		while(true) {
			try {
				mh = mailQueue.take();
			} catch (InterruptedException e) {
				log.error("fail to retrieve mailholder, the server would be shutting down...", e);
				break;
			}
			
			if(mh != null) {
				
				try {
					MailHelper.sendMail(mh.mailType, mh.mailTos, mh.infoMap);
				} catch(Exception e) {
					log.error("fail to send the email: " + mh, e);
				}
				
				CommonUtil.deepClear(mh.infoMap);
			}
		}
	}
}
@@ -1195,24 +1195,132 @@
 class);%0D%0A%09%0D%0A
+%09private static final int MAX_RETRY = BaseConfiguration.getIntProperty(%22async_mail_send_max_retry%22, 3);%0D%0A%09%0D%0A
 %09private sta
@@ -2316,20 +2316,16 @@
 Map) %7B%0D%0A
-%09%09%0D%0A
 %09%09mailQu
@@ -2685,25 +2685,162 @@
 %09%09%0D%0A%09%09%09%09
+sendMail(mh, MAX_RETRY);%0D%0A%09%09%09%09CommonUtil.deepClear(mh.infoMap);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09private void sendMail(MailHolder mh, int maxRe
 try
+)
  %7B%0D%0A%09%09
+try %7B%0D%0A
 %09%09%09MailH
@@ -2893,18 +2893,16 @@
 oMap);%0D%0A
-%09%09
 %09%09%7D catc
@@ -2926,106 +2926,120 @@
 %0A%09%09%09
-%09%09log.error(%22fail to send the email: %22 + mh, e);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09%0D%0A%09%09%09%09CommonUtil.deepClear(mh.infoMap
+if(maxRetry %3E 0) %7B%0D%0A%09%09%09%09sendMail(mh, --maxRetry);%0D%0A%09%09%09%7D else %7B%0D%0A%09%09%09%09log.error(%22fail to send the email: %22 + mh, e
 );%0D%0A

添加异步队列重试机制。