All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.rx.support.Subscribers Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version

package org.mule.rx.support;

import javax.resource.spi.work.Work;

import org.mule.RequestContext;
import org.mule.api.MessagingException;
import org.mule.api.MuleEvent;
import org.mule.api.context.WorkManager;
import org.mule.rx.RxMuleEventException;

import rx.Subscriber;

@SuppressWarnings("deprecation")
public abstract class Subscribers
{
    public static void routeEventToSubscriber(final MuleEvent muleEvent,
                                              final Subscriber subscriber,
                                              final WorkManager workManager)
    {
        try
        {
            workManager.scheduleWork(new Work()
            {
                @Override
                public void run()
                {
                    subscriber.onNext(RequestContext.setEvent(muleEvent));
                }

                @Override
                public void release()
                {
                    // NOOP
                }
            });
        }
        catch (final Exception e)
        {
            routeErrorToSubscriber(e, muleEvent, subscriber);
        }
    }

    public static void routeErrorToSubscriber(final Throwable t,
                                              final MuleEvent muleEvent,
                                              final Subscriber subscriber)
    {
        if (t instanceof RxMuleEventException)
        {
            subscriber.onError(t);
        }
        else if (t instanceof MessagingException)
        {
            subscriber.onError(new RxMuleEventException((MessagingException) t));
        }
        else
        {
            subscriber.onError(new MessagingException(muleEvent, t));
        }
    }

    private Subscribers()
    {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy