Skip to content

Commit 25baa28

Browse files
committed
Deprecate base job
1 parent 89d76ea commit 25baa28

File tree

20 files changed

+119
-177
lines changed

20 files changed

+119
-177
lines changed

examples/twitter/TwitterClient/src/com/path/android/jobqueue/examples/twitter/jobs/FetchTweetsJob.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.path.android.jobqueue.examples.twitter.jobs;
22

3-
import com.path.android.jobqueue.BaseJob;
43
import com.path.android.jobqueue.Job;
54
import com.path.android.jobqueue.Params;
65
import com.path.android.jobqueue.examples.twitter.controllers.TwitterController;

examples/twitter/TwitterClient/src/com/path/android/jobqueue/examples/twitter/jobs/PostTweetJob.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.path.android.jobqueue.examples.twitter.jobs;
22

3-
import com.path.android.jobqueue.BaseJob;
43
import com.path.android.jobqueue.Job;
54
import com.path.android.jobqueue.Params;
65
import com.path.android.jobqueue.examples.twitter.controllers.TwitterController;

jobqueue/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.path.android.jobqueue"
44
android:versionCode="2"
5-
android:versionName="1.1.2">
5+
android:versionName="1.2">
66
<application android:label="">
77
</application>
88
</manifest>

jobqueue/src/com/path/android/jobqueue/BaseJob.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Deprecated. Use {@link Job}
1414
*/
1515
@Deprecated
16-
abstract public class BaseJob implements Serializable {
16+
abstract class BaseJob implements Serializable {
1717
public static final int DEFAULT_RETRY_LIMIT = 20;
1818
private boolean requiresNetwork;
1919
private String groupId;

jobqueue/src/com/path/android/jobqueue/JobHolder.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,36 @@ public class JobHolder {
1919
protected long createdNs;
2020
protected long runningSessionId;
2121
protected boolean requiresNetwork;
22-
transient BaseJob baseJob;
22+
transient Job job;
2323

2424
/**
2525
* @param id Unique ID for the job. Should be unique per queue
2626
* @param priority Higher is better
2727
* @param groupId which group does this job belong to? default null
2828
* @param runCount Incremented each time job is fetched to run, initial value should be 0
29-
* @param baseJob Actual job to run
29+
* @param job Actual job to run
3030
* @param createdNs System.nanotime
3131
* @param delayUntilNs System.nanotime value where job can be run the very first time
3232
* @param runningSessionId
3333
*/
34-
public JobHolder(Long id, int priority, String groupId, int runCount, BaseJob baseJob, long createdNs, long delayUntilNs, long runningSessionId) {
34+
public JobHolder(Long id, int priority, String groupId, int runCount, Job job, long createdNs, long delayUntilNs, long runningSessionId) {
3535
this.id = id;
3636
this.priority = priority;
3737
this.groupId = groupId;
3838
this.runCount = runCount;
3939
this.createdNs = createdNs;
4040
this.delayUntilNs = delayUntilNs;
41-
this.baseJob = baseJob;
41+
this.job = job;
4242
this.runningSessionId = runningSessionId;
43-
this.requiresNetwork = baseJob.requiresNetwork();
43+
this.requiresNetwork = job.requiresNetwork();
4444
}
4545

46-
public JobHolder(int priority, BaseJob baseJob, long runningSessionId) {
47-
this(null, priority, null, 0, baseJob, System.nanoTime(), Long.MIN_VALUE, runningSessionId);
46+
public JobHolder(int priority, Job job, long runningSessionId) {
47+
this(null, priority, null, 0, job, System.nanoTime(), Long.MIN_VALUE, runningSessionId);
4848
}
4949

50-
public JobHolder(int priority, BaseJob baseJob, long delayUntilNs, long runningSessionId) {
51-
this(null, priority, baseJob.getRunGroupId(), 0, baseJob, System.nanoTime(), delayUntilNs, runningSessionId);
50+
public JobHolder(int priority, Job job, long delayUntilNs, long runningSessionId) {
51+
this(null, priority, job.getRunGroupId(), 0, job, System.nanoTime(), delayUntilNs, runningSessionId);
5252
}
5353

5454
/**
@@ -57,7 +57,7 @@ public JobHolder(int priority, BaseJob baseJob, long delayUntilNs, long runningS
5757
* @return
5858
*/
5959
public final boolean safeRun(int currentRunCount) {
60-
return baseJob.safeRun(currentRunCount);
60+
return job.safeRun(currentRunCount);
6161
}
6262

6363
public Long getId() {
@@ -108,12 +108,12 @@ public long getDelayUntilNs() {
108108
return delayUntilNs;
109109
}
110110

111-
public BaseJob getBaseJob() {
112-
return baseJob;
111+
public Job getJob() {
112+
return job;
113113
}
114114

115-
public void setBaseJob(BaseJob baseJob) {
116-
this.baseJob = baseJob;
115+
public void setJob(Job job) {
116+
this.job = job;
117117
}
118118

119119
public String getGroupId() {

jobqueue/src/com/path/android/jobqueue/JobManager.java

Lines changed: 54 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -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}

jobqueue/src/com/path/android/jobqueue/Params.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.path.android.jobqueue;
22

33
/**
4-
* BaseJob builder object to have a more readable design.
4+
* Job builder object to have a more readable design.
55
* Methods can be chained to have more readable code.
66
*/
77
public class Params {

jobqueue/src/com/path/android/jobqueue/config/Configuration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public Builder networkUtil(NetworkUtil networkUtil) {
139139

140140
/**
141141
* JobManager is suitable for DependencyInjection. Just provide your DependencyInjector and it will call it
142-
* before {BaseJob#onAdded} method is called.
142+
* before {Job#onAdded} method is called.
143143
* if job is persistent, it will also be called before run method.
144144
* @param injector your dependency injector interface, if using one
145145
* @return
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.path.android.jobqueue.di;
22

3-
import com.path.android.jobqueue.BaseJob;
3+
import com.path.android.jobqueue.Job;
44

55
/**
66
* interface that can be provided to {@link com.path.android.jobqueue.JobManager} for dependency injection
77
* it is called before the job's onAdded method is called. for persistent jobs, also run after job is brought
88
* back from disk.
99
*/
1010
public interface DependencyInjector {
11-
public void inject(BaseJob job);
11+
public void inject(Job job);
1212
}

jobqueue/src/com/path/android/jobqueue/executor/JobConsumerExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private void onAfterRun(JobHolder jobHolder) {
115115
}
116116

117117
private String createRunningJobHolderKey(JobHolder jobHolder) {
118-
return createRunningJobHolderKey(jobHolder.getId(), jobHolder.getBaseJob().isPersistent());
118+
return createRunningJobHolderKey(jobHolder.getId(), jobHolder.getJob().isPersistent());
119119
}
120120

121121
private String createRunningJobHolderKey(long id, boolean isPersistent) {

0 commit comments

Comments
 (0)