Posts

Showing posts from 2019

OpenJDK:JMH Simple Usage

Image
Introduction Few people have heard of JMH, yet if harnessed properly, can be a very valuable tool, typically when we come across performance considerations. Now and then we will come across a task that can be done multiple different ways but we are unsure which one is the best performing code. A quick JMH code over Java command line application can quickly tell you how is one code performing compared to the other. What JMH does when properly coded is to take your piece of code and run it for multiple warm up iterations to clear the memory cache, and then run it again over X amount iterations to obtain the averaged benchmark result. Elaboration Using JMH It is recommended to use JMH in the most simplest java way, ie using the old and standard public static void main: public static void main (String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(JmhTestRun. class .getSimpleName()) .forks( 1 ) .build() ; new Runner(opt).run

Cucumber + Junit5 + Localstack

Background What are the options to test an Amazon lambda that interacts with Dynamo DB locally? To be honest, there are several options, such as using Mockito, using stubs, swapping Dynamo DB with Mongo DB, however none of these truly replicate the amazon lambda and dynamo db ecosystem. Enter Localstack  ( https://github.com/localstack/localstack ), open source project that aims to emulate a local AWS cloud stack. With its docker version, we could spin up localstack and push our lambda and create our table in the dynamodb. To further increase its testing prowess, Localstack comes up with utils to integrate with Junit 4 and 5. Now we have the tools to create a BDD test using Cucumber, JUnit5 and Localstack as you can see below. Do note that due to the open source nature of localstack, it has several bugs that we manage to get around Prereq: Docker images: localstack/localstack:0.9.1 Note that the latest version of localstack docker image has bugs with the service port nu

Dual Spring Boot and AWS Lambda Project

Image
Sometimes, we need to convert an existing Spring Boot application to AWS Lambda function, often due to upgrade in technology or company's new direction to the sky.. I mean cloud and beyond... In some extremely rare case we might be forced to write a dual Spring Boot and AWS Lambda project, that is a project that can run as a Spring Boot application in its embedded Tomcat container and also can be uploaded as AWS Lambda function. This usually happens when one of the developer in the team has had one too many "discussions" about Spring Boot or Lambda that he proceeded to create dual app while the rest of the team going round and round in circle. Luckily, awslabs has answer to that, they call it Serverless Java Container :  https://github.com/awslabs/aws-serverless-java-container Here is my simple example on how to achieve the dual Spring Boot x AWS Lambda application: The source:  https://github.com/overtakerx/springbootawslambdademo  contains a simple typical S

Spring Boot 2: Spring WebFlux for Reactive APIs

Spring Boot 2: Spring WebFlux for Reactive APIs Spring WebFlux is a relatively new library from Spring Boot 2 that provides massive improvement in performance compared to the traditional sync calls. To quote the Spring documentation itself: Why was Spring WebFlux created? Part of the answer is the need for a non-blocking web stack to handle concurrency with a small number of threads and scale with fewer hardware resources. Servlet 3.1 did provide an API for non-blocking I/O. However, using it leads away from the rest of the Servlet API, where contracts are synchronous ( Filter ,  Servlet ) or blocking ( getParameter ,  getPart ). This was the motivation for a new common API to serve as a foundation across any non-blocking runtime. That is important because of servers (such as Netty) that are well-established in the async, non-blocking space. Further reading:  https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html In short, Spring Webflux

Scamming the Scammer

Image
Not so long ago I noticed these flyers on my apartment's notice board which in summary warns the general public of the recent scam events that has been going around and caught many people by surprise. One thing that aroused my interest was that the flyers were not translated to mandarin , something that my apartment management usually would never fail to do in the past. Then quite recently I came across the answer:    And on the other chats:   And few hours later: If you are confused, here are the summary: Scammer pretended to be a guy fell in love with the girl Scammer faked sending a gift to her The gift was supposedly blocked by custom and require $$ for clearance Scammer's fake delivery company asked for $3100 to clear the custom Scammer claimed he put money inside the gift to pay for the custom, but she needs to pay first to get the gift out of custom before she can get the money from the gift box. Typical