What is @Qualifier annotation in Spring and why it is used


In this tutorial, we will explore the @Qualifier annotation of Spring framework.

1. What does @Qualifier annotation do
2. @Qualifier examples


AWS Lambda to save data in DynamoDB

In this tutorial, we will see how using AWS Lambda we can save data in Dynamo DB.

Here are the steps which are required :

- Create a table in the Dynamo DB with the name Employee

- Create a AWS Lambda function which can save firstName and surName of an employee using a Employee POJO in the Dynamo DB

- Create a Policy which will give Read/Write access to only Employee table of DynamoDB

- Attach Policy to a Role

- Upload the code of AWS lambda function in the form of jar in the AWS lambda console

- Attach role created in step 4 to the AWS Lambda

- Run Test event to invoke AWS Lambda to save Employee data in Dynamo DB

AWS Lambda function with Java Pojo as Input Output Example

In the previous tutorial, we saw How to Create AWS Lambda function with Java and we passed String as input and also returned String as Output.I will recommend to go through that tutorial first, if you are creating lambda function for the first time.

In this tutorial, we will see, how we can pass Java Plain old Java object(POJO) as input and also return it as Output.



Here we will be implementing RequestHandler interface.


package com.blogspot.javasolutionsguide;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.blogspot.javasolutionsguide.input.Input;
import com.blogspot.javasolutionsguide.output.Output;

public class HelloWorldWithPojoInputOutput implements RequestHandler<Input,Output>{

@Override
public Output handleRequest(Input input, Context context) {
String message = String.format("Hello %s%s.", input.getName() , " " + "from" + context.getFunctionName());
return new Output(message);
}

}


And here is our Input and Output classes.


package com.blogspot.javasolutionsguide.input;


