import com.fasterxml.jackson.databind.ObjectMapper;
import msxf.model.People;
import msxf.until.ImpalaJdbc;
import msxf.until.NowDate;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by shengjk1 on 2016/6/23.
*/
public class Main {
private final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Main.class);
private static long c =System.currentTimeMillis();
public static void main(String[] args) {
try {
//impala 查询返回,peopleList
List<People> peopleList= ImpalaJdbc.connImpala();
Map ma=new HashMap();
ma.put("sendtime", NowDate.nowDate());
if(peopleList.size()==0){
logger.info("peopleList.size()==0");
}
ma.put("data",peopleList);
ObjectMapper om=new ObjectMapper();
PropertiesConfiguration pro =new PropertiesConfiguration("conf.properties");
String serviceAddr=pro.getString("serviceAddr");
logger.info("serviceAddr "+serviceAddr);
//将对象变为json
String jsonStr=om.writeValueAsString(ma);
// System.out.println(jsonStr);
CloseableHttpResponse httpResponse=null;
CloseableHttpClient httpClient= HttpClientBuilder.create().setRetryHandler(new DefaultHttpRequestRetryHandler()).build();
//设置超时时间
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectTimeout(1000*60*6); //单位毫秒
factory.setReadTimeout(1000*60*5);
//解决中文乱码
StringEntity stringEntity=new StringEntity(jsonStr,"UTF-8");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
HttpUriRequest httpUriRequest= RequestBuilder.post(serviceAddr).setEntity(stringEntity).build();
httpResponse=httpClient.execute(httpUriRequest);
logger.info(" 发送 ");
int statusCode=httpResponse.getStatusLine().getStatusCode();
if(statusCode== HttpStatus.SC_OK){
// HttpEntity entity = httpResponse.getEntity();
// InputStream in =entity.getContent();
logger.info("====文件传输服务器正常响应!");
long d =System.currentTimeMillis();
logger.info("结束 "+(d-c)/1000 +" s");
}else {
logger.info("====文件传输服务器未正常响应!");
}
}catch (Exception e) {
logger.error(e);
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有