今天做的线上微信支付出了问题,微信公众号支付--(统一下单失败)
查看原因,在做验证签名的时候报错:
java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.Make sure to release the connection before allocating another one.
提示错误,未释放链接,官方samples中提示方法:
CloseableHttpClient httpclient = HttpClients.createDefault();
try { HttpGet httpget = new HttpGet("http://www.apache.org/");
// Execute HTTP request
CloseableHttpResponse response = httpclient.execute(httpget);
try { // Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to bother about connection release
if (entity != null) { InputStream instream = entity.getContent();
try { instream.read(); // do something useful with the response
} catch (IOException ex) {
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
} finally {
// Closing the input stream will trigger connection release
instream.close();
}
}
} finally { response.close(); }
} finally { httpclient.close();}
在自己代码中加入了finally ,关闭httpclient之后又报了另一个错:
java.lang.IllegalStateException: Connection manager has been shut down
原因是:
public class GetWxOrderno {
private final static Loglogger = LogFactory.getLog(GetWxOrderno.class);
public static DefaultHttpClienthttpclient;
static {
httpclient =new DefaultHttpClient();
httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient);
}
将httpclient作为静态类变量,将这个类作为了工具类来调用,第一次请求进行httpclietn关闭之后,剩下来的请求便请求不到了.
做static处理是为了让其作为工具类来用,出现了并发访问,又不能进行关流,那么我们便做了同步处理:
public static synchronized String getPayNo(String url, String xmlParam) {...}
在调用的方法上加上synchronized.static加上synchronized会将整个类进行锁定,直至一次调用结束.
在加上同步锁之后,检测到httpclient请求超时错误;
然后结合运维同志们发现,是一处网卡未起到作用,导致请求不成功,这边重复发送请求未有响应,但也从侧面反映了自己代码没有考虑到同步和并发的问题.