Giter Site home page Giter Site logo

rex's Introduction

Rex

Introduction

Rex facilitates the construction of reactive command-oriented service layers for enterprise applications in Java.

Quick start

Rex is compatible with Java >= 1.8. It also works with GraalVM.

  1. Add the following dependency to your Maven project pom.xml*:
<dependency>
    <groupId>com.github.jahwag.rex</groupId>
    <artifactId>rex</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</dependency>
  • Rex is not yet available in Maven central i.e. you will have to install it on your own repository before use.

Using Spring Boot

  1. To instruct Spring to scan for components inside rex jars, you may create a configuration as follows:
@ComponentScan("com.github.jahwag.rex")
@Configuration
public class RexConfiguration {
}

Using Java EE / Jakarta EE / MicroProfile / Weld (CDI)

  • In CDI >= 1.1, by default all beans are discovered (including Rex).
  • For older versions, consult framework documentation on how to configure CDI bean-discovery-mode.

How to use

Instructions below are provided with CDI annotations to define and inject enterprise beans. When using Spring, simply substitute @ApplicationScoped with Spring DI-annotations e.g. @Component.

1. Defining commands and reactions

Command

A Command defines a single use case consisting of an input and the corresponding result or output.

Example:

public final class ListClientsByLastNameCommand implements Command.Many<Client> {
    private final String lastName;
    ...
}

Reaction

A Reaction encapsulates how the application responds to the associated Command.

Example:

@ApplicationScoped
public final class ListClientsByLastNameReaction implements Reaction<ListClientsByLastNameCommand, Iterable<Client>> {

    @Override
    public Iterable<Client> react(ListClientsByLastNameCommand command) {
        ...
        return clients;
    }
}

2. Execution

Reactive

Rex exposes its reactive API using org.reactivestreams*.

*Pending wider adoption this will be replaced with java.util.concurrent

public class SampleEndpoint implements LinksService {

    @Inject
    private Rex rex;
    
    public Publisher<Something> doSomething(String description) {
        CreateSomethingCommand command = new CreateSomethingCommand(description);
        
        return rex.observe(command);
    }
}

Here Reactive::observe returns a single item containing the entire result of CreateSomethingCommand. This may not be preferable if your Command returns an instance of java.lang.Iterable (i.e. it is an instance of Command.Many). In this case use Rex::observeIterable.

If you like most of us prefer something more useful than Publisher, simply wrap the Publisher using your preferred implementation (Rxjava, Reactor etc) e.g. so:

Mono<String> something = Mono.from(rex.single(command))
                        .map(Something::toString);

The Maven artifact "rex-reactor" provides ready-made wrappers for Project Reactor.

Related reading

rex's People

Contributors

jahwag avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.