Enterprise Java

KivaKit and AWS Lambda

KivaKit 1.2 adds seamless support for AWS Lambda. Lambdas for REST and GRPC can be added to a KivaKit Microservice without alteration (which will make this a short article).

Creating a Lambda

We have already seen a KivaKit request handler for REST in the Microservices article. We will simply reuse this code as our Lambda request handler. As a reminder the code from that article looks like this:

@OpenApiIncludeType(description = "Request for divisive action")
public class DivisionRequest extends BaseMicroservletRequest
{
    @OpenApiIncludeType(description = "Response to a divide request")
    public class DivisionResponse extends BaseMicroservletResponse
    {
        @Tag(1)
        @Expose
        @OpenApiIncludeMember(description = "The result of dividing",
                              example = "42")
        int quotient;

        public DivisionResponse()
        {
            this.quotient = dividend / divisor;
        }

        public String toString()
        {
            return Integer.toString(quotient);
        }
    }

    @Tag(1)
    @Expose
    @OpenApiIncludeMember(description = "The number to be divided",
                          example = "84")
    private int dividend;

    @Tag(2)
    @Expose
    @OpenApiIncludeMember(description = "The number to divide the dividend by",
                          example = "2")
    private int divisor;

    public DivisionRequest(int dividend, int divisor)
    {
        this.dividend = dividend;
        this.divisor = divisor;
    }

    public DivisionRequest()
    {
    }

    @Override
    @OpenApiRequestHandler(summary = "Divides two numbers")
    public DivisionResponse onRequest()
    {
        return listenTo(new DivisionResponse());
    }

    @Override
    public Class<DivisionResponse> responseType()
    {
        return DivisionResponse.class;
    }

    @Override
    public Validator validator(ValidationType type)
    {
        return new BaseValidator()
        {
            @Override
            protected void onValidate()
            {
                problemIf(divisor == 0, "Cannot divide by zero");
            }
        };
    }
}

Adding a Lambda Service

In a similar fashion to adding a REST service, a Lambda service is added like this:

public class DivisionMicroservice extends Microservice
{
    [...]

    @Override
    public MicroserviceLambdaService onNewLambdaService()
    {
        return new DivisionLambdaService(this);
    }
}

The onNewLambdaService() method returns an instance of DivisionLambdaService, which extends MicroserviceLambdaService:

public class DivisionLambdaService extends MicroserviceLambdaService
{
    [...]

    @Override
    public void onInitialize()
    {
        mount("division", "1.0", DivisionRequest.class);
    }
}

When the service is initialized, a call to the mount() method in onInitialize() is used to associate the name of our lambda and its version with the handler DivisionRequest. Nothing more is required.

Code

The code discussed above is available on GitHub:

The KivaKit Microservice API, including support for AWS Lambda, is available on Maven Central at these coordinates:

<dependency>
    <groupId>com.telenav.kivakit</groupId>
    <artifactId>kivakit-microservice</artifactId>
    <version>1.2.0</version>
</dependency>

Published on Java Code Geeks with permission by Jonathan Locke, partner at our JCG program. See the original article here: KivaKit and AWS Lambda

Opinions expressed by Java Code Geeks contributors are their own.

Jonathan Locke

Jonathan has been working with Java since 1996, and he was a member of the Sun Microsystems Java Team. As an open source author, he is originator of the Apache Wicket web framework (https://wicket.apache.org), as well as KivaKit (https://www.kivakit.org, @OpenKivaKit) and Lexakai (a tool for producing UML diagrams and Markdown indexes from Java source code, available at https://www.lexakai.org, @OpenLexakai). Jonathan works as a Principal Software Architect at Telenav (https://www.telenav.com), and in the future, Telenav will release further toolkit designed and led by Jonathan called MesaKit, focused on map analysis and navigation.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button