Getting Started

Quickstart

The easiest to start using SmallRye Reactive Messaging is to start it directly in a main class.

Creates a Maven project, and include the following dependency in your pom.xml:

<dependency>
  <groupId>io.smallrye.reactive</groupId>
  <artifactId>smallrye-reactive-messaging-provider</artifactId>
  <version>3.13.0</version>
</dependency>
<dependency>
 <groupId>io.smallrye.reactive</groupId>
 <artifactId>mutiny-reactive-streams-operators</artifactId>
 <version>1.1.2</version>
</dependency>
<dependency>
  <groupId>org.jboss.weld.se</groupId>
  <artifactId>weld-se-core</artifactId>
  <version>3.1.8.Final</version>
</dependency>
<dependency>
  <groupId>io.smallrye.config</groupId>
  <artifactId>smallrye-config</artifactId>
  <version>2.6.1</version>
</dependency>

Once created, create a class file with a public static void main(String…​ args) method:

package quickstart;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.se.SeContainerInitializer;

@ApplicationScoped
public class Main {

    public static void main(String[] args) {
        SeContainerInitializer.newInstance().initialize();
    }

}

This example use CDI SE, so a version compatible with regular (i.e. SE) Java.

Then, we need CDI beans. For instance:

package quickstart;

import javax.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;
import org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder;
import org.eclipse.microprofile.reactive.streams.operators.ReactiveStreams;

@ApplicationScoped
public class MyBean {

    @Outgoing("source")
    public PublisherBuilder<String> source() {
        return ReactiveStreams.of("hello", "with", "SmallRye", "reactive", "message");
    }

    @Incoming("source")
    @Outgoing("processed-a")
    public String toUpperCase(String payload) {
        return payload.toUpperCase();
    }

    @Incoming("processed-a")
    @Outgoing("processed-b")
    public PublisherBuilder<String> filter(PublisherBuilder<String> input) {
        return input.filter(item -> item.length() > 4);
    }

    @Incoming("processed-b")
    public void sink(String word) {
        System.out.println(">> " + word);
    }

}

Finally, you need an empty beans.xml. Copy the following content to src/main/resources/META-INF/beans.xml:

<beans
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
      http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  bean-discovery-mode="annotated">
</beans>

Once everything is setup, you should be able to run the application using:

mvn compile exec:java -Dexec.mainClass=quickstart.Main

Running the previous example should give the following output:

>> HELLO
>> SMALLRYE
>> REACTIVE
>> MESSAGE

SmallRye Reactive Messaging uses JBoss-logging to log.

For testing purpose, you can simply add a logging.properties file in ${basedir}/src/main/resources/logging.properties

Then run the application using:

mvn package exec:java -Dexec.mainClass=quickstart.Main -Djava.util.logging.config.file=${basedir}/src/main/resources/logging.properties

Example logging.properties:

handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s] %5$s %n
.level=OFF
acme.level=FINEST