public StatisticAggregator(CouchDbDaoFactory daoFactory, StatisticDao statisticDao) { _statisticDao = statisticDao; _packageDao = daoFactory.getPackageDao(); _productDao = daoFactory.getProductDao(); _productCategoryDao = daoFactory.getProductCategoryDao(); _shopCategoryDao = daoFactory.getShopCategoryDao(); _shopDao = daoFactory.getShopDao(); _unitDao = daoFactory.getUnitDao(); }
public void run() { _stopFlag = false; long seqNr = 0; while (!_stopFlag) { try { seqNr = Math.max(seqNr, _statisticDao.getMaxSequenceNr()); CouchDbConfig configuration = CouchDbDaoFactory.getConfiguration(); String host = configuration.getCouchHost(); String userInfo = null; if (configuration.getCouchUser() != null || configuration.getCouchPassword() != null) { userInfo = configuration.getCouchUser() + ":" + configuration.getCouchPassword(); } int port = configuration.getCouchPort(); String path = "/" + configuration.getCouchDatabase() + "/_changes"; String query = "feed=continuous&include_docs=true&since=" + seqNr; _client = new DefaultHttpClient(); URI uri = new URI("http", userInfo, host, port, path, query, null); _getRequest = new HttpGet(uri); HttpResponse response = _client.execute(_getRequest); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { if (line.length() > 0) { seqNr = Math.max(seqNr, parseLine(line)); } } } } catch (Throwable e) { Log.error("Exception in StatisticAggregator caught: " + e.getMessage(), e); } // make sure we won't hang in a CPU eating loop if there is an error try { sleep(5000); } catch (InterruptedException e) { // ignore it } } }