Search in sources :

Example 1 with IRegistrationHandle

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());
}
Also used : TypeParameterBeanRegistry(org.eclipse.scout.rt.platform.TypeParameterBeanRegistry) ArrayList(java.util.ArrayList) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) Test(org.junit.Test)

Example 2 with IRegistrationHandle

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());
    }
}
Also used : IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IFuture(org.eclipse.scout.rt.platform.job.IFuture)

Example 3 with IRegistrationHandle

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);
        }
    }
}
Also used : IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle)

Example 4 with IRegistrationHandle

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();
        }
    };
}
Also used : IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle)

Example 5 with IRegistrationHandle

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);
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IFuture(org.eclipse.scout.rt.platform.job.IFuture) IJobListener(org.eclipse.scout.rt.platform.job.listener.IJobListener) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) JobInput(org.eclipse.scout.rt.platform.job.JobInput) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IFilter(org.eclipse.scout.rt.platform.filter.IFilter) RunContext(org.eclipse.scout.rt.platform.context.RunContext) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) HashSet(java.util.HashSet)

Aggregations

IRegistrationHandle (org.eclipse.scout.rt.platform.util.IRegistrationHandle)8 HashSet (java.util.HashSet)2 IFuture (org.eclipse.scout.rt.platform.job.IFuture)2 IJobManager (org.eclipse.scout.rt.platform.job.IJobManager)2 IJobListener (org.eclipse.scout.rt.platform.job.listener.IJobListener)2 JobEvent (org.eclipse.scout.rt.platform.job.listener.JobEvent)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 TypeParameterBeanRegistry (org.eclipse.scout.rt.platform.TypeParameterBeanRegistry)1 RunContext (org.eclipse.scout.rt.platform.context.RunContext)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 IFilter (org.eclipse.scout.rt.platform.filter.IFilter)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 JobInput (org.eclipse.scout.rt.platform.job.JobInput)1 ThreadInfo (org.eclipse.scout.rt.platform.job.internal.NamedThreadFactory.ThreadInfo)1 JobEventData (org.eclipse.scout.rt.platform.job.listener.JobEventData)1 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)1 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)1 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)1