java線程池的使用-發送短信驗證碼

package com.wei.service.sms.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wei.constants.CacheConstant;
import com.wei.constants.SysConstants;
import com.wei.service.CacheService;
import com.wei.service.sms.SmsService;
import com.wei.util.DateUtil;
import com.wei.util.ErrorCode;
import com.wei.util.HttpResult;
import com.wei.util.HttpUtils;
import com.wei.util.VerifyUtils;
/**
 * @author changning
 */
@SuppressWarnings("unchecked")
@Service("smsService")
public class SmsServiceImpl implements SmsService {
	private static final transient Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);
	@Autowired@Qualifier("cacheService")
	private CacheService cacheService;
	private static final String SMS_URL = "http://116.213.72.20/SMSHttpService/send.aspx";
	private static final String SMS_USERNAME = "bdxx1";
	private static final String SMS_PASSWORD = "bd111111";
	private ThreadPoolExecutor smsPoolExecutor;
	private int corePoolSize = 5;
	private int maximumPoolSize = 10;
	
	@Override
	public void sendSms(String type, String mobile, String content) {
		if (type.equals(SysConstants.SMS_TYPE_VERIFYMOBILE)) {
			smsPoolExecutor.execute(new VerifyMobileSmsTask(mobile));
		}
	}

	@PostConstruct
	public void init() {
		BlockingQueue<Runnable> taskQueue = new ArrayBlockingQueue<Runnable>(3000);
		smsPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 30, TimeUnit.SECONDS, taskQueue);
		smsPoolExecutor.allowCoreThreadTimeOut(false);
	}
	
	private ErrorCode<String> sendVerifyMobileSms(String mobile) {
		if (!VerifyUtils.isMobileNo(mobile)) {
			return ErrorCode.CODE_PARAM_ERROR_MOBILE;
		}
		String mobileValidateCode = String.valueOf(Math.random()*9000+1000).substring(0, 4);
		String content = SysConstants.SMS_TITLE_VERIFYMOBILE + mobileValidateCode;
		Map<String,String> params=new HashMap<String,String>();
		params.put("username", SMS_USERNAME);
		params.put("password", SMS_PASSWORD);
		params.put("mobile", mobile);
		params.put("content", content);
		params.put("extcode", "");
		params.put("senddate",DateUtil.formatTimestamp(new Date()));
		params.put("batchID", "");
		HttpResult result = HttpUtils.postUrlAsString(SMS_URL, params);
		if (result.getStatus() == HttpStatus.SC_OK && result.getResponse().equals(SysConstants.SMS_SUCCESS)) {
			String key = SysConstants.CACHE_KEY_VERIFYMOBILE + mobile;
			cacheService.set(CacheConstant.REGION_TENMIN, key, mobileValidateCode);
			return ErrorCode.getSuccess(mobile + SysConstants.SMS_VERIFYMOBILE_SUCCESS);
		} else {
			return ErrorCode.getFailure(mobile + SysConstants.SMS_VERIFYMOBILE_ERROR);
		}
	}
	
	private class VerifyMobileSmsTask implements Runnable {
		private String mobile;
		public VerifyMobileSmsTask(String mobile) {
			this.mobile = mobile;
		}
		@Override
		public void run() {
			ErrorCode<String> code = sendVerifyMobileSms(mobile);
			if (!code.isSuccess()) {
				logger.error(code.getMsg());
			}
		}
	}
}

發佈了48 篇原創文章 · 獲贊 19 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章