Example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager MultiThreadedHttpConnectionManager

List of usage examples for org.apache.commons.httpclient MultiThreadedHttpConnectionManager MultiThreadedHttpConnectionManager

Introduction

In this page you can find the example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager MultiThreadedHttpConnectionManager.

Prototype

public MultiThreadedHttpConnectionManager() 

Source Link

Usage

From source file:org.kchine.r.server.http.RHttpProxy.java

private static GDDevice newDevice(String url, String sessionId, int width, int height, boolean broadcasted)
        throws TunnelingException {
    GetMethod getNewDevice = null;//from ww  w.  ja va  2s .co  m
    try {
        Object result = null;
        mainHttpClient = new HttpClient();
        if (System.getProperty("proxy_host") != null && !System.getProperty("proxy_host").equals("")) {
            mainHttpClient.getHostConfiguration().setProxy(System.getProperty("proxy_host"),
                    Integer.decode(System.getProperty("proxy_port")));
        }
        getNewDevice = new GetMethod(url + "?method=newdevice" + "&width=" + width + "&height=" + height
                + "&broadcasted=" + broadcasted);
        try {
            if (sessionId != null && !sessionId.equals("")) {
                getNewDevice.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
                getNewDevice.setRequestHeader("Cookie", "JSESSIONID=" + sessionId);
            }
            mainHttpClient.executeMethod(getNewDevice);
            result = new ObjectInputStream(getNewDevice.getResponseBodyAsStream()).readObject();
        } catch (ConnectException e) {
            throw new ConnectionFailedException();
        } catch (Exception e) {
            throw new TunnelingException("", e);
        }
        if (result != null && result instanceof TunnelingException) {
            throw (TunnelingException) result;
        }

        String deviceName = (String) result;
        return (GDDevice) RHttpProxy.getDynamicProxy(url, sessionId, deviceName, new Class[] { GDDevice.class },
                new HttpClient(new MultiThreadedHttpConnectionManager()));

    } finally {
        if (getNewDevice != null) {
            getNewDevice.releaseConnection();
        }
        if (mainHttpClient != null) {
        }
    }
}

From source file:org.kchine.r.server.http.RHttpProxy.java

private static GenericCallbackDevice newGenericCallbackDevice(String url, String sessionId)
        throws TunnelingException {
    GetMethod getNewDevice = null;/*from  w w  w . j a  va2 s  . c  o m*/
    try {
        Object result = null;
        mainHttpClient = new HttpClient();
        if (System.getProperty("proxy_host") != null && !System.getProperty("proxy_host").equals("")) {
            mainHttpClient.getHostConfiguration().setProxy(System.getProperty("proxy_host"),
                    Integer.decode(System.getProperty("proxy_port")));
        }
        getNewDevice = new GetMethod(url + "?method=newgenericcallbackdevice");
        try {
            if (sessionId != null && !sessionId.equals("")) {
                getNewDevice.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
                getNewDevice.setRequestHeader("Cookie", "JSESSIONID=" + sessionId);
            }
            mainHttpClient.executeMethod(getNewDevice);
            result = new ObjectInputStream(getNewDevice.getResponseBodyAsStream()).readObject();
        } catch (ConnectException e) {
            throw new ConnectionFailedException();
        } catch (Exception e) {
            throw new TunnelingException("", e);
        }
        if (result != null && result instanceof TunnelingException) {
            throw (TunnelingException) result;
        }

        String deviceName = (String) result;
        return (GenericCallbackDevice) RHttpProxy.getDynamicProxy(url, sessionId, deviceName,
                new Class[] { GenericCallbackDevice.class },
                new HttpClient(new MultiThreadedHttpConnectionManager()));

    } finally {
        if (getNewDevice != null) {
            getNewDevice.releaseConnection();
        }
        if (mainHttpClient != null) {
        }
    }
}

From source file:org.kchine.r.server.http.RHttpProxy.java

