Commit df96fb77 by caosy

add the gitignore fie;

parent c8b478ad
/**
* Copyright 2014 CTRIP Co.,Ltd. All rights reserved.
*/
package com.ctrip.fun.golf.service.message;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import com.ctrip.fun.common.vo.basic.SmsBean;
/**
* 用来处理发送短信的rpc调用
*
* @author zgsong
* @version 1.0.0
*/
public class MessageService {
private static Logger logger = LoggerFactory.getLogger(MessageService.class);
@Value("${corpId}")
private String corpId = null;
@Value("${corpPwd}")
private String corpPwd = null;
@Value("${corpService}")
private String corpService = null;
@Value("${corpUrl}")
private String corpUrl = null;
@Value("${useSystemProxy}")
private boolean useSystemProxy = false;
public String getCorpId() {
return corpId;
}
public void setCorpId(String corpId) {
this.corpId = corpId;
}
public String getCorpPwd() {
return corpPwd;
}
public void setCorpPwd(String corpPwd) {
this.corpPwd = corpPwd;
}
public String getCorpService() {
return corpService;
}
public void setCorpService(String corpService) {
this.corpService = corpService;
}
public String getCorpUrl() {
return corpUrl;
}
public void setCorpUrl(String corpUrl) {
this.corpUrl = corpUrl;
}
/*
* public SmsDao getSmsDao() { return smsDao; }
*
* public void setSmsDao(SmsDao smsDao) { this.smsDao = smsDao; }
*/
private void sendOnce(String mobile, String content, String msg_id) {
try {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if(useSystemProxy){
httpClientBuilder.useSystemProperties();
}
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost post = new HttpPost(corpUrl);
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("corp_id", corpId));
formparams.add(new BasicNameValuePair("corp_pwd", corpPwd));
formparams.add(new BasicNameValuePair("corp_service", corpService));
formparams.add(new BasicNameValuePair("mobile", mobile));
formparams.add(new BasicNameValuePair("msg_content", content));
formparams.add(new BasicNameValuePair("corp_msg_id", msg_id));
formparams.add(new BasicNameValuePair("ext", ""));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(entity);
HttpResponse httpResponse = closeableHttpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("send message result="+result);
post.releaseConnection();
} catch (Exception e) {
logger.error(e.getMessage(), e);
// throw new SmsException(e.getMessage(), e);
}
}
private void sendBatch(String mobiles, String content, String msg_id) {
try {
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if(useSystemProxy){
httpClientBuilder.useSystemProperties();
}
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost post = new HttpPost(corpUrl);
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("corp_id", corpId));
formparams.add(new BasicNameValuePair("corp_pwd", corpPwd));
formparams.add(new BasicNameValuePair("corp_service", corpService));
formparams.add(new BasicNameValuePair("mobile", mobiles));
formparams.add(new BasicNameValuePair("msg_content", content));
formparams.add(new BasicNameValuePair("corp_msg_id", msg_id));
formparams.add(new BasicNameValuePair("ext", ""));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
post.setEntity(entity);
// post.setConfig(getRequestConfig());
HttpResponse httpResponse = closeableHttpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println(result);
post.releaseConnection();
} catch (Exception e) {
logger.error(e.getMessage(), e);
// throw new SmsException(e.getMessage(), e);
}
}
/**
* 每个号码单独发消息
*
* @param list
* @throws IOException
* @throws HttpException
*/
public Boolean sendMessages(List<SmsBean> smsBeanList) {
Boolean result = true;
for (SmsBean smsBean : smsBeanList) {
try {
this.sendOnce(smsBean.getMobileNo(), smsBean.getMessage(), smsBean.getMessageId().toString());
} catch (Exception e) {
logger.error(e.getMessage(), e);
result = false;
// this.smsDao.updateSmsStatus(smsBeanList, SmsStatus.SEND_FAIL.getKey());
// throw new SmsException(e.getMessage(), e);
// TODO: 处理状态,失败次数
}
}
return result;
}
/**
* 发送单条信息
*
* @param list
* @throws IOException
* @throws HttpException
*/
public Boolean sendOnceMessage(SmsBean smsBean) {
Boolean result = true;
try {
this.sendOnce(smsBean.getMobileNo(), smsBean.getMessage(), smsBean.getMessageId().toString());
} catch (Exception e) {
// TODO: 处理状态,失败次数
logger.error(e.getMessage(), e);
result = false;
// List<SmsBean> smsBeanList = new ArrayList<SmsBean>();
// smsBeanList.add(smsBean);
// this.smsDao.updateSmsStatus(smsBeanList, SmsStatus.SEND_FAIL.getKey());
// throw new SmsException(e.getMessage(), e);
}
return result;
}
/**
* 多个号码发送同一消息
*
* @param numberList
* @param content
* @throws IOException
* @throws HttpException
*/
public Boolean sendBatchMessage(List<SmsBean> numberList, String content, String msg_id) {
Boolean result = true;
StringBuffer mobiles = new StringBuffer();
List<SmsBean> sendList = new ArrayList<SmsBean>();
int length = numberList.size();
int total = (0 == length % 200) ? (length / 200) : (length / 200 + 1);
for (int j = 0; j < total; j++) {
int start = j * 200;
int end = start + 200;
if (start > end || start > length) {
break;
}
if (end > length) {
end = length;
}
sendList = numberList.subList(start, end);
for (int i = 0; i < sendList.size(); i++) {
mobiles.append(sendList.get(i).getMobileNo()).append(",");
}
try {
this.sendBatch(mobiles.toString(), content, msg_id);
} catch (Exception e) {
logger.error(e.getMessage(), e); // TODO: 处理状态,失败次数
result = false;
// this.smsDao.updateSmsStatus(numberList, SmsStatus.SEND_FAIL.getKey());
// throw new SmsException(e.getMessage(), e);
}
}
return result;
}
public static void main(String[] args) throws Exception {
List<SmsBean> sms = new ArrayList<SmsBean>();
MessageService rpc = new MessageService();
rpc.useSystemProxy=true;
rpc.corpId = "wj5007";
rpc.corpPwd = "5376wh";
rpc.corpService = "1069007205007";
rpc.corpUrl = "http://cloud.hongshutech.com:8080/sms_send2.do";
for (int i = 0; i < 405; i++) {
SmsBean s = new SmsBean();
s.setMessage("ss");
s.setMobileNo("4111");
sms.add(s);
}
rpc.sendBatchMessage(sms, "ss", "ssss11");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment