Search in sources :

Example 1 with IntegerAction

use of org.mule.runtime.api.notification.IntegerAction in project mule by mulesoft.

the class ServerNotificationsTestCase method onNotification.

@Override
public void onNotification(Notification notification) {
    if (new IntegerAction(CONTEXT_STOPPED).equals(notification.getAction())) {
        managerStopped.set(true);
        managerStoppedEvents.incrementAndGet();
    }
}
Also used : IntegerAction(org.mule.runtime.api.notification.IntegerAction)

Example 2 with IntegerAction

use of org.mule.runtime.api.notification.IntegerAction in project mule by mulesoft.

the class TransactionNotificationsTestCase method testTransactionNotifications.

@Test
public void testTransactionNotifications() throws Exception {
    final CountDownLatch latch = new CountDownLatch(3);
    // the code is simple and deceptive :) The trick is this dummy transaction is handled by
    // a global TransactionCoordination instance, which binds it to the current thread.
    Transaction transaction = new DummyTransaction(muleContext);
    ((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationListenerRegistry.class).registerListener(new TransactionNotificationListener<TransactionNotification>() {

        @Override
        public boolean isBlocking() {
            return false;
        }

        @Override
        public void onNotification(TransactionNotification notification) {
            if (new IntegerAction(TRANSACTION_BEGAN).equals(notification.getAction())) {
                assertEquals("begin", notification.getActionName());
                latch.countDown();
            } else if (new IntegerAction(TRANSACTION_COMMITTED).equals(notification.getAction())) {
                assertEquals("commit", notification.getActionName());
                latch.countDown();
            } else if (new IntegerAction(TRANSACTION_ROLLEDBACK).equals(notification.getAction())) {
                assertEquals("rollback", notification.getActionName());
                latch.countDown();
            }
        }
    }, notification -> transaction.getId().equals(notification.getResourceIdentifier()));
    transaction.begin();
    transaction.commit();
    transaction.rollback();
    // Wait for the notifcation event to be fired as they are queued
    latch.await(2000, MILLISECONDS);
    assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification) IntegerAction(org.mule.runtime.api.notification.IntegerAction) AbstractSingleResourceTransaction(org.mule.runtime.core.privileged.transaction.AbstractSingleResourceTransaction) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with IntegerAction

use of org.mule.runtime.api.notification.IntegerAction in project mule by mulesoft.

the class AbstractProcessingStrategyTestCase method testAsyncCpuLightNotificationThreads.

protected void testAsyncCpuLightNotificationThreads(AtomicReference<Thread> beforeThread, AtomicReference<Thread> afterThread) throws Exception {
    muleContext.getNotificationManager().addInterfaceToType(MessageProcessorNotificationListener.class, MessageProcessorNotification.class);
    muleContext.getNotificationManager().addListener((MessageProcessorNotificationListener) notification -> {
        if (new IntegerAction(MESSAGE_PROCESSOR_PRE_INVOKE).equals(notification.getAction())) {
            beforeThread.set(currentThread());
        } else if (new IntegerAction(MESSAGE_PROCESSOR_POST_INVOKE).equals(notification.getAction())) {
            afterThread.set(currentThread());
        }
    });
    flow = flowBuilder.get().processors(annotatedAsyncProcessor).build();
    flow.initialise();
    flow.start();
    processFlow(testEvent());
}
Also used : Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) Thread.currentThread(java.lang.Thread.currentThread) IO_RW(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType.IO_RW) DEFAULT_TIMEOUT(org.mule.tck.probe.PollingProber.DEFAULT_TIMEOUT) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Future(java.util.concurrent.Future) LifecycleUtils.setMuleContextIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.setMuleContextIfNeeded) Arrays.asList(java.util.Arrays.asList) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) NamedThreadFactory(org.mule.runtime.core.api.util.concurrent.NamedThreadFactory) SOURCE(org.mule.runtime.core.internal.processor.strategy.AbstractProcessingStrategyTestCase.Mode.SOURCE) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Operators.requestUnbounded(org.mule.runtime.core.internal.util.rx.Operators.requestUnbounded) Builder(org.mule.runtime.core.api.construct.Flow.Builder) CountDownLatch(java.util.concurrent.CountDownLatch) Matchers.contains(org.hamcrest.Matchers.contains) DEFAULT_POLLING_INTERVAL(org.mule.tck.probe.PollingProber.DEFAULT_POLLING_INTERVAL) TriggerableMessageSource(org.mule.tck.TriggerableMessageSource) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) Supplier(java.util.function.Supplier) MESSAGE_PROCESSOR_POST_INVOKE(org.mule.runtime.api.notification.MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE) ArrayList(java.util.ArrayList) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleException(org.mule.runtime.api.exception.MuleException) Before(org.junit.Before) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MessageProcessorNotificationListener(org.mule.runtime.api.notification.MessageProcessorNotificationListener) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) IntegerAction(org.mule.runtime.api.notification.IntegerAction) Matcher(org.hamcrest.Matcher) BackPressureStrategy(org.mule.runtime.core.api.source.MessageSource.BackPressureStrategy) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Flow.builder(org.mule.runtime.core.api.construct.Flow.builder) ScheduledFuture(java.util.concurrent.ScheduledFuture) Collections.synchronizedSet(java.util.Collections.synchronizedSet) PollingProber(org.mule.tck.probe.PollingProber) Assert.assertThat(org.junit.Assert.assertThat) PROCESSOR_SCHEDULER_CONTEXT_KEY(org.mule.runtime.core.internal.processor.strategy.AbstractProcessingStrategy.PROCESSOR_SCHEDULER_CONTEXT_KEY) Scheduler(org.mule.runtime.api.scheduler.Scheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Mono.just(reactor.core.publisher.Mono.just) Thread.sleep(java.lang.Thread.sleep) Exceptions.bubble(reactor.core.Exceptions.bubble) Parameterized(org.junit.runners.Parameterized) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) TimeZone(java.util.TimeZone) Collection(java.util.Collection) MessageProcessorNotification(org.mule.runtime.api.notification.MessageProcessorNotification) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Flow(org.mule.runtime.core.api.construct.Flow) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) FlowBackPressureException(org.mule.runtime.core.internal.construct.FlowBackPressureException) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) List(java.util.List) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) MESSAGE_PROCESSOR_PRE_INVOKE(org.mule.runtime.api.notification.MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE) CPU_LITE(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType.CPU_LITE) FLOW_BACK_PRESSURE_ERROR_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.FLOW_BACK_PRESSURE_ERROR_IDENTIFIER) ProcessingType(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Flux.from(reactor.core.publisher.Flux.from) InternalProcessor(org.mule.runtime.core.privileged.processor.InternalProcessor) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) BLOCKING(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType.BLOCKING) HashSet(java.util.HashSet) JUnitLambdaProbe(org.mule.tck.probe.JUnitLambdaProbe) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MuleContext(org.mule.runtime.core.api.MuleContext) CPU_LITE_ASYNC(org.mule.runtime.core.api.processor.ReactiveProcessor.ProcessingType.CPU_LITE_ASYNC) CoreMatchers.allOf(org.hamcrest.CoreMatchers.allOf) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) AnnotatedProcessor(org.mule.runtime.core.privileged.processor.AnnotatedProcessor) Logger(org.slf4j.Logger) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) Executors.newFixedThreadPool(java.util.concurrent.Executors.newFixedThreadPool) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Latch(org.mule.runtime.api.util.concurrent.Latch) Rule(org.junit.Rule) Exceptions.rxExceptionToMuleException(org.mule.runtime.core.api.rx.Exceptions.rxExceptionToMuleException) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) IntegerAction(org.mule.runtime.api.notification.IntegerAction)

Example 4 with IntegerAction

use of org.mule.runtime.api.notification.IntegerAction in project mule by mulesoft.

the class DefaultExceptionStrategyTestCase method testExceptionNotifications.

// MULE-1627
@Test
public void testExceptionNotifications() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger notificationCount = new AtomicInteger(0);
    ((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationListenerRegistry.class).registerListener((ExceptionNotificationListener) notification -> {
        if (new IntegerAction(EXCEPTION_ACTION).equals(notification.getAction())) {
            assertEquals("exception", notification.getActionName());
            assertEquals("Wrong info type", TYPE_ERROR, notification.getType());
            notificationCount.incrementAndGet();
            latch.countDown();
        }
    });
    // throwing exception
    InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
    strategy.setAnnotations(singletonMap(LOCATION_KEY, TEST_CONNECTOR_LOCATION));
    strategy.setMuleContext(muleContext);
    strategy.setNotificationFirer(((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationDispatcher.class));
    strategy.handleException(new IllegalArgumentException("boom"));
    // Wait for the notifcation event to be fired as they are queue
    latch.await(2000, MILLISECONDS);
    assertEquals(1, notificationCount.get());
}
Also used : Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) CountDownLatch(java.util.concurrent.CountDownLatch) IntegerAction(org.mule.runtime.api.notification.IntegerAction) MuleContext(org.mule.runtime.core.api.MuleContext) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TYPE_ERROR(org.mule.runtime.api.notification.AbstractServerNotification.TYPE_ERROR) Collections.singletonMap(java.util.Collections.singletonMap) EXCEPTION_ACTION(org.mule.runtime.api.notification.ExceptionNotification.EXCEPTION_ACTION) ExceptionNotificationListener(org.mule.runtime.api.notification.ExceptionNotificationListener) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) Assert.assertEquals(org.junit.Assert.assertEquals) IntegerAction(org.mule.runtime.api.notification.IntegerAction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with IntegerAction

use of org.mule.runtime.api.notification.IntegerAction in project mule by mulesoft.

the class DefaultMuleApplication method setMuleContext.

private void setMuleContext(final MuleContext muleContext, Registry registry) {
    statusListener = new MuleContextNotificationListener<MuleContextNotification>() {

        @Override
        public boolean isBlocking() {
            return false;
        }

        @Override
        public void onNotification(MuleContextNotification notification) {
            Action action = notification.getAction();
            if (new IntegerAction(CONTEXT_INITIALISED).equals(action) || new IntegerAction(CONTEXT_STARTED).equals(action) || new IntegerAction(CONTEXT_STOPPED).equals(action) || new IntegerAction(CONTEXT_DISPOSED).equals(action)) {
                updateStatusFor(muleContext.getLifecycleManager().getCurrentPhase());
            }
        }
    };
    notificationRegistrer = registry.lookupByType(NotificationListenerRegistry.class).get();
    notificationRegistrer.registerListener(statusListener);
}
Also used : Action(org.mule.runtime.api.notification.Notification.Action) IntegerAction(org.mule.runtime.api.notification.IntegerAction) IntegerAction(org.mule.runtime.api.notification.IntegerAction) MuleContextNotification(org.mule.runtime.core.api.context.notification.MuleContextNotification)

Aggregations

IntegerAction (org.mule.runtime.api.notification.IntegerAction)6 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)4 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 NotificationListenerRegistry (org.mule.runtime.api.notification.NotificationListenerRegistry)3 MuleContext (org.mule.runtime.core.api.MuleContext)3 AbstractMuleContextTestCase (org.mule.tck.junit4.AbstractMuleContextTestCase)3 Callable (java.util.concurrent.Callable)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Thread.currentThread (java.lang.Thread.currentThread)1 Thread.sleep (java.lang.Thread.sleep)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Collections.synchronizedSet (java.util.Collections.synchronizedSet)1 EventObject (java.util.EventObject)1 HashSet (java.util.HashSet)1