Hi All, This Blog is all about the various challenges and tasks we perform while coding.It will cover various updates related to Spring,Struts,Hibernate,JPA,Web Services,Javascript, Bootstrap,third party integration etc
Friday, 30 December 2022
Coding trends
Monday, 3 October 2022
Java 8 Features
1)Lambda expression/function: is an anonymous function that can be passed around where anonymous means that it does not have name .It is function as it is not associated with class like method. Passed around means it can passed as an argument to a method or can be stored in a variable.
Usage ::
Used to represent the instance of the functional interface.
syntax is as follows:
(a,b)->System.out.println(a+b);
Advantage ::
No boiler plate code for simple things.
2)Functional interface: is an interface which contains only one abstract method but can have any number of default methods.
@FunctionalInteface
public interface Predicate{
boolean test(T t);
}
@FunctionalInterface
public interface Consumer {
void accept(T t);
}
@FunctionalInterface
public interface Function{
public R apply(T t);
}
@FunctionalInterface
public interface Supplier{
public T get();
}
3)Default method inside Interface :
public interface MyInterface { // regular interface methods default void defaultMethod() { // default method implementation } }
In a typical design based on abstractions, where an interface has one or multiple implementations, if one or more methods are added to the interface, all the implementations will be forced to implement them too. Otherwise, the design will just break down.
Default interface methods are an efficient way to deal with this issue. They allow us to add new methods to an interface that are automatically available in the implementations. Therefore, we don't need to modify the implementing classes.
In this way, backward compatibility is neatly preserved without having to refactor the implementers.
what happens when a class implements several interfaces that define the same default methods.
In that case, the code simply won't compile, as there's a conflict caused by multiple interface inheritance (a.k.a the Diamond Problem).
To solve this ambiguity, we must explicitly provide an implementation for the methods by overriding the implementation in class.
@Override public String turnAlarmOn() { return Vehicle.super.turnAlarmOn(); } @Override public String turnAlarmOff() { return Vehicle.super.turnAlarmOff(); }
4)static method inside Interface
5)Predicate<T> : has test method returns boolean
6)Function <T,R> : has apply method return R type after applying some business logic.
7)Consumer<T> : has accept method and it does not return anything instead can be used for iteration.
8)Supplier< > : has get Method and it returns T type object.
9)Method reference and constructor reference by using :: (double colon ) operator.
10)Streams
11)Date & Time API (Joda API)
Saturday, 1 October 2022
What is PermGen?
PermGen is memory area which was part of heap prior to Java 8. This is used to load class and method objects which means it was directly related to the number of classes objects being created. So when number of classes increases there objects can also increase and hence we use to face one issue java.lang.OutOfMemory error due to PermGen Size. We use to increase PermGen size using xx:MaxPermGen .
But there is new change in java 8 wherein this PermGen has been replaced by Metaspace which means there is no PermGen in java 8 onwards.
Old JVM memory consist of 1)Heap 2)Native memory
wherein Heap consists of
a)Old generation. b)New Generation. c)PermGen
New JVM memory consists of 1)Heap 2)Native memory
wherein Heap consists of
a)Old generation. b)New Generation
Native memory consists of
a)Metaspace
Now metaspace is controlled by the native memory which is dependent of the host .So in case many classes are loaded due to xyz reason it may happen that process size can increase immensely and by so entire server can crash not only application.
So for this we have new parameter using which we can we can limit size of metaspace given by xx::MaxMetaspaceSize.
Conclusion::
1)we need to monitor heap as well as process size now. Process size can be monitored using system utilities like top in unix/linux and Task Manager in windows.
2)Jmap can be used as follows: Jmap -permstat <PID>
3)No more PermGen space from java 8 onwards. Inclusion of metaspace.
3)QA should be made aware of this while doing testing.
Thursday, 15 September 2022
AWS Dynamo DB table using AWS CLI
1)Create Ec2 instance and get its key-pair for ssh to it .
ssh ec2user@17.38.292.1 -i ec2key.pem (Make sure key has right permission if not change it by command . chmod 400 ec2key.pem)
2)Once login can configure aws cli by running below command. Below command shall work in Amazon type Ec2.
aws configure (This works in amazon instance)
Prompt for below ::
Access key :
Secret access key:
Default region name: us-east-1
Output format: json
3)create dynamco db tables
a) After running AWS Configure, create a DynamoDB table using the following command:
aws dynamodb create-table --table-name ProductCatalog --attribute-definitions \
AttributeName=Id,AttributeType=N --key-schema \
AttributeName=Id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
b) This is the command to populate the table:
**** (make sure items.json is located in your working directory) ****
aws dynamodb batch-write-item --request-items file://items.json
where items.json contains: {
"ProductCatalog": [{
"PutRequest": {
"Item": {
"Id": {
"N": "201"
},
"ProductCategory": {
"S": "Bicycle"
},
"Description": {
"S": "Womens Road Bike"
},
"BicycleType": {
"S": "Road"
},
"Brand": {
"S": "Raleigh"
},
"Price": {
"N": "399"
},
"Color": {
"S": "Red"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "403"
},
"ProductCategory": {
"S": "Helmet"
},
"Description": {
"S": "Womens Cycling Helmet"
},
"Size": {
"S": "Small"
},
"Price": {
"S": "99"
},
"Color": {
"S": "Black"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "411"
},
"ProductCategory": {
"S": "Book"
},
"Description": {
"S": "The Read Aloud Cloud"
},
"Author": {
"S": "Forrest Brazeal"
},
"Price": {
"N": "19.99"
},
"Format": {
"S": "Hardback"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "563"
},
"ProductCategory": {
"S": "Helmet"
},
"Description": {
"S": "Mens Cycling Helmet"
},
"Size": {
"S": "Small"
},
"Price": {
"N": "75"
},
"Color": {
"S": "Blue"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "543"
},
"ProductCategory": {
"S": "Helmet"
},
"Description": {
"S": "Womens Cycling Helmet"
},
"Size": {
"S": "Medium"
},
"Price": {
"N": "199"
},
"Color": {
"S": "Red"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "493"
},
"ProductCategory": {
"S": "Helmet"
},
"Description": {
"S": "Childs Cycling Helmet"
},
"Size": {
"S": "Small"
},
"Price": {
"N": "99"
},
"Color": {
"S": "Black"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "347"
},
"ProductCategory": {
"S": "Helmet"
},
"Description": {
"S": "Womens Cycling Helmet"
},
"Size": {
"S": "Small"
},
"Price": {
"N": "79"
},
"Color": {
"S": "Blue"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "467"
},
"ProductCategory": {
"S": "Bicycle"
},
"Description": {
"S": "Mens Road Bike"
},
"BicycleType": {
"S": "Road"
},
"Brand": {
"S": "Raleigh"
},
"Price": {
"N": "250"
},
"Color": {
"S": "Blue"
}
}
}
},
{
"PutRequest": {
"Item": {
"Id": {
"N": "566"
},
"ProductCategory": {
"S": "Bicycle"
},
"Description": {
"S": "Mens Mountain Bike"
},
"BicycleType": {
"S": "Mountain"
},
"Brand": {
"S": "Raleigh"
},
"Price": {
"N": "599"
},
"Color": {
"S": "Black"
}
}
}
}
]
}
Response:
{
"UnprocessedItems":{}
}
c) This is the command to query Dynamodb from EC2 command line - make sure the region is correct. You should be working in us-east-1.
aws dynamodb get-item --table-name ProductCatalog --region us-east-1 --key '{"Id": {"N":"403"}}'
Tuesday, 13 September 2022
MCQ for AWS Developer Associate Exam
Monday, 12 September 2022
Amazon SQS
SQS Delay Queues: is used to postpone the delivery of messages to the consumer from 0 sec to 15 mins which is maximum. In this q messages will be invisible for delayed duration and if any consumer wants to consume message they wont see any message till that time.
Delay Queue is not supported for FIFO queues in case of per message. This is not allowed may be because by doing so order will not be maintained.
Difference between Visibility Timeout and Delay Queues:
1)for delay queues, a message is hidden when it is first added to queue, whereas for visibility timeouts a message is hidden only after it is consumed from the queue.
2)Secondly visibility Timeout is mainly used for problematic messages and in case there is any issue with message processing by the consumer , message automatically gets added on the queue. For such scenarios we can even move those erroneous messages to Dead letter Q.
Use case of SQS Delay queue: When we want delay in processing of messages like in case there is some rate limit on the consumer side and we want give some buffer so that we can process once we have some free resources etc.
Takeaways:
1)Visibility timeout range is 0 seconds and 12 hours.
2)Message that can be transferred in SQS queues can have maximum payload of size 256KB.
3)In order to increase payload or video streaming etc SQS extended client lib can be used . In this that make use of S3 and SQS where S3 has actual data and SQS has message which stores link to S3.
4)Visibility time can be set on queue basis or per message basis .
5)Dead letter Queue is good candidate for processing failed scenarios or messages.
6)Delay Q has delay-seconds set which can range from 0 to 15 minutes.
SQS Dead Letter Queue: used to handle problematic messages.It makes use of redrive policy wherein we define source queue and maxReceiveCount . In this when receiveCount> maxReceiveCount then message is moved to Dead letter Queue if its not deleted. Later we can have consumer which can send notification or can have diagnosis.
Enqueue timestamp is the time when queue was queued in normal SQS queue but remember when that message is moved to Dead letter queue that enqueue timestamp is same which means deadLetter Q can have lower retention period in order to process that particular message. So we shall cautious for the expiry fo the message when message is moved to Dead Letter Queue.
Saturday, 25 June 2022
DES(Data Encryption Key) AWS
DEKs (Data encryption keys AWS)
1)DEKs: stands for Data encryption keys in AWS.
2)KMS(Key management service ) does not store DEKs in any case.
3)Used for data size > 4KB
4)KMS is regional service.
How does DEKs gets generated and used??
KMS generates 2 key version of DEKs mentioned below:
a)Plain text key : This key is used to encrypt data and once data is encrypted .It is discarded immediately.
b)Cipher Text Key : This key is ciphertext of key (a) and used later when data need to be decrypted.
Summary::
Encryption process: DEKs generates 2 keys and one is plain text and second one is encrypted key . Plain text is used to encrypt data and once data is encrypted , plain key is discarded. Now when data need decryption we require plain key for decryption. So this plain key we can ask KMS to return by passing encrypted key and once we have plain text key we can do decryption directly.
S3 uses
CMK at isolated at region level and never leave. Means CMK is not global nor alias are global. We can similar alias in different region but they can point to different stuff.
There are two types of keys
a)AWS managed :: Less flexible. Compulsory key rotation.
b)Customer Managed keys:: More flexible. More policy. Rotation is optional.
CMK has key policy which is resource policy.
Tuesday, 17 May 2022
OpenShift Jenkins Pipeline.
Various object present in OpenShift plugin to make Jenkins pipeline work in openshift container.
openshiftBuild,
openshiftCreateResource,
openshiftDeleteResourceByJsonYaml,
openshiftDeleteResourceByKey,
openshiftDeleteResourceByLabels,
openshiftDeploy, openshiftExec,
openshiftImageStream, openshiftScale,
openshiftTag, openshiftVerifyBuild,
openshiftVerifyDeployment, openshiftVerifyService,
parallel, podTemplate,
powershell,
properties,
publishBuildInfo,
publishHTML,
pwd,
readFile,
readJSON,
readManifest,
readMavenPom,
readProperties,
readTrusted,
readYaml,
resolveScm,
retry,
script,
sh,
sha1,
slackSend,
sleep, stage, stash, step, svn, tee, timeout, tm, tool, touch, unarchive, unstash, unzip, validateDeclarativePipeline, waitUntil, withContext, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, wrap, writeFile, writeJSON, writeMavenPom, writeYaml, ws, xrayScanBuild, zip] or symbols [all, allOf, always, any, anyOf, apiToken, architecture, archiveArtifacts, artifactManager, authorizationMatrix, batchFile, bitbucket, booleanParam, branch, buildButton, buildDiscarder, buildParameter, buildingTag, caseInsensitive, caseSensitive, certificate, changeRequest, changelog, changeset, checkoutToSubdirectory, choice, choiceParam, cleanWs, clock, cloud, cobertura, command, configFile, configFileProvider, configMapVolume, containerEnvVar, containerLivenessProbe, containerTemplate, copyArtifacts, credentials, cron, crumb, default, defaultView, demand, disableConcurrentBuilds, disableResume, docker, dockerCert, dockerfile, downloadSettings, downstream, dumb, durabilityHint, emptyDirVolume, emptyDirWorkspaceVolume, envVar, envVars, environment, equals, expression, file, fileParam, filePath, fingerprint, frameOptions, freeStyle, freeStyleJob, fromScm, fromSource, git, github, githubPush, globalConfigFiles, headRegexFilter, headWildcardFilter, hostPathVolume, hostPathWorkspaceVolume, hyperlink, hyperlinkToModels, inheriting, inheritingGlobal, installSource, isRestartedRun, jdk, jgit, jgitapache, jnlp, jobDsl, jobName, kubernetes, label, lastCompleted, lastDuration, lastFailure, lastGrantedAuthorities, lastStable, lastSuccess, lastSuccessful, latestSavedBuild, legacy, legacySCM, list, local, location, logRotator, loggedInUsersCanDoAnything, masterBuild, maven, maven3Mojos, mavenErrors, mavenMojos, mavenWarnings, modernSCM, myView, never, newContainerPerStage, nfsVolume, nfsWorkspaceVolume, node, nodeProperties, nonInheriting, nonStoredPasswordParam, none, not, oc, onFailure, overrideIndexTriggers, paneStatus, parallelsAlwaysFailFast, parameters, password, pattern, permalink, permanent, persistentVolumeClaim, persistentVolumeClaimWorkspaceVolume, pipeline-model, pipelineTriggers, plainText, plugin, podAnnotation, podEnvVar, pollSCM, portMapping, preserveStashes, projectNamingStrategy, proxy, queueItemAuthenticator, quietPeriod, remotingCLI, run, runParam, schedule, scmRetryCount, scriptApprovalLink, search, secretEnvVar, secretVolume, security, shell, skipDefaultCheckout, skipStagesAfterUnstable, slave, sourceRegexFilter, sourceWildcardFilter, specific, sshUserPrivateKey, stackTrace, standard, status, string, stringParam, swapSpace, tag, text, textParam, tmpSpace, toolLocation, triggeredBy, unsecured, upstream, userSeed, usernameColonPassword, usernamePassword, viewsTabBar, weather, workspace, zfs, zip] or globals [Artifactory, currentBuild, docker, env, fileLoader, openshift, params, pipeline, scm]
Tuesday, 26 April 2022
GIT Commands
1)git config --get remote.origin.url : To know remote github branch url.
2)git rebase and squash : These are used to club multiple commits into one to make PR clean.
3)git stash : to save uncommitted change.
4)git stash apply : to restore changes which were stashed.
5)git switch branch-1 : to switch branch
6)git commit -m "Commit message"
7)git push origin branchName : push changes to branch
8)git diff filename fileName
9)git status:
10)git restore filename
11)git remote -v: will give remote details
12)git reset --soft HEAD^: To uncommit your changes in local branch in case you commit something by mistake.
13) git switch master : To switch to any branch
14)git branch -a : list all branches
15)git checkout branchname
Wednesday, 16 March 2022
Friday, 11 February 2022
Different services running on MAC or not.
Check below services are running or not on MAC.
1)Mongodb
ps -ef | grep mongod | grep -v grep | wc -l | tr -d ' '
This will get you the number of MongoDB
processes running, thus if it is other than 0
, then you have MongoDB
running on your system.
2)Elasticsearch
Tuesday, 25 January 2022
Collection Classes and Interfaces in Java
Monday, 24 January 2022
ClassNotFoundException Vs NoClassDefFoundError
ClassNotFoundException : This is checked exception and occurs when class is not found at runtime.The reason for this could be classpath is not updated with required jars.
// Java Program to Illustrate ClassNotFoundException
public class Demo {
// Main driver method
public static void main(String args[])
{
// Try block to check for exceptions
try {
Class.forName("ApachePOI");
}
// Catch block to handle exceptions
catch (ClassNotFoundException ex) {
// Displaying exceptions on console along with
// line number using printStackTrace() method
ex.printStackTrace();
}
}
}
NoClassDefFoundError: This is unchecked exception and occurs when class was present during compile time but absent during runtime. This generally happens when no .class file is present at runtime.This is kind of LinkageError.
Comparable and Comparator Interface
Comparable interface is used to sort or order the elements on the basis of single field. Using this we can only sort using single field. Comparable interface is present in java.lang.comparable package.
package java.lang;
public interface Comparable<T> {
int compareTo(T var1);
}
Comparator interface is used to sort or order the elements on the basis of multiple fields. Using this we can create different comparator and sort elements on the basis of different fields. Comparator interface is present in java.util package.
package java.util;
import java.io.Serializable;
import java.util.Comparators.NaturalOrderComparator;
import java.util.Comparators.NullComparator;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
@FunctionalInterface
public interface Comparator<T> {
int compare(T var1, T var2);
boolean equals(Object var1);
Observe that Comparator is marked functional interface . It may be because all classes which implement Comparator interface by default will have equals implementation from default super class (Object).
Checked and Unchecked Exception
Checked Exceptions are those which are checked by compiler during compile time only. These need to handled either by try catch block or by throwing the exception. Examples of same are ::
ClassNotFoundException, InterruptedException, InstantiationException, IOException,
SQLException, IllegalAccessException, FileNotFoundException,CloneNotSupportedException.
All exceptions except RunTimeException and Error are checked Exception.
In case of checked exception Compiler reports unhandled exception FileNotFoundException or must be caught or declared to be thrown during compiletime.
Unchecked Exceptions are those which are not checked by compiler and occurs due to error in programming logic. Examples ClassCastException extends RunTimeException, ClassGenException extends RunTimeException ,ArithmeticException,IndexOutOfBoundsException extends RunTimeException, ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException, ConcurrentModification Exception, NumberFormatException.
All exceptions under (RuntimeException and Error ) are unchecked Exception.
Exception hierarchy
Object
|
Throwable
|
Exceptions Error
| |
I/O. Runtime CloneNotSupported. Linkage Assertion VirtualMachineError
SOLID principle
S: S stands for single responsibility principle.Each class is responsible for single responsibility.
O: Class shall be open for extension but closed for modification.
L: L stands for Liskov substitution.This means that objects of superclass shall be replaced with objects of subclasses without breaking the system.
I: I stands for Interface segregation Principle. This means that Interface shall not have unnecessary methods which need to be implemented by client forcefully. So if there are methods which are not always required by clients so better would be to keep them in separate interface and the clients which want those can override those methods by implementing different interface.
D: D stands for dependency inversion principle.It states that class shall depend on abstract reference(abstract class or interface) rather than concrete implementation(class) . Here , inversion refers to the dependent classes on which another class depends. In simple terms we shall use abstract classes or interface as dependency to one class not concrete classes.
For eg
Class Car{
Engine engine// This Engine either has to be abstract class or interface.
}
Monday, 10 January 2022
Generics in Java
Generics means parameterised types. With the help of parameterised types we can create classes, interfaces or methods in such a way that the type of the data on which they operate can be passed as a parameter.
Generic only works with object.
Main advantage of generic
a)Generic provide Type Safety :: This means that error can be detected during compile time only instead of runtime .
b)Generic provide Code Reusability :: Since with generic we just need to create class once and we can use it for any type .
c)Type casting is not needed.
d)Helpful for making generic implementation.
Generic class creation: It is very similar to normal class except that the declaration is followed by type parameter with diamond bracket.
Class Dao<T>{//T is generic type parameter
private T t; //instance of generic type
}
Class Matrix<T,S>{
private T t;
private S r;
}
By convention ,type parameter are named as single uppercase letter. Commonly used Type parameter are:
* T : First typed parameter
* S. : Second type parameter
* U : Third type parameter
* V : Fourth type parameter
* K : Key type parameter
Bounded Type::
Bounded type is used to restrict type to a specific type or its subtype. In short in many cases we want the type to be restricted to specific types like in case of sum we want that type should be restricted to Number or its type then in that scenario we can use Bounded type.
Bounded types are divided on 3 types based on wildcard:
1)Upper Bound Type :: This is implemented using ?(unknown type) wildcard followed by extends keyword and used to relax the restriction on any type which means we can pass any type which extends Type T.
public void addNumsDifferentTypes(List<? extends Number> list>{
}
2)Lower Bounded Type :: This is implemented using ? (unknown type) followed by super keyword and used to allow specific type passed in parameters. This means we can pass only pass or return type of specific type or it super type while using it.
public void passOnlyIntegerOrNumber(List<? super Integer> list){}
3)Unbounded Type:: This is implemented using ? (unknown type) and is used to allow pass or return any type.
public void printList(List <?> list){}