Picnic :: Reactive Support

Reactive programming support library by Picnic.

License

License

Categories

Categories

React User Interface Web Frameworks
GroupId

GroupId

tech.picnic.reactive-support
ArtifactId

ArtifactId

reactive-support
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

pom
Description

Description

Picnic :: Reactive Support
Reactive programming support library by Picnic.
Project URL

Project URL

https://github.com/PicnicSupermarket/reactive-support
Project Organization

Project Organization

Picnic Technologies BV
Source Code Management

Source Code Management

https://github.com/PicnicSupermarket/reactive-support

Download reactive-support

Filename Size
reactive-support-0.0.2.pom 47 KB
Browse

How to add to project

<!-- https://jarcasting.com/artifacts/tech.picnic.reactive-support/reactive-support/ -->
<dependency>
    <groupId>tech.picnic.reactive-support</groupId>
    <artifactId>reactive-support</artifactId>
    <version>0.0.2</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/tech.picnic.reactive-support/reactive-support/
implementation 'tech.picnic.reactive-support:reactive-support:0.0.2'
// https://jarcasting.com/artifacts/tech.picnic.reactive-support/reactive-support/
implementation ("tech.picnic.reactive-support:reactive-support:0.0.2")
'tech.picnic.reactive-support:reactive-support:pom:0.0.2'
<dependency org="tech.picnic.reactive-support" name="reactive-support" rev="0.0.2">
  <artifact name="reactive-support" type="pom" />
</dependency>
@Grapes(
@Grab(group='tech.picnic.reactive-support', module='reactive-support', version='0.0.2')
)
libraryDependencies += "tech.picnic.reactive-support" % "reactive-support" % "0.0.2"
[tech.picnic.reactive-support/reactive-support "0.0.2"]

Dependencies

provided (1)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 jar 3.0.2

Project Modules

  • rxjava-support

Picnic Reactive Support

Build Status Maven Central Coverage SonarCloud Quality Gate SonarCloud Bugs SonarCloud Vulnerabilities SonarCloud Maintainability BCH compliance

A collection of Reactive Programming (RxJava and Reactor) utilities forged and implemented in the Picnic backend.

How to Install

Artifacts are hosted on Maven's Central Repository:

Gradle

dependencies {
    compile 'tech.picnic.reactive-support:rxjava-support:0.0.3'
}

Maven

<dependency>
    <groupId>tech.picnic.reactive-support</groupId>
    <artifactId>rxjava-support</artifactId>
    <version>0.0.3</version>
</dependency>

How To Use

This library contains a number of useful RxJava utilities. Listed below are example usages of some notable cases.

RxJava Thread Context Propagation

To propagate thread-local contexts to RxJava Scheduler thread pools you can create your own RxThreadLocal containing a value fetcher Supplier<T> (that is called before a Runnable is scheduled by a Scheduler), a context setter Consumer<T> that sets the context on the new RxJava Scheduler thread, and an unset Consumer<T> which reinstates the original context after the Scheduler has finished executing the scheduled Runnable. The same Consumer<T> can be used to both configure and restore the context.

Please see the code below for an example usage with initialisation in ServletContextListener, where we propagate the contents of SLF4J's MDC map and Spring's SecurityContextHolder. This way that we can retain meaningful logging attributes and execute secured methods from within RxJava-scheduled actions.

@WebListener
public final class RxServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        RxThreadLocal<?> mdcPropagation =
                RxThreadLocal.from(
                        MDC::getCopyOfContextMap,
                        m -> {
                            if (m != null) {
                                MDC.setContextMap(m);
                            } else {
                                MDC.clear();
                            }
                        });
        RxThreadLocal<?> securityContextPropagation =
                RxThreadLocal.from(
                        SecurityContextHolder::getContext, SecurityContextHolder::setContext);
        PicnicRxPlugins.configureContextPropagation(mdcPropagation, securityContextPropagation);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        PicnicRxPlugins.unsetContextPropagation();
        Schedulers.shutdown();
    }
}

RetryStrategy

The RetryStrategy type is meant to be used in conjunction with Flowable#retryWhen to create and define more advanced retry strategies. For example:

someFlowable
    .retryWhen(
         RetryStrategy.ifInstanceOf(TooManyRequestsException.class)
             .withBackoffScheduler(Schedulers.io())
             .exponentialBackoff(Duration.ofMillis(500)
             .build())

RxSpring4Util

This is a utility that can be used to convert RxJava reactive types into asynchronous Spring web types. See below for a number of example usages.

Converting Spring DeferredResult:

@PostMapping("/endpoint")
public DeferredResult<Integer> someEndpoint() {
    return Single.just(1).to(singleToDeferredResult());
}

@GetMapping("/another")
public DeferredResult<ImmutableSet<Integer>> someOtherEndpoint() {
    return Flowable.just(1, 2, 3).to(publisherToDeferredResult(ImmutableSet::copyOf));
}

Converting to Spring's SseEmitter:

@GetMapping("/sse")
public SseEmitter sseEndpoint() {
    return Flowable.just(1, 2, 3).to(publisherToSse());
}

Contributing

Contributions are welcome! Feel free to file an issue or open a pull request.

When submitting changes, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. New code must be covered by tests. As a rule of thumb, overall test coverage should not decrease. (There are exceptions to this rule, e.g. when more code is deleted than added.)

tech.picnic.reactive-support

Picnic

Our mission is to simplify the life of busy families by making grocery shopping the best experience ever!

Versions

Version
0.0.2
0.0.1