Search in sources :

Example 11 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobFutureTask method changeState.

/**
 * Sets the new state, and fires the given event, unless already being in state {@link JobState#DONE} or
 * {@link JobState#REJECTED}, or the specified state is already the current state.
 * <p>
 * The caller is responsible for setting {@link JobEventData#getState()} and {@link JobEventData#getFuture()}
 * accordingly.
 */
protected synchronized void changeState(final JobEventData eventData) {
    Assertions.assertNotNull(eventData.getState(), "missing state");
    Assertions.assertSame(this, eventData.getFuture(), "wrong future [expected={}]", this);
    // Do nothing if equals to current state.
    if (m_state == eventData.getState()) {
        return;
    }
    // Do nothing if already in done or rejected state.
    if (m_state == JobState.DONE || m_state == JobState.REJECTED) {
        return;
    }
    m_state = eventData.getState();
    m_jobManager.fireEvent(new JobEvent(m_jobManager, JobEventType.JOB_STATE_CHANGED, eventData));
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent)

Example 12 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class DevelopmentThreadNameDecorator method decorate.

@Override
public IUndecorator decorate() throws Exception {
    final ThreadInfo threadInfo = ThreadInfo.CURRENT.get();
    final IFuture<?> future = IFuture.CURRENT.get();
    // Update the thread name.
    threadInfo.updateThreadName(future.getJobInput().getThreadName(), buildExecutionInfo(future));
    // Decorate the thread name upon job state change.
    final IRegistrationHandle listenerRegistration = future.addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.RUNNING, JobState.WAITING_FOR_PERMIT, JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter(), new IJobListener() {

        @Override
        public void changed(final JobEvent event) {
            threadInfo.updateThreadName(future.getJobInput().getThreadName(), buildExecutionInfo(future));
        }
    });
    return new IUndecorator() {

        @Override
        public void undecorate() {
            // Restore to the original thread name.
            listenerRegistration.dispose();
            threadInfo.reset();
        }
    };
}
Also used : ThreadInfo(org.eclipse.scout.rt.platform.job.internal.NamedThreadFactory.ThreadInfo) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IJobListener(org.eclipse.scout.rt.platform.job.listener.IJobListener)

Example 13 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobStateTest method testScheduleAtFixedRate.

@Test
public void testScheduleAtFixedRate() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final AtomicInteger rounds = new AtomicInteger(0);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            if (rounds.incrementAndGet() == 3) {
                IFuture.CURRENT.get().cancel(false);
            }
        }
    }, Jobs.newInput().withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.MILLISECONDS).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(1).repeatForever())));
    future.awaitDone(5, TimeUnit.SECONDS);
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // first round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // second round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    // third round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 14 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobStateTest method testScheduleAtFixedRateAndMutex.

@Test
public void testScheduleAtFixedRateAndMutex() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final AtomicInteger rounds = new AtomicInteger(0);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            if (rounds.incrementAndGet() == 3) {
                IFuture.CURRENT.get().cancel(false);
            }
        }
    }, Jobs.newInput().withExecutionSemaphore(Jobs.newExecutionSemaphore(1)).withExecutionTrigger(Jobs.newExecutionTrigger().withStartIn(1, TimeUnit.MILLISECONDS).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(1).repeatForever())));
    future.awaitDone(5, TimeUnit.SECONDS);
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    // first round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    // second round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.PENDING, capturedEvents.get(i));
    assertEquals(JobState.PENDING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.WAITING_FOR_PERMIT, capturedEvents.get(i));
    assertEquals(JobState.WAITING_FOR_PERMIT, capturedFutureStates.get(i));
    // third round
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Example 15 with JobEvent

use of org.eclipse.scout.rt.platform.job.listener.JobEvent in project scout.rt by eclipse.

the class JobStateTest method testRunningAndDone.

@Test
public void testRunningAndDone() throws ThreadInterruptedError, java.lang.InterruptedException {
    JobEventCaptureListener captureListener = new JobEventCaptureListener();
    Jobs.getJobManager().addListener(captureListener);
    final BlockingCountDownLatch runningLatch = new BlockingCountDownLatch(1);
    IFuture<Void> future = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            runningLatch.countDownAndBlock();
        }
    }, Jobs.newInput());
    assertTrue(runningLatch.await());
    assertEquals(JobState.RUNNING, future.getState());
    runningLatch.unblock();
    future.awaitDone(5, TimeUnit.SECONDS);
    assertEquals(JobState.DONE, future.getState());
    // verify events
    int i = -1;
    List<JobEvent> capturedEvents = captureListener.getCapturedEvents();
    List<JobState> capturedFutureStates = captureListener.getCapturedFutureStates();
    i++;
    assertStateChangedEvent(future, JobState.SCHEDULED, capturedEvents.get(i));
    assertEquals(JobState.SCHEDULED, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.RUNNING, capturedEvents.get(i));
    assertEquals(JobState.RUNNING, capturedFutureStates.get(i));
    i++;
    assertStateChangedEvent(future, JobState.DONE, capturedEvents.get(i));
    assertEquals(JobState.DONE, capturedFutureStates.get(i));
    assertEquals(i + 1, capturedEvents.size());
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Test(org.junit.Test)

Aggregations

JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)27 Test (org.junit.Test)23 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)19 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)7 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)6 JobEventData (org.eclipse.scout.rt.platform.job.listener.JobEventData)6 IClientSession (org.eclipse.scout.rt.client.IClientSession)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Times (org.eclipse.scout.rt.testing.platform.runner.Times)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)2 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 JobInput (org.eclipse.scout.rt.platform.job.JobInput)2 JobState (org.eclipse.scout.rt.platform.job.JobState)2 IJobListener (org.eclipse.scout.rt.platform.job.listener.IJobListener)2 IRegistrationHandle (org.eclipse.scout.rt.platform.util.IRegistrationHandle)2 ISession (org.eclipse.scout.rt.shared.ISession)2 SessionJobEventFilter (org.eclipse.scout.rt.shared.job.filter.event.SessionJobEventFilter)2 HashSet (java.util.HashSet)1