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

Java tutorial

Introduction

Here is the source code for uk.ac.aber.raf8.mmp.core.jobs.YQLCacheIndustryQueryJob.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.YQLIndustryQueryModel;
import uk.ac.aber.raf8.mmp.core.model.yql.sectors.YQLSectorModel;
import uk.ac.aber.raf8.mmp.core.model.yql.sectors.YQLSectorsQueryModel;
import uk.ac.aber.raf8.mmp.core.services.YQLService;

/**
 * Refreshes the YQLStocksQueryModel local cache.
 *
 * Created by raf8 on 09/02/2015.
 */
public class YQLCacheIndustryQueryJob 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 = new YQLIndustryQueryModel();
        final YQLSectorsQueryModel sectorsQueryModel = YQLQueryCache.getInstance().getYQLSectorsQueryModel();

        for (final YQLSectorModel sectorModel : sectorsQueryModel.getSectorModels()) {
            Boolean success = Boolean.FALSE;
            Integer tries = 0;
            while (!success && tries < MAX_TRIES) {
                try {
                    final YQLIndustryQueryModel temp = YQLService.getInstance()
                            .getYQLIndustryQueryModel(sectorModel.getIndustryModels());
                    industryQueryModel.getIndustryModels().addAll(temp.getIndustryModels());
                    success = Boolean.TRUE;
                } catch (final Exception e) {
                    e.printStackTrace();
                    tries++;
                    try {
                        Thread.sleep(DELAY_BETWEEN_TRIES);
                    } catch (final InterruptedException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }

        YQLQueryCache.getInstance().setYQLIndustryQueryModel(industryQueryModel);
    }
}