Search in sources :

Example 6 with CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage 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();
                }
            }
        }
    });
}
Also used : SchedulerMessage(com.birbit.android.jobqueue.messaging.message.SchedulerMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) SchedulerMessage(com.birbit.android.jobqueue.messaging.message.SchedulerMessage) AddJobMessage(com.birbit.android.jobqueue.messaging.message.AddJobMessage) Message(com.birbit.android.jobqueue.messaging.Message) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) CancelMessage(com.birbit.android.jobqueue.messaging.message.CancelMessage) ConstraintChangeMessage(com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage) CancelMessage(com.birbit.android.jobqueue.messaging.message.CancelMessage) AddJobMessage(com.birbit.android.jobqueue.messaging.message.AddJobMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) ConstraintChangeMessage(com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 7 with CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage in project android-priority-jobqueue by yigit.

the class DelayedMessageBagRecycleTest method recycleOnClear.

@Test
public void recycleOnClear() {
    CommandMessage cm = factory.obtain(CommandMessage.class);
    cm.set(CommandMessage.POKE);
    bag.add(cm, 1000);
    bag.clear();
    verify(factory).release(cm);
}
Also used : CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Example 8 with CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage in project android-priority-jobqueue by yigit.

the class DelayedMessageBagRecycleTest method recycleOnCancel.

@Test
public void recycleOnCancel() {
    final CommandMessage cm = factory.obtain(CommandMessage.class);
    cm.set(CommandMessage.POKE);
    bag.add(cm, 1000);
    final CommandMessage cm2 = factory.obtain(CommandMessage.class);
    cm2.set(CommandMessage.POKE);
    bag.add(cm2, 1000);
    bag.removeMessages(new MessagePredicate() {

        @Override
        public boolean onMessage(Message message) {
            return message == cm;
        }
    });
    verify(factory).release(cm);
    verify(factory, times(0)).release(cm2);
}
Also used : CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) Test(org.junit.Test)

Example 9 with CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage in project android-priority-jobqueue by yigit.

the class DelayedMessageBagRemoveTest method add.

private Message add(long readyNs) {
    CommandMessage msg = factory.obtain(CommandMessage.class);
    bag.add(msg, readyNs);
    added.put(readyNs, msg);
    return msg;
}
Also used : CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 10 with CommandMessage

use of com.birbit.android.jobqueue.messaging.message.CommandMessage in project android-priority-jobqueue by yigit.

the class JobManager method destroy.

/**
     * Destroys the JobManager. You cannot make any calls to this JobManager after this call.
     * Useful to be called after your tests.
     *
     * @see #stopAndWaitUntilConsumersAreFinished()
     */
public void destroy() {
    JqLog.d("destroying job queue");
    stopAndWaitUntilConsumersAreFinished();
    CommandMessage message = messageFactory.obtain(CommandMessage.class);
    message.set(CommandMessage.QUIT);
    messageQueue.post(message);
    jobManagerThread.callbackManager.destroy();
}
Also used : CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Aggregations

CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)24 Test (org.junit.Test)12 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)6 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 AddJobMessage (com.birbit.android.jobqueue.messaging.message.AddJobMessage)3 Message (com.birbit.android.jobqueue.messaging.Message)2 PublicQueryMessage (com.birbit.android.jobqueue.messaging.message.PublicQueryMessage)2 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)1 CallbackMessage (com.birbit.android.jobqueue.messaging.message.CallbackMessage)1 CancelMessage (com.birbit.android.jobqueue.messaging.message.CancelMessage)1 CancelResultMessage (com.birbit.android.jobqueue.messaging.message.CancelResultMessage)1 ConstraintChangeMessage (com.birbit.android.jobqueue.messaging.message.ConstraintChangeMessage)1 JobConsumerIdleMessage (com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage)1 RunJobResultMessage (com.birbit.android.jobqueue.messaging.message.RunJobResultMessage)1 SchedulerMessage (com.birbit.android.jobqueue.messaging.message.SchedulerMessage)1 SchedulerConstraint (com.birbit.android.jobqueue.scheduling.SchedulerConstraint)1 AssertionFailedError (junit.framework.AssertionFailedError)1