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));
}
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();
}
};
}
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());
}
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());
}
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());
}
Aggregations