Home » Articles posted by All In One Tutorial (Page 2)

Author Archives: All In One Tutorial

HazelCast Management Centre

HazelCast Management Center :

1. Hazelcast Management Center is a tool or User interface which allows you to monitor and manage your cluster
members running Hazelcast IMDG.
2. It helps you to analyze and browse data structures like used IMap,IQueue configurations.
3. It helps you to take thread dumps from members.
4. It helps you to analyze heap usage.
5. It comes with free trial version and also with license version with some extra modules.
5. It has SSL enabled or disable feature.
6. It helps you to run JavaScript,groovy scripts,commands on your members with its scripting and console modules.

Step 1 :
Download Management Center from official HazelCastIMDG Download Link
I have downloaded Management Center 3.10.1
Step 2:
Start management center locally by clicking on start.bat.After this you should see “Hazelcast management center successfully started” in command prompt console.

Step 3 :
Use below pom.xml.Please note here that management-centre version and “hazelcast-all” jar version should match.otherwise you will get error in connection to management center.

Step 4 :
Create a class similar to HazleCastClusterMain.java and enable hazlecast management center and provide its url as well.Use the config to create HazelcastInstance.

OutPut ::

Step 5 : Register first time using a user id and password.

Step 6 : Login again to management center with same user id and password .The URL where you are login should be used in your application HazelcastInstance management center config.

Step 7 : Now you can see your cluster data and monitor it.Enjoy !!!!!

JsonSchemaToPojoGeneration

How to Generate POJOs from json schema File :
In this post I have described the simplest and best way to generate POJOs or model classes from a JSON schema file.
Step 1 :
Place the schema file in resource folder of the project.
Here I have kept the schema file in src/main/resources/schema folder.

Sample Schema File :
Employee.json

Step 2 :
Prepare pom.xml
a. Add “jsonschema2pojo-maven-plugin” plugin.
b. Provide json schema directory path in “sourceDirectory”.
c. Provide output directory path in “outputDirectory” where you want to generate the POJOs.
d. Provide the package name for the POJOs in “targetPackage”.
e. Add commons-lang jar to provide hashCode() and equals() implementation of POJOs.
f. Add jackson-annotations to provide jackson annotations for POJOs generated from JSON schema.
g. I have also used “generateBuilders” just to follow builder pattern in POJOs.If you do not require it ,do not use it.
pom.xml :

Step 3 :
Build the application using maven

Step 4 :
Output is generated now

Java8-ForEach

In this post we are going to code forEach() and forEachOrdered() examples. forEach() method is used to iterate over a collection like List,Set,Map etc or a stream.forEachOrdered() method process the elements in the order the elements are added to collection.

Syntax ::

Program ::
In the below Program I have used three methods to use forEach() over List,Set and Map.I have used the same method to show forEachOrdered() on List and Set using stream.

Output ::

Design Patterns

Abstract Factory Design Pattern

Abstract Factory Design Pattern ::Abstract Factory design pattern is also called as factory of factories. This type of design pattern comes under creational design pattern as this pattern provides way to select a factory and then create an instance of related classes of that factory.
 
Real Time Example :: I am going to have two factories where one factory will have VideoPlayer and its implementation classes and another factory will have AudioPlayer and and its implementation classes.Based on user input a factory will be selected and then an instance will be created of related classed of that factory.

 

UML Diagram ::

 

 

Step 1 :
Create an Interface VideoPlayer .java and declare method playVideo()

Step 2 :
Create a class VlcMediaPlayer.java which implements interface VideoPlayer.java and has its own implementation for method playVideo()

Step 3 :
Create a class WindowsMediaPlayer.java which implements interface VideoPlayer.java and has its own implementation for method playVideo()

Step 4 :
Create a class AdobeFlashPlayer.java which implements interface VideoPlayer.java and has its own implementation for method playVideo()

Step 5 :
Create an Interface AudioPlayer.java and declare method playSong()

