Nov 18, 2014

UDP Server with Spring Boot and Reactor

UDP support was added to the 1.1.0 release of project Reactor, but the web has been pretty quiet about it since. There are some good existing technologies out there if you need to write a UDP server, but those aren’t part of the sweet Reactor project… or integrate seamlessly with Spring Boot!

I have created a very simple, starter server on Github and will run through the pieces:

Reactor Spring Support
If you open the build.gradle file in the project you will notice the “org.projectreactor.spring:reactor-spring-context” dependency. This project implicitly creates the Environment and puts it into your application context when you use the @EnableReactor annotation on the configuration class. It makes wiring everything together very easy by providing annotations for specifying consumers, selectors, and etc. when you’re ready to start doing something with the incoming packets. Of course, it also does the lifecycle stuff you would expect with a Spring project, so check out their documentation for more.

The Datagram Server
Much like the Reactor TCP server, you shouldn’t instantiate an implementation of DatagramServer directly. Instead use the DatagramServerSpec to build the server and configure it. We aren’t doing anything interesting with the incoming packets for this example, just logging them when they arrive.

The CountDownLatch
As noted in this example from Spring, the TCP and UDP servers are non-blocking so this is necessary so the main thread doesn’t exit.

Give It a Run
Use the Gradle wrapper to build, and then run the jar (also be sure you are using JDK 8):

Spring boot will start up and you should see logging from the framework. Look for a statement that looks something like:

r.net.netty.udp.NettyDatagramServer      : BIND /0:0:0:0:0:0:0:0:{SOME_PORT_NUMBER}

This will tell you what port number the server is listening, keep this in mind for the next step.

Test It Out
Netcat is very convenient for sending simple UDP packets for situations like this. In a separate shell type:

Where $UDP_SERVER_PORT is the port we collected from the log output in the previous step.

About the Author

Object Partners profile.

One thought on “UDP Server with Spring Boot and Reactor

Leave a Reply

Your email address will not be published.

Related Blog Posts
Building Better Data Visualization Experiences: Part 1 of 2
Through direct experience with data scientists, business analysts, lab technicians, as well as other UX professionals, I have found that we need a better understanding of the people who will be using our data visualization products in order to build them. Creating a product utilizing data with the goal of providing insight is fundamentally different from a typical user-centric web experience, although traditional UX process methods can help.
Kafka Schema Evolution With Java Spring Boot and Protobuf
In this blog I will be demonstrating Kafka schema evolution with Java, Spring Boot and Protobuf.  This app is for tutorial purposes, so there will be instances where a refactor could happen. I tried to […]
Redis Bitmaps: Storing state in small places
Redis is a popular open source in-memory data store that supports all kinds of abstract data structures. In this post and in an accompanying example Java project, I am going to explore two great use […]
Let’s build a WordPress & Kernel updated AMI with Packer
First, let’s start with What is an AMI? An Amazon Machine Image (AMI) is a master image for the creation of virtual servers in an AWS environment. The machine images are like templates that are configured with […]