use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.
the class TypeParameterBeanRegistryTest method testStringHandler.
@Test
public void testStringHandler() {
final TypeParameterBeanRegistry<ITestHandler> registry = new TypeParameterBeanRegistry<>(ITestHandler.class);
final List<ITestHandler> l = new ArrayList<>();
l.add(new String1Handler());
l.add(new LongHandler());
l.add(new String2Handler());
l.add(new CharSequenceHandler());
IRegistrationHandle registration = registry.registerBeans(l);
final List<ITestHandler> stringResult = registry.getBeans(String.class);
assertEquals(3, stringResult.size());
assertEquals(l.get(0), stringResult.get(0));
assertEquals(l.get(2), stringResult.get(1));
assertEquals(l.get(3), stringResult.get(2));
List<ITestHandler> charSequenceResult = registry.getBeans(CharSequence.class);
assertEquals(1, charSequenceResult.size());
assertEquals(l.get(3), charSequenceResult.get(0));
registration.dispose();
assertEquals(0, registry.getBeans(String.class).size());
assertEquals(0, registry.getBeans(CharSequence.class).size());
}
use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.
the class AssertNoRunningJobsStatement method evaluate.
@Override
public void evaluate() throws Throwable {
final ScheduledDescendantJobListener jobListener = new ScheduledDescendantJobListener();
final IRegistrationHandle listenerRegistration = Jobs.getJobManager().addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.SCHEDULED).toFilter(), jobListener);
try {
// Continue the chain.
m_next.evaluate();
} finally {
listenerRegistration.dispose();
}
final Set<IFuture<?>> scheduledFutures = jobListener.getScheduledFutures();
if (!scheduledFutures.isEmpty()) {
assertNoRunningJobs(Jobs.newFutureFilterBuilder().andMatchFuture(scheduledFutures).toFilter());
}
}
use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.
the class TransactionProcessor method runTxRequiresNew.
/**
* Continues the chain in a new transaction, which upon completion is committed or rolled back.
*/
protected RESULT runTxRequiresNew(final Chain<RESULT> chain) throws Exception {
// Create and register the new transaction.
final ITransaction newTransaction = BEANS.get(ITransaction.class);
// Register the transaction members.
for (final ITransactionMember transactionMember : m_transactionMembers) {
newTransaction.registerMember(transactionMember);
}
final IRegistrationHandle currentTransactionRegistration = registerAsCurrentTransaction(newTransaction);
final IRegistrationHandle cancellationRegistration = registerTransactionForCancellation(newTransaction);
try {
try {
return chain.continueChain();
} catch (final Throwable t) {
// NOSONAR
// Register failure to rollback transaction.
newTransaction.addFailure(t);
throw BEANS.get(DefaultExceptionTranslator.class).translate(t);
} finally {
BEANS.get(ITransactionCommitProtocol.class).commitOrRollback(newTransaction);
}
} finally {
currentTransactionRegistration.dispose();
cancellationRegistration.dispose();
for (final ITransactionMember transactionMember : m_transactionMembers) {
newTransaction.unregisterMember(transactionMember);
}
}
}
use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.
the class BlockingTestUtility method addBlockingConditionTimeoutListener.
/**
* Used by {@link ClientTestRunner} to detect unexpected blocking conditions (forms)
* <p>
* Adds a blocking condition timeout listener on {@link IJobManager} for the current {@link IClientSession}
*
* @return the handle for the listener containing the first exception caught. Call
* {@link IRegistrationHandle#dispose()} to remove the listener.
*/
public static IBlockingConditionTimeoutHandle addBlockingConditionTimeoutListener(long timeout, TimeUnit unit) {
final BlockingConditionTimeoutListener listener = new BlockingConditionTimeoutListener(IClientSession.CURRENT.get(), timeout, unit);
final IRegistrationHandle handle = BEANS.get(IJobManager.class).addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).toFilter(), listener);
return new IBlockingConditionTimeoutHandle() {
@Override
public Exception getFirstException() {
return listener.m_firstException;
}
@Override
public void dispose() {
handle.dispose();
}
};
}
use of org.eclipse.scout.rt.platform.util.IRegistrationHandle in project scout.rt by eclipse.
the class BlockingTestUtility method runBlockingAction.
/**
* Helper method to test code which will enter a blocking condition.
* <p>
* If <code>runnableOnceBlocked</code> throws an exception, it is given to {@link JUnitExceptionHandler} to make the
* JUnit test fail.
*
* @param runnableGettingBlocked
* {@code IRunnable} that will enter a blocking condition.
* @param runnableOnceBlocked
* {@code IRunnable} to be executed once the 'runnableGettingBlocked' enters a blocking condition.
* @param awaitBackgroundJobs
* true waits for background jobs running in the same session to complete before runnableOnceBlocked is
* called
*/
public static void runBlockingAction(final IRunnable runnableGettingBlocked, final IRunnable runnableOnceBlocked, final boolean awaitBackgroundJobs) {
final ClientRunContext runContext = ClientRunContexts.copyCurrent();
final IBlockingCondition onceBlockedDoneCondition = Jobs.newBlockingCondition(true);
// remember the list of client jobs before blocking
final Set<IFuture<?>> jobsBefore = new HashSet<>();
jobsBefore.addAll(BEANS.get(IJobManager.class).getFutures(new IFilter<IFuture<?>>() {
@Override
public boolean accept(IFuture<?> cand) {
final RunContext candContext = cand.getJobInput().getRunContext();
return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession();
}
}));
final IRegistrationHandle listenerRegistration = IFuture.CURRENT.get().addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).andMatchExecutionHint(ModelJobs.EXECUTION_HINT_UI_INTERACTION_REQUIRED).toFilter(), new IJobListener() {
@Override
public void changed(final JobEvent event) {
// waitFor was entered
final IRunnable callRunnableOnceBlocked = new IRunnable() {
@Override
public void run() throws Exception {
try {
runnableOnceBlocked.run();
} finally {
event.getData().getBlockingCondition().setBlocking(false);
onceBlockedDoneCondition.setBlocking(false);
}
}
};
final JobInput jobInputForRunnableOnceBlocked = ModelJobs.newInput(runContext).withExceptionHandling(BEANS.get(JUnitExceptionHandler.class), true).withName("JUnit: Handling blocked thread because waiting for a blocking condition");
if (awaitBackgroundJobs) {
// wait until all background jobs finished
Jobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
jobsBefore.add(IFuture.CURRENT.get());
BEANS.get(IJobManager.class).awaitFinished(new IFilter<IFuture<?>>() {
@Override
public boolean accept(IFuture<?> f) {
RunContext candContext = f.getJobInput().getRunContext();
return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession() && !jobsBefore.contains(f);
}
}, 5, TimeUnit.MINUTES);
// call runnableOnceBlocked
ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
}
}, Jobs.newInput().withName("wait until background jobs finished"));
} else {
// call runnableOnceBlocked directly
ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
}
}
});
try {
// this action will enter a blocking condition which causes the 'runnableOnceBlocked' to be executed.
runnableGettingBlocked.run();
} catch (final Exception e) {
throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
} finally {
listenerRegistration.dispose();
}
// we need to wait until the runnableOnceBlocked is completed.
// runnableOnceBlocked may, during its execution, set the original blocking condition to non-blocking but still execute
// important code afterwards. Therefore, the original blocking condition that starts runnableOnceBlocked is only used
// to indicate the start of the runnableOnceBlocked, but this method returns only AFTER runnableOnceBlocked completes execution.
onceBlockedDoneCondition.waitForUninterruptibly(120, TimeUnit.SECONDS);
}
Aggregations