Java tutorial
/* * EYWA.COM (Eywa Commerce) * This program is an integrated platform with E-Commerce and Configurator system. * Support: Please, contact the Author on http://www.smartfeeling.org. * Copyright (C) 2014 Gian Angelo Geminiani * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * */ package com.eywa.impl.app.mongo.services; import com.eywa.impl.app.controllers.central.CentralControl; import com.eywa.impl.app.controllers.scheduled.IJobConstants; import com.eywa.impl.app.mongo.entities.ScheduledJob; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBObject; import org.ly.Smartly; import org.ly.commons.logging.Logger; import org.ly.commons.logging.util.LoggingUtils; import org.ly.commons.util.DateUtils; import org.ly.commons.util.FormatUtils; import org.ly.packages.mongo.impl.AbstractMongoService; import org.ly.packages.mongo.impl.util.MongoUtils; import org.ly.proxies.DBProxy; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * @author angelo.geminiani */ public final class ScheduledJobService extends AbstractMongoService { // -------------------------------------------------------------------- // c o n s t r u c t o r // -------------------------------------------------------------------- public ScheduledJobService() throws Exception { super((DB) DBProxy.get().getDBMain(), ScheduledJob.COLLECTION, Smartly.getLanguages()); } // -------------------------------------------------------------------- // p u b l i c // -------------------------------------------------------------------- public List<DBObject> removeExpired() throws Exception { final DBObject query = MongoUtils.queryLowerThan(ScheduledJob.EXPIRATION_DATE, DateUtils.now().getTime(), true); query.put(ScheduledJob.RUNNING, true); final List<DBObject> expired = super.find(query); if (!expired.isEmpty()) { super.remove(query); } return expired; } /** * Returns all jobs that are not still running and should. * * @return List of jobs * @throws Exception */ public List<DBObject> getRunnable() throws Exception { final List<DBObject> result = new LinkedList<>(); final DBObject query = MongoUtils.queryLowerThan(ScheduledJob.RUN_NEXT_DATE, DateUtils.now().getTime(), true); query.put(ScheduledJob.RUNNING, false); final List<DBObject> jobs = super.find(query); // check max count for (final DBObject job : jobs) { final int max = ScheduledJob.getMaxRunCount(job); // 0=infinite final int count = ScheduledJob.getRunCount(job); // running=true, so next task will not find the job until stopped ScheduledJob.setRunning(job, true); // inc count if (max == 0 || max > count) { result.add(job); } else { // mark job as expired, so will be removed ScheduledJob.setExpirationDate(job, DateUtils.now().getTime()); } super.upsert(job); } return result; } // -------------------------------------------------------------------- // p r i v a t e // -------------------------------------------------------------------- // -------------------------------------------------------------------- // S T A T I C p r i v a t e // -------------------------------------------------------------------- // -------------------------------------------------------------------- // S T A T I C // -------------------------------------------------------------------- // error to log only once private static boolean _ERROR_GETRUNNABLE = false; private static Logger getStaticLogger() { return LoggingUtils.getLogger(ScheduledJobService.class); } public static DBObject get(final String id) { try { final ScheduledJobService srvc = new ScheduledJobService(); return srvc.findById(id); } catch (Throwable ignored) { } return null; } public static List<DBObject> getByType(final String userId, final String type) { try { final ScheduledJobService srvc = new ScheduledJobService(); final DBObject query = new BasicDBObject(); query.put(ScheduledJob.USER_ID, userId); query.put(ScheduledJob.TYPE, type); query.put(ScheduledJob.EXPIRED, false); return srvc.find(query); } catch (Throwable ignored) { } return new ArrayList<DBObject>(); } public static List<DBObject> getByUid(final String userId, final String uid) { try { final ScheduledJobService srvc = new ScheduledJobService(); final DBObject query = new BasicDBObject(); query.put(ScheduledJob.USER_ID, userId); query.put(ScheduledJob.UID, uid); query.put(ScheduledJob.EXPIRED, false); return srvc.find(query); } catch (Throwable ignored) { } return new ArrayList<DBObject>(); } public static List<DBObject> getTypeImport(final String userId) { return getByType(userId, IJobConstants.JOB_TYPE_IMPORT); } public static DBObject save(final DBObject item) { try { final ScheduledJobService srvc = new ScheduledJobService(); srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } public static DBObject ping(final String id) { try { final ScheduledJobService srvc = new ScheduledJobService(); final DBObject item = srvc.findById(id); ScheduledJob.ping(item); srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } public static DBObject incWorkDone(final String id) { try { final ScheduledJobService srvc = new ScheduledJobService(); final DBObject item = srvc.findById(id); ScheduledJob.ping(item); ScheduledJob.incWorkDone(item); srvc.upsert(item); return item; } catch (Throwable ignored) { } return null; } /** * Remove all timed out jobs and returns removed items * * @return Removed items */ public static List<DBObject> clean() { try { final ScheduledJobService srvc = new ScheduledJobService(); return srvc.removeExpired(); } catch (Throwable ignored) { } return null; } public static synchronized List<DBObject> runnable() { try { final ScheduledJobService srvc = new ScheduledJobService(); return srvc.getRunnable(); } catch (Throwable t) { if (!_ERROR_GETRUNNABLE) { _ERROR_GETRUNNABLE = true; CentralControl.onError(getStaticLogger(), FormatUtils.format("ERROR getting runnable tasks: ", t), t); } } return new ArrayList<>(); } }