A perfect java blog

My colleague were just discussing about the new architecture serverless most of the time and out of curiosity I fiddle around with this term and learned what this really is? Then I found out that it is just a way to execute your normal functions and from URL and you do not need your server to be online all the time which results in super cost effective unlike normal server where you pay for every month/hour/second of its availability. The term looks super easy to understand. Execute my function foo() when https://mydomain.com/foo is called via HTTP protocol. The term serverless without any server is just for the developers but beneath this architecture there will still be a fully managed server who handles your request and warms up your code (jar file). So I came to think of demonstrating how to run your minimal functions as serverless . But before that lets learn some basics via image.

Java Serverless diagram

Long thing in short : If any POST request in /foo URI happen the Function handler will call to the appropriate function of a class which is predefined in serverless.yml. We developers do not have to worry about all those API endpoints handling because these are handled by serverless itself, this is the beauty of serverless. In an Amazon Web Service world serverless is handled from AWS Lambda service.

Now it is the time to catchup and write our very first and simple serverless project.

You can directly override RequestHandler‘s handleRequest()method/function or you can create just simple method which is also supported for serverless function execution. The only difference is you have to use fully qualified Classname as well as the method name while mapping it to the URI in serverless.yml

Now there is one final piece of puzzle which is serverless.yml which will do the mappings of all functions to uri .

YAML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

service: serverless-example # NOTE: update this with your service name

# You can pin your service to only deploy with a specific Serverless version

# Check out our docs for more details

# frameworkVersion: "=X.X.X"

provider:

name: aws

runtime: java8

stage: ${opt:stage,'dev'}

# you can add packaging information here

package:

artifact: target/${self:service}-${self:provider.stage}.jar

functions:

fooHandler:

handler: com.serverless.Handler

events:

- http:

path: /foo

method: post

barHandler:

handler: com.serverless.Handler::barRequest

events:

- http:

path: /bar

method: get

infoHandler:

handler: com.serverless.Handler::barRequest

events:

- http:

path: /info

method: get

And it is just simple is that. You are ready for deploying your first serverless using simple Java class which will handle your HTTP request directly. You can deploy your codes directly using your serverless command. But make sure you have proper AWS credentials and policy to deploy your code from commandline.

Now it is time to do small testing from Postman . As provided in the url : https://0manjxyoi0.execute-api.us-east-1.amazonaws.com/dev/foo [POST]

AWS Lambda Serverless Endpoing test

It is quiet so easy isn’t it ? Just code what you want and deploy it. But there are some limitation of the serverless. It is not suitable for all kind of application and it definitely does not supersede the normal server deployment because of its nature and limits.

1. Maximum execution duration per request

We just cannot use request for long task like more than 5 minutes. AWS has a hard limit of 5 minutes for every request execution.

2. Memory resources limit

There are some limits of memory to be used by every single function . It is like 3008M in aws lambda as of now (2018-09-11).

3. Coldstart

Your function will actually sleep after unused for some minutes. When you request after the sleep time it need to warm up the server again which takes some seconds and it might not be good for those who wants to get the request instantly (realtime). There are some way which will keep your server warm all time described How to keep desired amount of AWS Lambda function containers warm

4. Payload size limit

You might not want to send a large data from your http request because there is a limit for request payload as well . As per AWS there is 6MB limit for payload however you can use event driven aws lambda execution for avoid sending payload directly to lambda.

5. Ephemeral disk capacity

You just cannot use your disk space more than 512 mb which is /tmp directory of server while you do any data processing or any process which involves usage of hard disk space.

After talking about the limitations I would still prefer this serverless for some of my application like generating the pdf invoices for customer, doing scheduled tasks and others. In next blog post I would talk about how to run serverless locally and we can discuss about the overall complication and solutions.

Stay cool , be happy and code 😉

Thanks

Share this:

It has been a long time since i posted any java related blog post. Today it is a time i came up from my busy schedule. So in this post i am just only going to demonstrate how to add JWT authentication for small boot application which will use redis for saving its user’s session and information. There are many way we can use session but lets try this time with Redis as primary source for session storage.

I have my redis server in my VMWare which is opened in default port 6379. So now all i need is small spring boot application to get started. Lets start a new project with clean maven project and some dependencies essential for making JWT Token based session using redis.

1. Project Create

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.7.RELEASE</version>

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

<groupId>io.jsonwebtoken</groupId>

<artifactId>jjwt</artifactId>

<version>0.7.0</version>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

</dependency>

</dependencies>

Here we have just added <parent> for making the project to inherit all the stuffs from the spring-boot-starter-parent. So now we have added spring-boot-starter-web,spring-boot-starter-data-redis, spring-security, jwt, jedis(redis) and lombok in our project which are essential dependency for starting our spring boot rest application with redis support.

Share this:

It was a nearly a middle of a day [Saturday April 25 2015] while me and my friend were talking about our last journey from Kathmandu(Nepal) to Raxaul(India) (friend’s brother’s wedding) and we were so tired as we returned back to Kathmandu just Friday. I was forcing my friend to get off from the bed and take a breakfast but suddenly I felt dizzy movement which was an earthquake, it was so quick with very high intensity of movement. I could easily decide that was a massive quake because there was a mass of crackling sound coming from around my house and those sounds were new to me. I grabbed one big pillow from bed and brought my friend to cover up with that big rectangular pillow. We were sitting side by side near by door frame with our knee down position covering our heads. We were having a tough moment at that time and convince each other for not getting panic. After that massive shake my father and elder brother came down to the ground floor where I am living, Everybody were so terrified by this first sudden quake, we were planning for staying at ground floor for next 5 minutes as it was not quiet safe to go out from home at that moment. I looked outside my window to see the condition and there were people running and I saw my bike fell down and quickly went outside to keep my bike in double stand position. The road way of my house was already cracked, my neighbor’s house also had cracks at the basement, I checked my house as well but it was not hurt.

Share this:

Well It has been quiet long time but I found it would be time for me to blog small post about making the table cell selection in JavaFX. Normally TableView has facility to select the cell or row by using Ctrl,Command (Shortcut Key) and Shift button. You can easily select the data using your keyboards. But when it comes for ease to user interaction using mouse is vital. So today we will go through how to create selection of data by mouse. Lets categorize them in two parts. Read the rest of this entry »

Share this:

After a long time I am back again with new stuffs. I have seen that JavaFX has got so many demand nowadays. Lots of people are requesting for something new something wow effect. In the same way one of my colleagues told me what if we have listview got some effects on scrolling the list. I got some dig around JavaFX Animation API and did some animation with ListCell but I thought it would be great if I share my works to you guys.

First We got to revamp what is the Listcell. ListCell are designed for making user to display text content in list format. But we can override these and make our own like displaying images,shapes and other controls as well.

ListCell inherits the character of Labeled so in default ListCell only displays the text content.If you want some control in your listcell other than label then there are some bunch of cellfactory in javafx.scene.control.cell package.

Every ListCell are being rendered according to the cellFactory implementation so you are free to implement your own cellFactory to make the listcell even more customizable. I had posted about TableCell customization which utilizes use of cellFactory (TableView Cell Modify)