Asynchronous Programming in Grails 3

Grails has had support for asynchronous programming for some time now but it seems to have become more well-defined in Grails 3. It has Promises, Events, and even asynchronous processing in GORM. The article is focusing on some functionality in Event processing, most of the functionality coming from Reactor.

You can see my simple example on Github. It’s pretty straightforward on how it works — you enter a number into a form, and then check the server output for counting from 1 to that number. The numbers won’t be in order though — that is because a service is sending them to another service in an asynchronous manner.

The LoopService is really what starts this off — it gets a number and starts the loop:

Note that it doesn’t call another service directly — instead it calls this magic notify method that takes a string and the value. This is because services in Grails 3 have the Event trait which give you an interface into Reactor. The string is the name of the event to fire and the second parameter is the Object to send to the consumer of the event. Notice that I said Object… I first used type int to make this example and it took me awhile to figure out why I had a NullPointerExeception. Simply moving it to Integer made it start working.

The consumer of this event is the EchoNumberService.

The Consumer and Selector annotations come from Reactor. Consumer simply signifies that this class consumes events and Selector lets you give the name of the event that method consumes. Note that the echo method consumes the int.echo event we used above. I think the other important thing to note is that there is nothing else do to! There is nothing in the method itself that signifies that it’s running in an asynchronous matter. And you could use the same method in a synchronous scenario if need be by calling the method directly.

About the Author

Object Partners profile.

One thought on “Asynchronous Programming in Grails 3

  1. Bruno says:

    Very nice post Mike! I recently started using asynchronous programming, and I have a doubt. What is the best (or most viable) way to test those situations when one service emit an event, and one or more services ?

    Thanks.

Leave a Reply to Bruno Cancel reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]