java关于验证码多线程_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 taskQueue = new ArrayBlockingQueue(3000);

smsPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 30, TimeUnit.SECONDS, taskQueue);

smsPoolExecutor.allowCoreThreadTimeOut(false);

}

private ErrorCode 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 params=new HashMap();

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 code = sendVerifyMobileSms(mobile);

if (!code.isSuccess()) {

logger.error(code.getMsg());

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值