Step 6 :
Create a class WinAmpPlayer .java which implements interface AudioPlayer.java and has its own implementation for method playSong()

Step 7 :
Create a class MP3Player.java which implements interface AudioPlayer.java and has its own implementation for method playSong()

Step 8 :
Create a class JetAudioHDPlayer.java which implements interface AudioPlayer.java and has its own implementation for method playSong()

Step 9:
Create an abstract class AbstractFactory.java which has two abstract methods getVideoPlayer() and getAudioPlayer() which returns VideoPlayer and AudioPlayer type instances respectively.

Step 10 :
Create a class VideoPlayerFactory .java which extends AbstractFactory class and returns corresponding VideoPlayer type instance based on user input.

Step 12 :
create a class AudioPlayerFactory.java which extends AbstractFactory class and returns corresponding AudioPlayer type instance based on user input.

Step 13 :
Create class SelectFactory.java which has public method getFactory() that returns corresponding VideoPlayerFactory or AudioPlayerFactory type instance based on user input.

Step 14 :
Create class AbstractFactoryDesignPatternDemo.java and select factory and then corresponding class object of that factory.

Output ::
Playing your favourite video using VLC MediaPlayer
Playing your favourite video using Windows MediaPlayer
Playing your favourite video using Adobe Flash Player

Playing your favourite song using WinAmp Player
Playing your favourite song using MP3 Player
Playing your favourite song using JetAudioHD Player

Observer Design Pattern

Observer Design Pattern :: Observer Design Pattern comes under behvioral design pattern.In this design pattern an observer is notified if any changes occur to an object which the observer is subscribed to.

Real Time Example :: Suppose there are serveral readers.One category of readers like to read Java Topics and other set of readers like to read Spring Topics .So these readers(Observers) first subscribes to corresponding SubjectTopic(Subjects).Once any changes to Subject, new articles added to topic and any subscriber is added/removed then if reader/subscriber opts for notification he/she can get the notification about the same.

 

UML Diagram ::

 

Step 1 :
Create an Interface Subject.java and declare methods subscribeObsever(Observer observer),unSubscribeObsever(Observer observer) and notifyAllObsevers() .
subscribeObsever() method adds an Observer/Subscriber.
unSubscribeObsever method removes an Observer/Subscriber.
notifyAllObsevers() method notify all Observers/Subscribers currently subscribed for the Subject.

Step 2 :
Create an Interface Observer.java and declare methods updateObserver() .
updateObserver() method updates an Observer/Subscriber if any changes happen to Subject.
I have also optionally added getName() method to get details about Observer/Subscriber.

Step 3 :
Create a class TopicEnrollment.java which implements interface Subject.java and has its own implementation for methods subscribeObsever(Observer observer),unSubscribeObsever(Observer observer) and notifyAllObsevers() .

Step 4 :
Create a class Subsrciber.java which implements interface Observer.java and has its own implementation for methods updateObserver()nd getName()
I also have an instance of Subject here ,so that a Subscriber can subscribe to corresponding Subject.I am doing the same in the constructor.

Step 5 :
Create main class ObserverDesignPatternDemo.java.I have created two topics “java-8” and “Spring-5” to demo Observer design pattern.

<


Output ::

Factory Design Pattern

Factory Design Pattern :: Factory Design Pattern comes under creation design pattern.As the name suggests,basically using these design pattern you can create an instance of class from a factory of related classes.

Real Time Example :: I am going to have a factory of MediaPlayer classes and each class has the functionality to play song.Based on user input ,user will select a particular type media player and will play song using that media player only.

 

UML Diagram ::

 

 

Step 1 :
Create an Interface MediaPlayer.java and declare method playSong()

Step 2 :
Create a class VlcMediaPlayer.java which implements interface MediaPlayer.java and has its own implementation for method playSong()

Step 3 :
Create a class WindowsMediaPlayer.java which implements interface MediaPlayer.java and has its own implementation for method playSong()

