How to implement actions in Kalix using Scala

Reading Time: 3 minutes

What is an action?

Actions are stateless functions that can be used to implement various use cases such as:

  • pure function: A function (block of code) that always returns the same result when passed the same arguments. It does not depend on any state or data change during program execution. Rather, it only depends on its input arguments.
  • request conversion – You can use an Action to convert incoming data to another format before routing the call to another component.
  • publish and subscribe events: publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program messages to be sent directly to specific recipients, called subscribers, but instead categorize published messages into classes without knowing which subscribers, if any, may exist.
  • schedule and cancel timers

How to trigger actions?

Actions can be triggered in the following ways:

  • a gRPC service call
  • an HTTP service call
  • a forwarded call from another component
  • a scheduled call from a timer
  • an incoming event from within the same service or a from different service

Steps to implement actions

Defining the proto file

Let’s see with an example how we can implement actions. So, we will be creating a FactorialAction that takes a number n and will return the factorial of that number.

Defining the proto

In the above code, we have created a Factorial service which contains an rpc FindFactorial.

Implementing the Action

When we compile the above code, the class FactorialAction is generated. FactorialAction class is where we will be implementing the action. Once the FactorialAction.scala file exists, it is not overwritten, so you can add logic to it as you like.

Below is how FactorialAction.scala will look like.

Implement the action

As we can see that the findFactorial method is unimplemented, we can go ahead and implement it.

Implement the action

Registering the Action

In order for Kalix to be aware of an Action, we need to register it with the service.

From code generation, the registration is automatically inserted into the generated KalixFactory.withComponents method from the Main class.

Registering the action

Testing the Action

This is the last step where we will be unit testing our FactorialAction.

When we compile the proto file, Kalix generates the FactorialActionTestKit that allows us to call the methods of FactorialAction. It also generates the test classes which helps us to write the test cases easily since a lot of boilerplate code is already there.

Testing the action

So, this was all about implementing the actions in Kalix using Scala.

Want to learn more about Kalix? You can checkout more blogs for Kalix here.

Written by 

Bhavya Verma is a Software Consultant at Knoldus. An avid Scala programmer, he is recognised as a good team player, dedicated and responsible professional, and a technology enthusiast. He has good time management skills. His hobbies include playing badminton, watching movies and travelling.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading