use of com.birbit.android.jobqueue.messaging.message.AddJobMessage 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.AddJobMessage in project android-priority-jobqueue by yigit.
the class JobManager method addJobInBackground.
/**
* Adds a Job to the JobManager. This method instantly returns and does not wait until the Job
* is added. You should always prefer this method over {@link #addJob(Job)}.
*
* @param job The Job to be added
*
* @see #addJobInBackground(Job, AsyncAddCallback)
* @see #addJob(Job)
*/
public void addJobInBackground(Job job) {
AddJobMessage message = messageFactory.obtain(AddJobMessage.class);
message.setJob(job);
messageQueue.post(message);
}
use of com.birbit.android.jobqueue.messaging.message.AddJobMessage in project android-priority-jobqueue by yigit.
the class PriorityMessageQueueTest method test1.
@Test
public void test1() {
CommandMessage mC1 = new CommandMessage();
CommandMessage mC2 = new CommandMessage();
AddJobMessage aj1 = new AddJobMessage();
AddJobMessage aj2 = new AddJobMessage();
mq.post(mC1);
mq.post(mC2);
mq.post(aj1);
mq.post(aj2);
final List<Message> expectedOrder = Arrays.asList(aj1, aj2, mC1, mC2);
mq.consume(new MessageQueueConsumer() {
int index;
@Override
public void handleMessage(Message message) {
assertThat(message, is(expectedOrder.get(index++)));
if (index == expectedOrder.size()) {
mq.stop();
}
}
@Override
public void onIdle() {
}
});
}
use of com.birbit.android.jobqueue.messaging.message.AddJobMessage in project android-priority-jobqueue by yigit.
the class MessageFactoryTest method test.
@Test
public void test() {
AddJobMessage aj1 = factory.obtain(AddJobMessage.class);
assertThat(aj1, isA(AddJobMessage.class));
CommandMessage cm1 = factory.obtain(CommandMessage.class);
assertThat(cm1, isA(CommandMessage.class));
assertThat(factory.obtain(AddJobMessage.class), not(sameInstance(aj1)));
assertThat(factory.obtain(CommandMessage.class), not(sameInstance(cm1)));
factory.release(aj1);
factory.release(cm1);
assertThat(factory.obtain(AddJobMessage.class), sameInstance(aj1));
assertThat(factory.obtain(CommandMessage.class), sameInstance(cm1));
assertThat(factory.obtain(AddJobMessage.class), not(sameInstance(aj1)));
assertThat(factory.obtain(CommandMessage.class), not(sameInstance(cm1)));
}
Aggregations