Step 4 :
Create a class AdobeFlashPlayer.java which implements interface MediaPlayer.java and has its own implementation for method playSong()

Step 5 :
Create a class MediaPlayerFactory.java and expose a public method which accepts an input and create instance of the corresponding class based on input.

Step 6 :
Create Main class FactoryDesignPatternDemo.java and create MediaPlayerFactory instance. Pass input tho the public method of MediaPlayerFactory class and get corresponding instance.


Output ::

Swagger

Swagger Overview ::
1. Swagger is an open source software framework backed by a large ecosystem of tools that helps developers design, build, document, and consume RESTful Web services.
2. The Swagger API project was created in 2011 by Tony Tam, Technical co-founder of the dictionary site Wordnik.
3. While most users identify Swagger by the Swagger UI tool, the Swagger toolset includes support for automated documentation, code generation, and test case generation.

What is Swagger UI tool ::
1. Swagger UI is an open source project to visually render documentation for an API defined with the OpenAPI (Swagger) Specification.
2. Using SWagger UI ,One can see all the restful services ,request url,request body and response body.
3. Swagger UI is so simple to use.It can be used by Developer,Tester,Business Analyst,DevOps etc.
4. Swagger UI is very helpful in Development and Testing phase of a project .
5. Swagger UI is the preferred tool now a days as it is used for developing APIs,interacting with APIs and documenting APIs

Adavatages/Usage of Swagger ::
1. Swagger open source tool can be used for developing APIS. It can generate open API document based on code.Swagger follows bottom-up or code-first API Development.API description,sample input is automated using Swagger Tool.
2. Swagger open source tool can be used for interacting with APIs.Using the swagger-codegen project, end users generate client SDKs directly from the Open API document, reducing the need for manual client coding done by developer.
3. Swagger open source tool can be used for documenting APIs.Rest requests can be made directly from the swagger UI .Using swagger UI ,the swagger tool project allows direct interaction with API with HTML-based user interface.Request Body can be manipulated and Response body can be seen in the same UI.Swagger also shows you the error and error description,if any error occurs.

Let us  code a program and see the how to use swagger.
References ::

Poperties Class in Java

Poperties Class in Java :: 
1.  Properties class is present in java.util package.
2.  Properties class extends java.util.Hashtable class.
3.  Property class is used to store a set of properties.These properties can be saved to a stream or file.These properties can be loaded from a stream or file.
4.  Properties class is a subclass of HashTable,it stores the properties in key value format.
5.  Each key and its corresponding value in the property list is a string.
6.  Exception is not thrown if property is not present.
7.  A property list can also contain another property list.The second property list is only searched if the property key is not found in the first property list.
8. By default the UTF-8 character encoding is used for reading from and writing to a property file, however a specific encoding may be specified if required

Method used in Properties class ::

Method Description
public String getProperty(String key) Returns value based on the input key.
public void setProperty(String key,String value) sets the key value pair property in the properties object.
public void load(Reader r) Loads property data of the Property file from the Reader object.
public void load(InputStream is) Loads property data of the Property file from the InputStream object
public void store(Writer w, String comment) Writes property data to the Property file using Writer object.
public void store(OutputStream os, String comment) Writes property data to the Property file using OutputStream object.
storeToXML(OutputStream os, String comment) Writes property data to the Property file using OutputStream object for generating xml document.
public void storeToXML(Writer w, String comment, String encoding) Writes property data to the Property file using Writer object.for generating xml document with specified encoding.

How to read a property file and How To Write to a Property File .To know the answer click here.

How to read from and write to a property file in Java

In this post I am going to code two simple programs for reading from and writing to a property file in Java.
If you want to know what is property file and what are its some of the useful methods you can see here.

1. Write a Program to read a Property File in Java ?

Output ::

2. Write a Program to write to a Property File in Java ?

Output ::