Search in sources :

Example 1 with Message

use of com.birbit.android.jobqueue.messaging.Message 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.");
    }
}
Also used : CancelResultMessage(com.birbit.android.jobqueue.messaging.message.CancelResultMessage) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) CallbackMessage(com.birbit.android.jobqueue.messaging.message.CallbackMessage) Message(com.birbit.android.jobqueue.messaging.Message) CallbackMessage(com.birbit.android.jobqueue.messaging.message.CallbackMessage) PublicQueryMessage(com.birbit.android.jobqueue.messaging.message.PublicQueryMessage) CancelResultMessage(com.birbit.android.jobqueue.messaging.message.CancelResultMessage) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) CommandMessage(com.birbit.android.jobqueue.messaging.message.CommandMessage)

Example 2 with Message

use of com.birbit.android.jobqueue.messaging.Message 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 3 with Message

use of com.birbit.android.jobqueue.messaging.Message 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));
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) JobHolder(com.birbit.android.jobqueue.JobHolder) MessageFactory(com.birbit.android.jobqueue.messaging.MessageFactory) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) 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) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) RunJobResultMessage(com.birbit.android.jobqueue.messaging.message.RunJobResultMessage) Test(org.junit.Test)

Example 4 with Message

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

the class ConsumerTest method idleMessage.

@Test
public void idleMessage() {
    MessageFactory factory = new MessageFactory();
    PriorityMessageQueue pmq = new PriorityMessageQueue(timer, factory);
    SafeMessageQueue mq = new SafeMessageQueue(timer, factory, "test");
    setRunning(pmq);
    setRunning(mq);
    timer.setNow(2001);
    Consumer consumer = new Consumer(pmq, mq, factory, timer);
    consumer.queueConsumer.onIdle();
    Message message = pmq.next(dummyConsumer);
    assertThat(message, CoreMatchers.instanceOf(JobConsumerIdleMessage.class));
    assertThat(((JobConsumerIdleMessage) message).getLastJobCompleted(), CoreMatchers.is(2001L));
}
Also used : PriorityMessageQueue(com.birbit.android.jobqueue.messaging.PriorityMessageQueue) MessageFactory(com.birbit.android.jobqueue.messaging.MessageFactory) MessageQueueConsumer(com.birbit.android.jobqueue.messaging.MessageQueueConsumer) Consumer(com.birbit.android.jobqueue.ConsumerManager.Consumer) 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) RunJobMessage(com.birbit.android.jobqueue.messaging.message.RunJobMessage) SafeMessageQueue(com.birbit.android.jobqueue.messaging.SafeMessageQueue) JobConsumerIdleMessage(com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage) Test(org.junit.Test)

Aggregations

Message (com.birbit.android.jobqueue.messaging.Message)4 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)4 CommandMessage (com.birbit.android.jobqueue.messaging.message.CommandMessage)4 JobConsumerIdleMessage (com.birbit.android.jobqueue.messaging.message.JobConsumerIdleMessage)3 RunJobResultMessage (com.birbit.android.jobqueue.messaging.message.RunJobResultMessage)3 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)2 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)2 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)2 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)2 PublicQueryMessage (com.birbit.android.jobqueue.messaging.message.PublicQueryMessage)2 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)2 Test (org.junit.Test)2 JobHolder (com.birbit.android.jobqueue.JobHolder)1 AddJobMessage (com.birbit.android.jobqueue.messaging.message.AddJobMessage)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 SchedulerMessage (com.birbit.android.jobqueue.messaging.message.SchedulerMessage)1