uk.ac.aber.raf8.mmp.core.jobs.YQLCacheStocksQueryJob.java Source code

Java tutorial

Introduction

Here is the source code for uk.ac.aber.raf8.mmp.core.jobs.YQLCacheStocksQueryJob.java

Source

/*
 * Copyright 2015 Roberto Augusto Fajardo Junior
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package uk.ac.aber.raf8.mmp.core.jobs;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import uk.ac.aber.raf8.mmp.core.cache.YQLQueryCache;
import uk.ac.aber.raf8.mmp.core.model.yql.industry.YQLIndustryModel;
import uk.ac.aber.raf8.mmp.core.model.yql.industry.YQLIndustryQueryModel;
import uk.ac.aber.raf8.mmp.core.model.yql.stocks.YQLStocksQueryModel;
import uk.ac.aber.raf8.mmp.core.services.YQLService;

/**
 * Refreshes the YQLStocksQueryModel local cache.
 *
 * Created by raf8 on 09/02/2015.
 */
public class YQLCacheStocksQueryJob extends QuartzJobBean {

    private static final Integer MAX_TRIES = 3;

    private static final Integer DELAY_BETWEEN_TRIES = 1000;

    @Override
    protected void executeInternal(final JobExecutionContext jobExecutionContext) throws JobExecutionException {

        final YQLIndustryQueryModel industryQueryModel = YQLQueryCache.getInstance().getYqlIndustryQueryModel();
        final YQLStocksQueryModel stocksQueryModel = new YQLStocksQueryModel();

        for (final YQLIndustryModel industryModel : industryQueryModel.getIndustryModels()) {
            Boolean success = Boolean.FALSE;
            Integer tries = 0;
            while (!success && tries < MAX_TRIES) {
                try {
                    final YQLStocksQueryModel temp = YQLService.getInstance()
                            .getYQLStocksQueryModel(industryModel.getCompanyModels());
                    stocksQueryModel.getStockModels().addAll(temp.getStockModels());
                    success = Boolean.TRUE;
                } catch (final Exception e) {
                    e.printStackTrace();
                    tries++;
                    try {
                        Thread.sleep(DELAY_BETWEEN_TRIES);
                    } catch (final InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }

        YQLQueryCache.getInstance().setYQLStocksQueryModel(stocksQueryModel);
    }
}