use of com.birbit.android.jobqueue.messaging.message.PublicQueryMessage in project android-priority-jobqueue by yigit.
the class CallbackManager method start.
private void start() {
Thread callbackThread = new Thread(new Runnable() {
@Override
public void run() {
messageQueue.consume(new MessageQueueConsumer() {
long lastDelivery = Long.MIN_VALUE;
@Override
public void onStart() {
}
@Override
public void handleMessage(Message message) {
if (message.type == Type.CALLBACK) {
CallbackMessage cm = (CallbackMessage) message;
deliverMessage(cm);
lastDelivery = timer.nanoTime();
} else if (message.type == Type.CANCEL_RESULT_CALLBACK) {
deliverCancelResult((CancelResultMessage) message);
lastDelivery = timer.nanoTime();
} else if (message.type == Type.COMMAND) {
CommandMessage command = (CommandMessage) message;
final int what = command.getWhat();
if (what == CommandMessage.QUIT) {
messageQueue.stop();
started.set(false);
} else if (what == CommandMessage.RUNNABLE) {
command.getRunnable().run();
}
} else if (message.type == Type.PUBLIC_QUERY) {
((PublicQueryMessage) message).getCallback().onResult(0);
}
}
@Override
public void onIdle() {
}
});
}
}, "job-manager-callbacks");
try {
callbackThread.start();
} catch (InternalError error) {
// process is already dying, no reason to crash for this (and hide the real crash)
JqLog.e(error, "Cannot start a thread. Looks like app is shutting down." + "See issue #294 for details.");
}
}
use of com.birbit.android.jobqueue.messaging.message.PublicQueryMessage 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.PublicQueryMessage in project android-priority-jobqueue by yigit.
the class JobManager method stop.
/**
* Stops the JobManager. Currently running Jobs will continue to run but no new Jobs will be
* run until restarted.
*
* @see #start()
*/
public void stop() {
PublicQueryMessage message = messageFactory.obtain(PublicQueryMessage.class);
message.set(PublicQueryMessage.STOP, null);
messageQueue.post(message);
}
use of com.birbit.android.jobqueue.messaging.message.PublicQueryMessage in project android-priority-jobqueue by yigit.
the class JobManager method count.
/**
* Returns the number of jobs in the JobManager. This number does not include jobs that are
* currently running.
* <p>
* You cannot call this method on the main thread because it may potentially block it for a long
* time.
*
* @return The number of jobs that are waiting to be run
*/
public int count() {
assertNotInMainThread();
assertNotInJobManagerThread("Cannot call count sync method in JobManager's thread");
PublicQueryMessage message = messageFactory.obtain(PublicQueryMessage.class);
message.set(PublicQueryMessage.COUNT, null);
return new IntQueryFuture<>(messageQueue, message).getSafe();
}
use of com.birbit.android.jobqueue.messaging.message.PublicQueryMessage in project android-priority-jobqueue by yigit.
the class JobManager method getActiveConsumerCount.
/**
* Returns the number of consumer threads that are currently running Jobs. This number includes
* consumer threads that are currently idle.
* <p>
* You cannot call this method on the main thread because it may potentially block it for a long
* time.
* @return The number of consumer threads
*/
public int getActiveConsumerCount() {
assertNotInMainThread();
assertNotInJobManagerThread("Cannot call sync methods in JobManager's callback thread.");
PublicQueryMessage message = messageFactory.obtain(PublicQueryMessage.class);
message.set(PublicQueryMessage.ACTIVE_CONSUMER_COUNT, null);
return new IntQueryFuture<>(messageQueue, message).getSafe();
}
Aggregations