private static GDDevice[] listDevices(String url, String sessionId) throws TunnelingException {
    GetMethod getListDevices = null;//from   ww  w  . j  a va2 s  . c  om
    try {
        Object result = null;
        mainHttpClient = new HttpClient();
        if (System.getProperty("proxy_host") != null && !System.getProperty("proxy_host").equals("")) {
            mainHttpClient.getHostConfiguration().setProxy(System.getProperty("proxy_host"),
                    Integer.decode(System.getProperty("proxy_port")));
        }
        getListDevices = new GetMethod(url + "?method=listdevices");
        try {
            if (sessionId != null && !sessionId.equals("")) {
                getListDevices.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
                getListDevices.setRequestHeader("Cookie", "JSESSIONID=" + sessionId);
            }
            mainHttpClient.executeMethod(getListDevices);
            result = new ObjectInputStream(getListDevices.getResponseBodyAsStream()).readObject();
        } catch (ConnectException e) {
            throw new ConnectionFailedException();
        } catch (Exception e) {
            throw new TunnelingException("", e);
        }
        if (result != null && result instanceof TunnelingException) {
            throw (TunnelingException) result;
        }

        Vector<String> deviceNames = (Vector<String>) result;

        GDDevice[] devices = new GDDevice[deviceNames.size()];

        for (int i = 0; i < deviceNames.size(); ++i) {
            devices[i] = (GDDevice) RHttpProxy.getDynamicProxy(url, sessionId, deviceNames.elementAt(i),
                    new Class[] { GDDevice.class }, new HttpClient(new MultiThreadedHttpConnectionManager()));
        }

        return devices;

    } finally {
        if (getListDevices != null) {
            getListDevices.releaseConnection();
        }
        if (mainHttpClient != null) {
        }
    }
}

From source file:org.kchine.r.server.http.RHttpProxy.java

public static SpreadsheetModelDevice newSpreadsheetModelDevice(String url, String sessionId, String id,
        String rowcount, String colcount) throws TunnelingException {
    GetMethod getNewDevice = null;//from  w  w w.j av  a  2  s. c  om
    try {
        Object result = null;
        mainHttpClient = new HttpClient();
        if (System.getProperty("proxy_host") != null && !System.getProperty("proxy_host").equals("")) {
            mainHttpClient.getHostConfiguration().setProxy(System.getProperty("proxy_host"),
                    Integer.decode(System.getProperty("proxy_port")));
        }
        getNewDevice = new GetMethod(url + "?method=newspreadsheetmodeldevice&id=" + id + "&rowcount="
                + rowcount + "&colcount=" + colcount);

        try {
            if (sessionId != null && !sessionId.equals("")) {
                getNewDevice.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
                getNewDevice.setRequestHeader("Cookie", "JSESSIONID=" + sessionId);
            }
            mainHttpClient.executeMethod(getNewDevice);
            result = new ObjectInputStream(getNewDevice.getResponseBodyAsStream()).readObject();
        } catch (ConnectException e) {
            throw new ConnectionFailedException();
        } catch (Exception e) {
            throw new TunnelingException("", e);
        }
        if (result != null && result instanceof TunnelingException) {
            throw (TunnelingException) result;
        }

        String deviceName = (String) result;
        return (SpreadsheetModelDevice) RHttpProxy.getDynamicProxy(url, sessionId, deviceName,
                new Class[] { SpreadsheetModelDevice.class },
                new HttpClient(new MultiThreadedHttpConnectionManager()));

    } finally {
        if (getNewDevice != null) {
            getNewDevice.releaseConnection();
        }
        if (mainHttpClient != null) {
        }
    }
}

From source file:org.kchine.r.server.http.RHttpProxy.java

