hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From KaiserSose <cbert...@libero.it>
Subject Re: Load balancer and Connection pool
Date Tue, 15 Jun 2010 09:13:56 GMT

Hi Sam, 
first of all thanks for your time and your help.
I can't set any packet sniffer on the frontends (since I don't have access
rights) ...
Here are the "interested" parts of my code ... this is a very reduced part
of the manager and its surrounded by other classes and so on ...


package it.seat.dol.http.connectionpool;

public class HttpConnectionManager {

    protected static final Logger log =
Logger.getLogger("HttpConnectionManager");
    private static HttpConnectionManager instance = null;
    private HttpParams hp = new BasicHttpParams();
    private HttpParams tempConfig;
    private Map<String, HttpConnectionParams> configList = new
HashMap<String, HttpConnectionParams>();
    BasicHttpContext localcontext = new BasicHttpContext();
    private DefaultHttpClient httpclient;
    private ThreadSafeClientConnManager poolManager;
    private static boolean isInit = false;
    
    // parametri che verranno letti dal file di risorse
    private static Integer managerMaxTotalConn = 0;
    private static Integer managerConnPerRoute = 0;
    

    private HttpConnectionManager() {
    }

      public static HttpConnectionManager getInstance() throws
HttpConnectorException {
        if (isInit()) {
            return instance;
        } else {
            log.warn("getInstance() __ tentativo di ottenere un istanza con
manager non inizializzato");
            throw new
HttpConnectorException(HttpConnectorException.HC_ERR_NOT_INIT,
                    "HttpConnectionManager.getInstance()",
                    "Impossibile ottenere il reference. Non � stato
inizializzato il Manager");
        }
    }

      public static synchronized void init(Properties connectorProperties)
throws HttpConnectorException {
        if (instance == null) {
            instance = new HttpConnectionManager();
            log.info("init() __ COSTRUITO IL MANAGER HTTP");
            try {
                ApplicationConfig.getInstance().init(connectorProperties);
                managerMaxTotalConn =
Integer.parseInt(ApplicationConfig.getInstance().getProperty("maxtotalconn"));
                managerConnPerRoute =
Integer.parseInt(ApplicationConfig.getInstance().getProperty("maxconnperroute"));
                if ((managerMaxTotalConn == 0) || (managerConnPerRoute ==
0)) {
                    log.error("init() __ Impossibile inizializzare con
managerMaxTotalConn= " + managerMaxTotalConn
                            + " e managerConnPerRoute= " +
managerConnPerRoute);
                    throw new
HttpConnectorException(HttpConnectorException.HC_ERR_WRONGPARAMETER,
                            "HttpConnectionManager.init() ",
                            "Errore nei parametri. Controllare
Application.properties"
                            + "o i parametri maxtotalconn e
maxconnperroute");
                }
            } catch (IOException ex) {
                if ((managerMaxTotalConn == 0) || (managerConnPerRoute ==
0)) {
                    log.error("init() __ errore nell'inizializzazione " +
ex);
                    throw new
HttpConnectorException(HttpConnectorException.HC_ERR_PATHFILE,
                            "HttpConnectionManager.init() ",
                            "Errore nel file delle proprietà");
                }
            }

            // logga           
            log.info("init() __ MANAGER HTTP INIZIALIZZAZIONE IN CORSO ...
");
            log.info("init() __ MANAGER HTTP CONNESSIONI TOTALI MASSIME: " +
managerMaxTotalConn);
            log.info("init() __ MANAGER HTTP CONNESSIONI MASSIME PER
PERCORSO: " + managerConnPerRoute);
            instance.startUp();
        }
    }

    public static synchronized void init() throws HttpConnectorException {
        init(null);
    }

    private SchemeRegistry createSchemeRegistry() {
        SchemeRegistry sr = new SchemeRegistry();
        sr.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));
        sr.register(new Scheme("https", SSLSocketFactory.getSocketFactory(),
443));
        log.info("createSchemeRegistry() __ MANAGER HTTP CREATO SCHEME
REGISTRY");
        return sr;
    }


    private void startUp() {
        if (!isInit()) {
            ConnManagerParamBean connParams = new ConnManagerParamBean(hp);
            // imposto le connessioni totali massime e di massime per
percorso
            connParams.setMaxTotalConnections(managerMaxTotalConn);
            hp.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new
ConnPerRoute() {
                @Override
                public int getMaxForRoute(HttpRoute route) {
                    return managerConnPerRoute;
                }
            });
            // Istanzio il connection manager ed il client http
            poolManager = new ThreadSafeClientConnManager(hp,
createSchemeRegistry());
            HttpManagerCleaner manCleaner = new
HttpManagerCleaner(poolManager, 60000);
            manCleaner.start();
            httpclient = new DefaultHttpClient(poolManager, hp);
            isInit = true;
            log.info("startUp() __ MANAGER HTTP INIZIALIZZAZIONE
COMPLETATA");
        }
    }

[....]

    private String doGetFinalRequest(String page, String user, String
password, HttpConnectionParams serviceParams) throws IOException,
HttpConnectorException {
        String result = null;
        if (isInit()) {
            Long startTime = null;
            if (log.isDebugEnabled()) {
                log.debug("[PROFILE]: Chiamata GET accettata");
                startTime = System.currentTimeMillis();
            }
            page = encodePage(page);
            String proxyAddress = "";
            Integer proxyPort = 0;
            String proxyUser = "";
            String proxyPassword = "";
            proxyAddress = (String) serviceParams.getParameter("PROXYHOST");
            proxyPort = (Integer) serviceParams.getParameter("PROXYPORT");
            proxyUser = (String) serviceParams.getParameter("PROXYUSER");
            proxyPassword = (String)
serviceParams.getParameter("PROXYPASSWORD");

            HttpEntity entity = null;
            HttpHost targetHost = serviceParams.getHostObject();
            HttpGet httpget = new HttpGet(page);
            httpget.setParams((HttpParams) serviceParams.getHttpParams());

            if ((!(proxyAddress == null)) && (!proxyAddress.equals("")) &&
(!(proxyPort == null)) && (proxyPort > 0)) {
                setProxy(httpget, proxyAddress, proxyPort, proxyUser,
proxyPassword);
            }

            if (!user.equals("")) {
                setHostAuthentication(targetHost.getHostName(),
targetHost.getPort(), user, password);
            }
           
            HttpResponse response = httpclient.execute(targetHost, httpget);

            switch (response.getStatusLine().getStatusCode()) {
                case HttpStatus.SC_OK:
                    // risposta ricevuta correttamente (200)
                    logIfDebug("doGetFinalRequest() __ Ottenuto un response
200 su " + targetHost.getHostName() + page);
                    entity = response.getEntity();
                    break;
                case HttpStatus.SC_MOVED_PERMANENTLY:
                    // redirect permanente ad un altra pagina (301)
                    logIfDebug("doGetFinalRequest() __ Ottenuto un response
301 su " + targetHost.getHostName() + page);
                    entity = response.getEntity();
                    break;
                case HttpStatus.SC_MOVED_TEMPORARILY:
                    // redirect temporaneo ad un altra pagina (302)
                    logIfDebug("doGetFinalRequest() __ Ottenuto un response
302 su " + targetHost.getHostName() + page);
                    entity = response.getEntity();
                    break;
                case HttpStatus.SC_UNAUTHORIZED:
                    logIfDebug("doGetFinalRequest() __ Response 401 su " +
targetHost.getHostName() + page + " connessione non autorizzata");
                    throw new
HttpConnectorException(HttpConnectorException.HC_ERR_NOT_AUTH,
"HttpRequester.doGetRequest()", "");
                default:
                    logIfDebug("doGetFinalRequest() __ Response " +
response.getStatusLine().getStatusCode() + " su " + targetHost.getHostName()
+ page);
                    throw new
HttpConnectorException(HttpConnectorException.HC_ERR_HTTP,
"HttpRequester.doGetRequest()", "");
            }

            if (entity != null) {
                result = EntityUtils.toString(entity);
                entity.consumeContent();
            }

        }
        return result;
    }

-- 
View this message in context: http://old.nabble.com/Load-balancer-and-Connection-pool-tp28883039p28888928.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message