Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Stream Processing in Spring XD 1.1

DZone's Guide to

Stream Processing in Spring XD 1.1

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Written by Josh Long on the Spring Blog

This tip is drawn heavily from this Wiki-page on Spring XD’s streaming support by various Spring XD team-members, and particularly the amazing Ilayaperumal Gopinathan

Spring XD 1.1 is here and is packed with lots of new features. One theme for this release is rich stream processing support. Spring XD 1.1 provides integration with Project Reactor Streams,RxJava Observables, and Spark’s streaming.

Let’s look specifically at using Reactor, though the concepts are similar across all of the supported streaming APIs.

Messages that are delivered on the Message Bus are accessed from the input Stream. The return value is the output Stream that is the result of applying various operations to the input stream. The content of the output Stream is sent to the message bus for consumption by other processors or sinks. To implement a Stream-based processor module you need to implement the interface org.springframework.xd.reactor.Processor:

import org.springframework.xd.reactor.Processor;
import org.springframework.xd.tuple.Tuple;
import reactor.rx.Stream;

import static com.acme.Math.avg;
import static org.springframework.xd.tuple.TupleBuilder.tuple;

public class MovingAverage implements Processor<Tuple, Tuple> {

  @Override
  public Stream<Tuple> process(Stream<Tuple> inputStream) {
    return inputStream.map(tuple -> tuple.getDouble("measurement"))
      .buffer(5)
      .map(data -> tuple().of("average", avg(data)));
  }
}

Writing a test for this is as simple as setting up a Spring Integration flow that takes input on a request channel and routes it to this processor via aorg.springframework.xd.reactor.SynchronousDispatcherMessageHandler component which itself writes its output to an output channel. From there, you can package and registerthe custom processor in the Spring XD admin server.


Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Published at DZone with permission of Pieter Humphrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}