public class Input {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

package com.blogspot.javasolutionsguide.output;


public class Output {
private String message;
public Output(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}


For uploading the code to AWS console, please follow steps from my previous tutorial.

Once you have uploaded your jar in AWS lambda console, click on "Select a Test event" and then "configure test events".

Enter event name as "HelloWorldWithPojoInputOutput" and replace following

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

with following :

{
  "name" : "Gaurav Bhardwaj"
}

and click on Create button.

Now click on Test button and you should see your lambda function executed successfully with message "Hello Gaurav Bhardwaj from HelloWorld",which is the output returned by our lambda function.




You can find all code of this tutorial in GitHub

Summary

So in this tutorial, we saw how we can pass POJO to lambda function and also we can return POJO from a lambda function.


How to create AWS Lambda function with Java


In this tutorial, we will see how we can create AWS Lambda function in Java and I tell you, it is quite easy to do so...

Basically, there are three ways in which we can create AWS Lambda function :

- By implementing RequestHandler interface
- By implementing RequestStreamHandler interface
- Custom implementation, which does not require us to implement any AWS specific interface


AWS Lambda function by implementing RequestHandler interface

For using this method of creating AWS lambda function, we need to have following dependency in our project :

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>

</dependency>



And below is how your class will look like :


package com.blogspot.javasolutionsguide;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class HelloWorldWithRequestHandler implements RequestHandler<Object,Object> {

@Override
public Object handleRequest(Object input, Context context) {
return String.format("Hello %s%s.", input ," from " + context.getFunctionName());
}
} 


Once you have created maven project with above dependency and class in your project, maven build the project, which will create jar for you in the target folder of your project.

Now open the AWS Console, go to Services and search for AWS Lambda.


On the following screen ,click on Create Function.



On following screen, enter Function name "HelloWorld" and choose Runtime as Java 11.


In Permissions section, choose "Create a new role with basic Lambda permissions"and AWS Lambda will create and execution role with name HelloWorld-role-jc6cmpnj. This role is required to allow AWS Lambda to upload logs to AWS Cloudwatch logs.

Click on Create Function.


You will see following screen, where it says that "Successfully created the function HelloWorld.You can now change its code and configuration.To invoke your function with a test event, choose Test".





Now in the Function code section, click on the upload button and browse on your computer for the jar that you created earlier.

- Also, in the Handler textbox, replace example with package name where your "HelloWorldWithRequestHandler" class is residing, which in our case it is "com.blogspot.javasolutionsguide"
- And replace Hello with "HelloWorldWithRequestHandler".
- And replace handleRequest will stays as is ,because we also have same method name in our class.
Click on Save button on extreme right side of the screen.



Now to test our lambda function, we need to configure test event(s),for which we will click on "Select a Test event" drop down and then click on "Configure test events".


You will see following screen.Enter Event name as "HelloWorldEvents" and replace following

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

with just your name like as below :

"Gaurav Bhardwaj"

and click on Create button.



Now click on Test button and you should see your lambda function executed successfully with message "Hello Gaurav Bhardwaj from HelloWorld",which is the output returned by our lambda function.


If you click on the logs link in the above screen, it will take you to the AWS Cloudwatch screen where you can see that for your lambda function a LogGroup has been created and under which you have LogStream where you can see logs of your lambda function.This was the reason we assigned role to our lambda function, because AWS lambda used that role to push logs to the Cloudwatch.


AWS Lambda function by implementing RequestStreamHandler interface

In this case you need to follow exact same steps as in above case.It is just that in the code you need to implement RequestStreamHandler interface rather than RequestHandler interface as below.


package com.blogspot.javasolutionsguide;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

public class HelloWorldWithRequestStreamHandler implements RequestStreamHandler {

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
int letter;
        while((letter = inputStream.read()) != -1)
        {
            outputStream.write(Character.toUpperCase(letter));
        }
}
}

AWS Lambda function by custom implementation, which does not require us to implement any AWS specific interface

You can also have your custom lambda function ,which does not follow signature from some AWS specific interface.You can even omit Context object as well, if you don't want it.

In the following code,I have tried to put two handler methods, one with Context object and one without Context object.To test these both ,you just need to change the name of the method in the AWS console and it will start hitting that method.

Also ,we can see that from Context object ,we can get lots of useful information like name of AWS fucnton,its version,ARN,how much memory is allocated to the function(by default it is 512 mb) .


package com.blogspot.javasolutionsguide;

import com.amazonaws.services.lambda.runtime.Context;

public class HelloWorld {
        //Handler method without Context
public String sayHelloWorldWithoutContext(String name) {
return String.format("Hello %s.", name);
}
//We need to add aws-lambda-java-core dependency if we add Context as parameter.
public String sayHelloWorldWithContext(String name, Context context) {
context.getLogger().log("Lambda Function Name:" + context.getFunctionName() +
"Version:" + context.getFunctionVersion() + 
"Arn:" + context.getInvokedFunctionArn() +
"Allocated Memory:" + context.getMemoryLimitInMB() +
"Remaining Time:"+ context.getRemainingTimeInMillis());
return String.format("Hello %s.", name);
}

}

Also in the following example ,we can see that if we have two handler methods with same name in our class,the handler method which has Context object as its last parameter will be called.


package com.blogspot.javasolutionsguide;

import com.amazonaws.services.lambda.runtime.Context;

public class HelloWorldWithMultipleHandlersWithSameName {
public String handler(String name) {
return String.format("Hello %s.", name);
}
public String handler(String name, Context context) {
return String.format("Hello %s%s.", name,   " Memory Allocated:" + context.getMemoryLimitInMB());
}

}

You can find all code of this tutorial in GitHub

Summary :

- So, we saw how can create AWS lambda function in various ways in Java.
- Name of the handler method does not matter.You can choose any name of your choice.
- If you have two handler methods with same name in your class, the handler method which has Context object as its last parameter will be called.
- The first parameter of the handler method is the input to the handler, which can be 
   - Event data published by an event source like S3 in the form of predefined AWS event types like S3Event.
   - Custom input (Primitive or Object type).
- In order for AWS lambda to successfully invoke the handler method, the function must be invoked     with input data that can be serialized into the data type of the input parameter.
- If you are invoking your lambda function synchronously(Invoke type RequestResponse), then you can return any allowed primitive or Object type from your handler, however if you are invoking your lambda function asynchronously(Invoke type Event), then return type of your handler must be void.



How to convert String to int in Java


In this tutorial, we will see the various ways in which we can convert String to int(or Integer) in Java.

You can use any of the following ways :

- Using Integer.parseInt(string)
- Using Integer.valueof(string)
- Using Apache commons NumberUtils.toInt(string)
- Using Apache commons  NumberUtils.createInteger(string)
- Using Guava library's Ints.tryParse(string) method
- Using Integer.decode(string)
- Using new Integer(string)

How to prepare for AWS Certified Solutions Architect Associate certification

Hello Friends,
In this post,I would share how I prepared for AWS Certified Solutions Architect Associate
certification, which should help you as well in your preparation.

Here are the steps :

- Get a Free tier AWS account.

- Go through AWS Certified Solutions Architect Associate Official Study guide
and do hands-on practice via AWS console.

- Go through Eissa A.'s Udemy course, wherever there are gaps in understanding

- Go through Ryan's exam tips at the end of each lecture


- Take Mock tests from Jon Bonso



It took me an average of 3 hrs a day for around 2 months to prepare.
Apart from the above resources, I went through a few AWS White papers and FAQs for
Few FAQs about this certification.
Which online course should I take?

There are lots of courses available on Udemy alone. You can see the bestseller and choose
the course. I took Ryan and Eissa' courses and I think if you have quite a lot of experience
then you can go for Ryan's course because he explains things, to the point and quickly, but
if you have limited experience with AWS, I would suggest Eissa's course because he
explains things in much detail but you need to patient as well for that. For me, It was kind
of a mix, such that I went through Ryan's course completely and went through Eissa's
lectures wherever I felt there were gaps left in understanding.Also during the end stage of
my preparation, I came across youtube videos from knowledge India and those are also a
very good source.
Which mock tests should I take?
Again, there are a lot of options. I will talk about the mock tests I took.
So I chose Jon Bonso and Whizlab and I believe both are very good in terms of giving you
a feel about the real exam and testing your knowledge.

Also, I feel, the difficulty level of these mock tests is on par with the actual exam. And also
another thing I would like to mention is, these mock tests are not dumps so that you will get
questions in the exam from these dumps. These mock tests are only to help you test your
knowledge and depending upon the results of the mock tests, you can prepare on the topic(s)
where you are getting a lower score.
Are there any labs which they ask to do in the exam?
No, All questions are multiple-choice questions with four options. There can be multiple
correct answers as well to some of the questions.

Few Other helpful links :

AWS Services health dashboard link :

https://status.aws.amazon.com/

AWS Policy Simulator link :

https://policysim.aws.amazon.com/home/index.jsp?#

AWS documentation link :

https://docs.aws.amazon.com/index.html
If you have any further queries, please feel free to post your queries in the comments
section or ping me separately on LinkedIn.

How to create immutabe Map in Java


Hello Friends,

In this tutorial, we will see how we can create an immutable Map in Java.


- What does it mean by immutable class or object?
- What is an Immutable Map?
- How to create an immutable Map in java?

How to add maven Support to a java Project in IntelliJ

Hello Friends


In this quick tutorial, we will see how we can add Maven support to the java project in IntelliJ.

How to increase the number of tabs in IntelliJ Idea

Hello Friends,

This is a quick small post on increasing the tab limits in the Intellij idea.

Sorting a List having null values with Comparator's nullsFirst

Hello Friends,

In this tutorial, we will see how we can sort a list of items when few of the items are null in the list using Java 8 Comparator.nullsFirst, such that nulls are treated as smallest elements in the list.

- What is Comparator
- What is nullsFirst method doing in Comparator
- Sorting a list of Strings having non null names
- Sorting a list of Strings having names and Nulls without using nullsFirst
- Solving above problem by sorting the list using nullsFirst method
- Sorting a list of Custom objects without nulls
- Sorting a list of Custom objects with nulls without using nullsFirst
- Solving above problem by sorting the list using nullsFirst method
- Sorting the list having employee with name as null