List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager ThreadSafeClientConnManager
public ThreadSafeClientConnManager(final SchemeRegistry schreg, final long connTTL, final TimeUnit connTTLTimeUnit)
From source file:cn.keke.travelmix.HttpClientHelper.java
public static HttpClient getNewHttpClient() { try {//from w w w. jav a 2s .co m SSLSocketFactory sf = new EasySSLSocketFactory(); // TODO test, if SyncBasicHttpParams is needed HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, false); HttpProtocolParams.setHttpElementCharset(params, HTTP.UTF_8); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpConnectionParams.setLinger(params, 1); HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setSoReuseaddr(params, true); HttpConnectionParams.setTcpNoDelay(params, true); HttpClientParams.setCookiePolicy(params, CookiePolicy.IGNORE_COOKIES); HttpClientParams.setAuthenticating(params, false); HttpClientParams.setRedirecting(params, false); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register(new Scheme("https", 443, sf)); ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(registry, 20, TimeUnit.MINUTES); ccm.setMaxTotal(100); ccm.setDefaultMaxPerRoute(20); return new DefaultHttpClient(ccm, params); } catch (Exception e) { LOG.warn("Failed to create custom http client. Default http client is created", e); return new DefaultHttpClient(); } }
From source file:com.itude.mobile.mobbl.core.services.datamanager.handlers.MBRESTServiceDataHandler.java
@Override public MBDocument doLoadDocument(String documentName, MBDocument args) { MBEndPointDefinition endPoint = getEndPointForDocument(documentName); if (endPoint != null) { LOGGER.debug(/*www .j av a 2 s . com*/ "MBRESTServiceDataHandler:loadDocument " + documentName + " from " + endPoint.getEndPointUri()); String dataString = null; MBDocument responseDoc = null; String body = args.getValueForPath("/*[0]").toString(); try { HttpPost httpPost = new HttpPost(endPoint.getEndPointUri()); // Content-Type must be set because otherwise the MidletCommandProcessor servlet cannot read the XML httpPost.setHeader("Content-Type", "text/xml"); if (body != null) { httpPost.setEntity(new StringEntity(body)); } HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. int timeoutConnection = DEFAULT_TIMEOUTCONNECTION; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = DEFAULT_TIMEOUT_SOCKET; SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 8080, PlainSocketFactory.getSocketFactory())); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(registry, timeoutConnection, TimeUnit.MILLISECONDS); cm.setMaxTotal(MAX_TOTAL_CONNECTIONS); cm.setDefaultMaxPerRoute(MAX_CONNECTIONS_PER_ROUTE); HttpClient httpClient = new DefaultHttpClient(cm, httpParameters); if (endPoint.getTimeout() > 0) { timeoutSocket = endPoint.getTimeout() * 1000; timeoutConnection = endPoint.getTimeout() * 1000; } HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); if (ALLOW_ANY_CERTIFICATE) allowAnyCertificate(httpClient); HttpResponse httpResponse = httpClient.execute(httpPost); int responseCode = httpResponse.getStatusLine().getStatusCode(); String responseMessage = httpResponse.getStatusLine().getReasonPhrase(); if (responseCode != HttpStatus.SC_OK) { LOGGER.error("MBRESTServiceDataHandler.loadDocument: Received HTTP responseCode=" + responseCode + ": " + responseMessage); } HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream inStream = entity.getContent(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int i = inStream.read(buffer); while (i > -1) { bos.write(buffer, 0, i); i = inStream.read(buffer); } inStream.close(); dataString = new String(bos.toByteArray()); } boolean serverErrorHandled = false; for (MBResultListenerDefinition lsnr : endPoint.getResultListeners()) { if (lsnr.matches(dataString)) { MBResultListener rl = _applicationFactory.createResultListener(lsnr.getName()); rl.handleResult(dataString, args, lsnr); serverErrorHandled = true; } } /* if (delegate.err != nil) { String errorMessage = null; //[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; Log.w("MOBBL","An error ("+errorMessage+") occured while accessing endpoint "+endPoint.getEndPointUri()); throw new NetworkErrorException(MBLocalizationService.getInstance().textForKey((errorMessage); } //[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; */ if (dataString != null) { byte[] data = dataString.getBytes(); responseDoc = MBDocumentFactory.getInstance().getDocumentWithData(data, getDocumentFactoryType(), MBMetadataService.getInstance().getDefinitionForDocumentName(documentName)); } // if the response document is empty and unhandled by endpoint listeners let the user know there is a problem if (!serverErrorHandled && responseDoc == null) { String msg = MBLocalizationService.getInstance() .getTextForKey("The server returned an error. Please try again later"); // if(delegate.err != nil) { // msg = [NSString stringWithFormat:@"%@ %@: %@", msg, delegate.err.domain, delegate.err.code]; // } throw new MBServerErrorException(msg); } } // TODO: clean up exception handling catch (Exception e) { // debug in stead of info because it can contain a password LOGGER.debug("Sent xml:\n" + body); LOGGER.info("Received:\n" + dataString); if (e instanceof RuntimeException) throw (RuntimeException) e; else throw new ItudeRuntimeException(e); } return responseDoc; } else { LOGGER.warn("No endpoint defined for document name " + documentName); return null; } }