So now let starting to preparing our platform, we will require Vagrant and Virtual Box, so install those first.
This will take a considerable amount of time so wait until everything is completed.
Now to test that the installation work we could just:
This will output:
Now we could stop our Whisk server anytime with:
And bring it back with:
Now let’s create a simple Kotlin function:
This function just calculate a set of Fibonacci numbers, let try it out:
This should output:
But how we could convert this into a Serverless function?
First we need to create a main function as OpenWhisk will be able to understand :
OpenWhisk use Google GSon so we will use in the final project maven to package our jar with dependencies, but for now let’s concentrate in the code, but the pom could be see here.
OpenWhisk will sent to our function as many parameters as we define when we create our function, so for this I’ll need a parameters called numbers.
We could get the value with a simple extension function:
So now in our main function we could do something like :
All so now we need to get a result so we could do something like:
This look OK but I think we could improve adding just one extension function:
With this our main could become just:
Let put all the parts together :
With this prototype we could create many functions, in fact with a couple of generics could get even better but let’s do that another day.
So now we need to compile our function, since latter we will use vagrant is better that we put our project in the OpenWhisk folder created at the beginning of the post
since it will be map into the OpenWhisk machine.
I’ve create mine under openwhisk/projects/KotlinServerless and I’ll use maven wrapper
Now get back into the vagrant directory and ssh into the OpenWhisk machine:
Now from the OpenWhisk machine we will get into our function directory:
We need to specify the full location of our class, and remember than an static method in Kotlin get created in a class named _FileName_Kt.class
Now let’s run our function:
And we will get as output something like:
And of course we could running without parameters as:
And the result will be:
Finally don’t forget to suspend the vagrant machine after your done for the day, if not the OpenWhisk server sometimes get in bad state.
You could get it back simply with:
I think this is enough for today, let’s see what else we do another day with this interesting topic.
About Juan Medina I'm just a normal geek that code all kind of stuff, from complex corporate applications to games.
Games, music, movies and traveling are my escape pods.