use of com.birbit.android.jobqueue.messaging.message.RunJobResultMessage in project android-priority-jobqueue by yigit.
the class JobManagerThread method run.
@Override
public void run() {
messageQueue.consume(new MessageQueueConsumer() {
@Override
public void handleMessage(Message message) {
canScheduleConstraintChangeOnIdle = true;
switch(message.type) {
case ADD_JOB:
handleAddJob((AddJobMessage) message);
break;
case JOB_CONSUMER_IDLE:
boolean busy = consumerManager.handleIdle((JobConsumerIdleMessage) message);
if (!busy) {
invokeSchedulersIfIdle();
}
break;
case RUN_JOB_RESULT:
handleRunJobResult((RunJobResultMessage) message);
break;
case CONSTRAINT_CHANGE:
boolean handled = consumerManager.handleConstraintChange();
ConstraintChangeMessage constraintChangeMessage = (ConstraintChangeMessage) message;
canScheduleConstraintChangeOnIdle = handled || !constraintChangeMessage.isForNextJob();
break;
case CANCEL:
handleCancel((CancelMessage) message);
break;
case PUBLIC_QUERY:
handlePublicQuery((PublicQueryMessage) message);
break;
case COMMAND:
handleCommand((CommandMessage) message);
break;
case SCHEDULER:
handleSchedulerMessage((SchedulerMessage) message);
break;
}
}
@Override
public void onIdle() {
JqLog.v("joq idle. running:? %s", running);
if (!running) {
return;
}
if (!canScheduleConstraintChangeOnIdle) {
JqLog.v("skipping scheduling a new idle callback because looks like last one" + " did not do anything");
return;
}
Long nextJobTimeNs = getNextWakeUpNs(true);
// TODO check network should be another message which goes idle if network is the
// same as now
JqLog.d("Job queue idle. next job at: %s", nextJobTimeNs);
if (nextJobTimeNs != null) {
ConstraintChangeMessage constraintMessage = messageFactory.obtain(ConstraintChangeMessage.class);
constraintMessage.setForNextJob(true);
messageQueue.postAt(constraintMessage, nextJobTimeNs);
} else if (scheduler != null) {
// if we have a scheduler but the queue is empty, just clean them all.
if (shouldCancelAllScheduledWhenEmpty && persistentJobQueue.count() == 0) {
shouldCancelAllScheduledWhenEmpty = false;
scheduler.cancelAll();
}
}
}
});
}
use of com.birbit.android.jobqueue.messaging.message.RunJobResultMessage in project android-priority-jobqueue by yigit.
the class ConsumerTest method runJobMessage.
@Test
public void runJobMessage() {
MessageFactory factory = new MessageFactory();
PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
setRunning(pmq);
SafeMessageQueue mq = new SafeMessageQueue(timer, factory, "test");
setRunning(mq);
timer.setNow(2001);
Consumer consumer = new Consumer(pmq, mq, factory, timer);
RunJobMessage rjm = factory.obtain(RunJobMessage.class);
JobHolder mockHolder = mock(JobHolder.class);
when(mockHolder.safeRun(0, timer)).thenReturn(JobHolder.RUN_RESULT_SUCCESS);
rjm.setJobHolder(mockHolder);
timer.setNow(3001);
consumer.queueConsumer.handleMessage(rjm);
Message message = pmq.next(dummyConsumer);
assertThat(message, CoreMatchers.instanceOf(RunJobResultMessage.class));
RunJobResultMessage result = (RunJobResultMessage) message;
assertThat(result.getResult(), CoreMatchers.is(JobHolder.RUN_RESULT_SUCCESS));
assertThat(result.getJobHolder(), CoreMatchers.is(mockHolder));
assertThat(consumer.lastJobCompleted, CoreMatchers.is(3001L));
}
Aggregations