@@ -144,7 +144,42 @@ private int countReadyJobs(boolean hasNetwork) {
144144 */
145145 public long addJob (Job job ) {
146146 //noinspection deprecation
147- return addJob (job .getPriority (), job .getDelayInMs (), job );
147+ JobHolder jobHolder = new JobHolder (job .getPriority (), job
148+ , job .getDelayInMs () > 0 ? System .nanoTime () + job .getDelayInMs () * NS_PER_MS : NOT_DELAYED_JOB_DELAY
149+ , NOT_RUNNING_SESSION_ID );
150+ long id ;
151+ if (job .isPersistent ()) {
152+ synchronized (persistentJobQueue ) {
153+ id = persistentJobQueue .insert (jobHolder );
154+ addOnAddedLock (persistentOnAddedLocks , id );
155+ }
156+ } else {
157+ synchronized (nonPersistentJobQueue ) {
158+ id = nonPersistentJobQueue .insert (jobHolder );
159+ addOnAddedLock (nonPersistentOnAddedLocks , id );
160+ }
161+ }
162+ if (JqLog .isDebugEnabled ()) {
163+ JqLog .d ("added job id: %d class: %s priority: %d delay: %d group : %s persistent: %s requires network: %s"
164+ , id , job .getClass ().getSimpleName (), job .getPriority (), job .getDelayInMs (), job .getRunGroupId ()
165+ , job .isPersistent (), job .requiresNetwork ());
166+ }
167+ if (dependencyInjector != null ) {
168+ //inject members b4 calling onAdded
169+ dependencyInjector .inject (job );
170+ }
171+ jobHolder .getJob ().onAdded ();
172+ if (job .isPersistent ()) {
173+ synchronized (persistentJobQueue ) {
174+ clearOnAddedLock (persistentOnAddedLocks , id );
175+ }
176+ } else {
177+ synchronized (nonPersistentJobQueue ) {
178+ clearOnAddedLock (nonPersistentOnAddedLocks , id );
179+ }
180+ }
181+ notifyJobConsumer ();
182+ return id ;
148183 }
149184
150185 /**
@@ -155,11 +190,23 @@ public long addJob(Job job) {
155190 */
156191 public void addJobInBackground (Job job ) {
157192 //noinspection deprecation
158- addJobInBackground (job . getPriority (), job . getDelayInMs (), job );
193+ addJobInBackground (job , null );
159194 }
160195
161- public void addJobInBackground (Job job , /*nullable*/ AsyncAddCallback callback ) {
162- addJobInBackground (job .getPriority (), job .getDelayInMs (), job , callback );
196+ public void addJobInBackground (final Job job , /*nullable*/ final AsyncAddCallback callback ) {
197+ timedExecutor .execute (new Runnable () {
198+ @ Override
199+ public void run () {
200+ try {
201+ long id = addJob (job );
202+ if (callback != null ) {
203+ callback .onAdded (id );
204+ }
205+ } catch (Throwable t ) {
206+ JqLog .e (t , "addJobInBackground received an exception. job class: %s" , job .getClass ().getSimpleName ());
207+ }
208+ }
209+ });
163210 }
164211
165212 //need to sync on related job queue before calling this
@@ -278,7 +325,7 @@ private JobHolder getNextJob() {
278325 return null ;
279326 }
280327 if (persistent && dependencyInjector != null ) {
281- dependencyInjector .inject (jobHolder .getBaseJob ());
328+ dependencyInjector .inject (jobHolder .getJob ());
282329 }
283330 if (jobHolder .getGroupId () != null ) {
284331 runningJobGroups .add (jobHolder .getGroupId ());
@@ -297,7 +344,7 @@ private JobHolder getNextJob() {
297344
298345 private void reAddJob (JobHolder jobHolder ) {
299346 JqLog .d ("re-adding job %s" , jobHolder .getId ());
300- if (jobHolder .getBaseJob ().isPersistent ()) {
347+ if (jobHolder .getJob ().isPersistent ()) {
301348 synchronized (persistentJobQueue ) {
302349 persistentJobQueue .insertOrReplace (jobHolder );
303350 }
@@ -354,7 +401,7 @@ public JobStatus getJobStatus(long id, boolean isPersistent) {
354401 }
355402
356403 private void removeJob (JobHolder jobHolder ) {
357- if (jobHolder .getBaseJob ().isPersistent ()) {
404+ if (jobHolder .getJob ().isPersistent ()) {
358405 synchronized (persistentJobQueue ) {
359406 persistentJobQueue .remove (jobHolder );
360407 }
@@ -468,111 +515,6 @@ public int countRemainingReadyJobs() {
468515 }
469516 };
470517
471- /**
472- * Deprecated, please use {@link #addJob(Job)}.
473- *
474- * <p>Adds a job with given priority and returns the JobId.</p>
475- * @param priority Higher runs first
476- * @param baseJob The actual job to run
477- * @return job id
478- */
479- @ Deprecated
480- public long addJob (int priority , BaseJob baseJob ) {
481- return addJob (priority , 0 , baseJob );
482- }
483-
484- /**
485- * Deprecated, please use {@link #addJob(Job)}.
486- *
487- * <p>Adds a job with given priority and returns the JobId.</p>
488- * @param priority Higher runs first
489- * @param delay number of milliseconds that this job should be delayed
490- * @param baseJob The actual job to run
491- * @return a job id. is useless for now but we'll use this to cancel jobs in the future.
492- */
493- @ Deprecated
494- public long addJob (int priority , long delay , BaseJob baseJob ) {
495- JobHolder jobHolder = new JobHolder (priority , baseJob , delay > 0 ? System .nanoTime () + delay * NS_PER_MS : NOT_DELAYED_JOB_DELAY , NOT_RUNNING_SESSION_ID );
496- long id ;
497- if (baseJob .isPersistent ()) {
498- synchronized (persistentJobQueue ) {
499- id = persistentJobQueue .insert (jobHolder );
500- addOnAddedLock (persistentOnAddedLocks , id );
501- }
502- } else {
503- synchronized (nonPersistentJobQueue ) {
504- id = nonPersistentJobQueue .insert (jobHolder );
505- addOnAddedLock (nonPersistentOnAddedLocks , id );
506- }
507- }
508- if (JqLog .isDebugEnabled ()) {
509- JqLog .d ("added job id: %d class: %s priority: %d delay: %d group : %s persistent: %s requires network: %s"
510- , id , baseJob .getClass ().getSimpleName (), priority , delay , baseJob .getRunGroupId ()
511- , baseJob .isPersistent (), baseJob .requiresNetwork ());
512- }
513- if (dependencyInjector != null ) {
514- //inject members b4 calling onAdded
515- dependencyInjector .inject (baseJob );
516- }
517- jobHolder .getBaseJob ().onAdded ();
518- if (baseJob .isPersistent ()) {
519- synchronized (persistentJobQueue ) {
520- clearOnAddedLock (persistentOnAddedLocks , id );
521- }
522- } else {
523- synchronized (nonPersistentJobQueue ) {
524- clearOnAddedLock (nonPersistentOnAddedLocks , id );
525- }
526- }
527- notifyJobConsumer ();
528- return id ;
529- }
530-
531- /**
532- * Please use {@link #addJobInBackground(Job)}.
533- * <p>Non-blocking convenience method to add a job in background thread.</p>
534- *
535- * @see #addJob(int, BaseJob) addJob(priority, job).
536- */
537- @ Deprecated
538- public void addJobInBackground (final int priority , final BaseJob baseJob ) {
539- timedExecutor .execute (new Runnable () {
540- @ Override
541- public void run () {
542- addJob (priority , baseJob );
543- }
544- });
545- }
546-
547- /**
548- * Deprecated, please use {@link #addJobInBackground(Job)}.
549- * <p></p>Non-blocking convenience method to add a job in background thread.</p>
550- * @see #addJob(int, long, BaseJob) addJob(priority, delay, job).
551- */
552- @ Deprecated
553- public void addJobInBackground (final int priority , final long delay , final BaseJob baseJob ) {
554- addJobInBackground (priority , delay , baseJob , null );
555- }
556-
557- protected void addJobInBackground (final int priority , final long delay , final BaseJob baseJob ,
558- /*nullable*/ final AsyncAddCallback callback ) {
559- final long callTime = System .nanoTime ();
560- timedExecutor .execute (new Runnable () {
561- @ Override
562- public void run () {
563- try {
564- final long runDelay = (System .nanoTime () - callTime ) / NS_PER_MS ;
565- long id = addJob (priority , Math .max (0 , delay - runDelay ), baseJob );
566- if (callback != null ) {
567- callback .onAdded (id );
568- }
569- } catch (Throwable t ) {
570- JqLog .e (t , "addJobInBackground received an exception. job class: %s" , baseJob .getClass ().getSimpleName () );
571- }
572- }
573- });
574- }
575-
576518
577519 /**
578520 * Default implementation of QueueFactory that creates one {@link SqliteJobQueue} and one {@link NonPersistentPriorityQueue}
0 commit comments