public static RServices getR(final String url, final String sessionId, final boolean handleCallbacks,
        final int maxNbrRactionsOnPop) {
    final HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    if (System.getProperty("proxy_host") != null && !System.getProperty("proxy_host").equals("")) {
        httpClient.getHostConfiguration().setProxy(System.getProperty("proxy_host"),
                Integer.decode(System.getProperty("proxy_port")));
    }//from  w w  w  .j  a  v a 2  s  . co  m
    final Object proxy = Proxy.newProxyInstance(RHttpProxy.class.getClassLoader(), new Class<?>[] {
            RServices.class, ScilabServices.class, OpenOfficeServices.class, HttpMarker.class },
            new InvocationHandler() {

                Vector<RCallBack> rCallbacks = new Vector<RCallBack>();
                Vector<RCollaborationListener> rCollaborationListeners = new Vector<RCollaborationListener>();
                Vector<RConsoleActionListener> rConsoleActionListeners = new Vector<RConsoleActionListener>();

                GenericCallbackDevice genericCallBackDevice = null;
                Thread popThread = null;

                boolean _stopThreads = false;
                {
                    if (handleCallbacks) {

                        try {
                            genericCallBackDevice = newGenericCallbackDevice(url, sessionId);
                            popThread = new Thread(new Runnable() {
                                public void run() {
                                    while (true && !_stopThreads) {
                                        popActions();

                                        try {
                                            Thread.sleep(10);
                                        } catch (Exception e) {
                                        }
                                    }

                                }
                            });
                            popThread.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                private synchronized void popActions() {
                    try {

                        Vector<RAction> ractions = genericCallBackDevice.popRActions(maxNbrRactionsOnPop);
                        if (ractions != null) {

                            for (int i = 0; i < ractions.size(); ++i) {
                                final RAction action = ractions.elementAt(i);
                                if (action.getActionName().equals("notify")) {
                                    HashMap<String, String> parameters = (HashMap<String, String>) action
                                            .getAttributes().get("parameters");
                                    for (int j = 0; j < rCallbacks.size(); ++j) {
                                        rCallbacks.elementAt(j).notify(parameters);
                                    }
                                }
                                if (action.getActionName().equals("rConsoleActionPerformed")) {
                                    RConsoleAction consoleAction = (RConsoleAction) action.getAttributes()
                                            .get("consoleAction");
                                    for (int j = 0; j < rConsoleActionListeners.size(); ++j) {
                                        rConsoleActionListeners.elementAt(j)
                                                .rConsoleActionPerformed(consoleAction);
                                    }
                                } else if (action.getActionName().equals("chat")) {
                                    String sourceUID = (String) action.getAttributes().get("sourceUID");
                                    String user = (String) action.getAttributes().get("user");
                                    String message = (String) action.getAttributes().get("message");
                                    for (int j = 0; j < rCollaborationListeners.size(); ++j) {
                                        rCollaborationListeners.elementAt(j).chat(sourceUID, user, message);
                                    }
                                } else if (action.getActionName().equals("consolePrint")) {
                                    String sourceUID = (String) action.getAttributes().get("sourceUID");
                                    String user = (String) action.getAttributes().get("user");
                                    String expression = (String) action.getAttributes().get("expression");
                                    String result = (String) action.getAttributes().get("result");
                                    for (int j = 0; j < rCollaborationListeners.size(); ++j) {
                                        rCollaborationListeners.elementAt(j).consolePrint(sourceUID, user,
                                                expression, result);
                                    }
                                }
                            }

                        }

                    } catch (NotLoggedInException nle) {

                        nle.printStackTrace();
                    } catch (Exception e) {

                        e.printStackTrace();
                    }

                }

                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    Object result = null;
                    if (method.getName().equals("newDevice")) {
                        result = newDevice(url, sessionId, (Integer) args[0], (Integer) args[1], false);
                    } else if (method.getName().equals("newBroadcastedDevice")) {
                        result = newDevice(url, sessionId, (Integer) args[0], (Integer) args[1], true);
                    } else if (method.getName().equals("listDevices")) {
                        result = listDevices(url, sessionId);
                    } else if (method.getName().equals("addRCallback")) {
                        rCallbacks.add((RCallBack) args[0]);
                    } else if (method.getName().equals("removeRCallback")) {
                        rCallbacks.remove((RCallBack) args[0]);
                    } else if (method.getName().equals("removeAllRCallbacks")) {
                        rCallbacks.removeAllElements();
                    }

            else if (method.getName().equals("addRCollaborationListener")) {
                        rCollaborationListeners.add((RCollaborationListener) args[0]);
                    } else if (method.getName().equals("removeRCollaborationListener")) {
                        rCollaborationListeners.remove((RCollaborationListener) args[0]);
                    } else if (method.getName().equals("removeAllRCollaborationListeners")) {
                        rCollaborationListeners.removeAllElements();
                    }

            else if (method.getName().equals("addRConsoleActionListener")) {
                        rConsoleActionListeners.add((RConsoleActionListener) args[0]);
                    } else if (method.getName().equals("removeRConsoleActionListener")) {
                        rConsoleActionListeners.remove((RConsoleActionListener) args[0]);
                    } else if (method.getName().equals("removeAllRConsoleActionListeners")) {
                        rConsoleActionListeners.removeAllElements();
                    }

            else if (method.getName().equals("newSpreadsheetTableModelRemote")) {
                        SpreadsheetModelDevice d = newSpreadsheetModelDevice(url, sessionId, "",
                                ((Integer) args[0]).toString(), ((Integer) args[1]).toString());
                        result = new SpreadsheetModelRemoteProxy(d);
                    } else if (method.getName().equals("getSpreadsheetTableModelRemote")) {
                        SpreadsheetModelDevice d = newSpreadsheetModelDevice(url, sessionId, (String) args[0],
                                "", "");
                        result = new SpreadsheetModelRemoteProxy(d);
                    }

            else if (method.getName().equals("stopThreads")) {
                        _stopThreads = true;
                        popThread.join();
                        try {
                            // IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!
                            // genericCallBackDevice.dispose();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else if (method.getName().equals("popActions")) {
                        popActions();
                    }

            else {

                        result = RHttpProxy.invoke(url, sessionId, "R", method.getName(),
                                method.getParameterTypes(), args, httpClient);
                    }

                    if (method.getName().equals("asynchronousConsoleSubmit")) {
                        popActions();
                    }

                    return result;

                }
            });

    return (RServices) proxy;
}

From source file:org.lilyproject.indexer.batchbuild.IndexingMapper.java

@Override
protected void setup(Context context) throws IOException, InterruptedException {
    super.setup(context);

    try {/* w w  w  . j  a  v  a2s . c o  m*/
        Configuration jobConf = context.getConfiguration();

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", jobConf.get("hbase.zookeeper.quorum"));
        conf.set("hbase.zookeeper.property.clientPort", jobConf.get("hbase.zookeeper.property.clientPort"));

        idGenerator = new IdGeneratorImpl();

        String zkConnectString = jobConf.get("org.lilyproject.indexer.batchbuild.zooKeeperConnectString");
        int zkSessionTimeout = getIntProp("org.lilyproject.indexer.batchbuild.zooKeeperSessionTimeout", null,
                jobConf);
        zk = ZkUtil.connect(zkConnectString, zkSessionTimeout);
        hbaseTableFactory = new HBaseTableFactoryImpl(conf, null, null);
        TypeManager typeManager = new HBaseTypeManager(idGenerator, conf, zk, hbaseTableFactory);

        BlobStoreAccessFactory blobStoreAccessFactory = LilyClient.getBlobStoreAccess(zk);

        RowLog wal = new DummyRowLog("The write ahead log should not be called from within MapReduce jobs.");
        repository = new HBaseRepository(typeManager, idGenerator, blobStoreAccessFactory, wal, conf,
                hbaseTableFactory);

        byte[] indexerConfBytes = Base64.decode(jobConf.get("org.lilyproject.indexer.batchbuild.indexerconf"));
        IndexerConf indexerConf = IndexerConfBuilder.build(new ByteArrayInputStream(indexerConfBytes),
                repository);

        Map<String, String> solrShards = new HashMap<String, String>();
        for (int i = 1; true; i++) {
            String shardName = jobConf.get("org.lilyproject.indexer.batchbuild.solrshard.name." + i);
            String shardAddress = jobConf.get("org.lilyproject.indexer.batchbuild.solrshard.address." + i);
            if (shardName == null)
                break;
            solrShards.put(shardName, shardAddress);
        }

        ShardSelector shardSelector;
        String shardingConf = jobConf.get("org.lilyproject.indexer.batchbuild.shardingconf");
        if (shardingConf != null) {
            byte[] shardingConfBytes = Base64.decode(shardingConf);
            shardSelector = JsonShardSelectorBuilder.build(shardingConfBytes);
        } else {
            shardSelector = DefaultShardSelectorBuilder.createDefaultSelector(solrShards);
        }

        connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.getParams().setDefaultMaxConnectionsPerHost(5);
        connectionManager.getParams().setMaxTotalConnections(50);
        HttpClient httpClient = new HttpClient(connectionManager);

        SolrServers solrServers = new SolrServers(solrShards, shardSelector, httpClient);

        indexLocker = new IndexLocker(zk);

        indexer = new Indexer(indexerConf, repository, solrServers, indexLocker, new IndexerMetrics("dummy"));

        int workers = getIntProp("org.lilyproject.indexer.batchbuild.threads", 5, jobConf);

        executor = new ThreadPoolExecutor(workers, workers, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(1000));
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

    } catch (Exception e) {
        throw new IOException("Error in index build map task setup.", e);
    }
}

From source file:org.lilyproject.indexer.engine.SolrServers.java

/**
 * This method is only meant for use by test cases.
 *//* w w  w. j  a  v a  2 s. c o m*/
public static SolrServers createForOneShard(String uri)
        throws URISyntaxException, ShardingConfigException, MalformedURLException {
    SortedMap<String, String> shards = new TreeMap<String, String>();
    shards.put("shard1", uri);
    ShardSelector selector = DefaultShardSelectorBuilder.createDefaultSelector(shards);
    return new SolrServers(shards, selector, new HttpClient(new MultiThreadedHttpConnectionManager()));
}

From source file:org.lilyproject.indexer.worker.IndexerWorker.java

@PostConstruct
public void init() {
    connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setDefaultMaxConnectionsPerHost(5);
    connectionManager.getParams().setMaxTotalConnections(50);
    httpClient = new HttpClient(connectionManager);

    eventWorkerThread = new Thread(new EventWorker(), "IndexerWorkerEventWorker");
    eventWorkerThread.start();/* w w w.j  a va2  s .c o  m*/

    synchronized (indexUpdatersLock) {
        Collection<IndexDefinition> indexes = indexerModel.getIndexes(listener);

        for (IndexDefinition index : indexes) {
            if (shouldRunIndexUpdater(index)) {
                addIndexUpdater(index);
            }
        }
    }
}

From source file:org.lockss.util.urlconn.LockssUrlConnectionPool.java

public void setMultiThreaded(int maxConn, int maxPerHost) {
    MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = cm.getParams();
    params.setMaxTotalConnections(maxConn);
    params.setDefaultMaxConnectionsPerHost(maxPerHost);
    setTimeouts(params);// w w  w .ja  v a 2s.c o m
    hcConnManager = cm;
    if (httpClient != null) {
        httpClient.setHttpConnectionManager(cm);
    }
}

From source file:org.mapfish.print.config.Config.java

private synchronized MultiThreadedHttpConnectionManager getConnectionManager() {
    if (connectionManager == null) {
        connectionManager = new MultiThreadedHttpConnectionManager();
        final HttpConnectionManagerParams params = connectionManager.getParams();
        params.setDefaultMaxConnectionsPerHost(perHostParallelFetches);
        params.setMaxTotalConnections(globalParallelFetches);
        params.setSoTimeout(socketTimeout);
        params.setConnectionTimeout(connectionTimeout);
    }/*from  w ww  .  j a v  a 2s.c  o  m*/
    return connectionManager;
}