tag:blogger.com,1999:blog-26317090911849918372024-03-18T08:33:41.216+05:30Program in Java - Java Examples, Interview Questions and AnswersLearn Java programming like a pro with the help of our simplified tutorials, examples and frequently asked Java interview questions and answers. Java tutorial for beginners and professional java developers!Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.comBlogger111125tag:blogger.com,1999:blog-2631709091184991837.post-84198012178415967992021-09-10T19:27:00.003+05:302021-09-10T19:28:12.332+05:30What is Java Virtual Machine (JVM) & Its Architecture
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!--InPostArticle-Ads-->
<h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 0px 0px 10px;">What is JVM?</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">JVM is a engine that provides runtime environment to drive the Java Code or applications. It converts Java bytecode into machines language. JVM is a part of JRE(Java Run Environment). It stands for Java Virtual Machine</p><ul style="background-color: white; clear: both; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin: 1em 0px 1em 1.25em; padding: 0px 0px 0px 1.25em;"><li>In other programming languages, the compiler produces machine code for a particular system. However, Java compiler produces code for a Virtual Machine known as Java Virtual Machine.</li><li>First, Java code is complied into bytecode. This bytecode gets interpreted on different machines</li><li>Between host system and Java source, Bytecode is an intermediary language.</li><li>JVM is responsible for allocating memory space.</li></ul><div><span style="color: #222222; font-family: Arial, sans-serif;"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-bgB5eIlWF7k/YTtib0rpfRI/AAAAAAAAIwM/v4AEBZmXLSArk6TrEL-v4ik3JrB78u70QCLcBGAsYHQ/s615/image1.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="109" data-original-width="615" height="71" src="https://1.bp.blogspot.com/-bgB5eIlWF7k/YTtib0rpfRI/AAAAAAAAIwM/v4AEBZmXLSArk6TrEL-v4ik3JrB78u70QCLcBGAsYHQ/w400-h71/image1.jpeg" width="400" /></a></div><br /></span><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">In this tutorial, you will learn-</p><ul style="background-color: white; clear: both; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin: 1em 0px 1em 1.25em; padding: 0px 0px 0px 1.25em;"><li>What is JVM?</li><li>JVM Architecture</li><li>Software Code Compilation & Execution process</li><li>C code Compilation and Execution process</li><li>Java code Compilation and Execution process</li><li>Why is Java both interpreted and Compiled Language?</li><li>Why is Java slow?</li></ul><h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 30px 0px 10px;">JVM Architecture</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">Let’s understand the Architecture of JVM. It contains classloader, memory area, execution engine etc.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-80NQEbd9b_8/YTtiq68M5_I/AAAAAAAAIwQ/mh9MwjpHhvgSao8WkHLly8AbfKigCMengCLcBGAsYHQ/s913/image2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="596" data-original-width="913" height="418" src="https://1.bp.blogspot.com/-80NQEbd9b_8/YTtiq68M5_I/AAAAAAAAIwQ/mh9MwjpHhvgSao8WkHLly8AbfKigCMengCLcBGAsYHQ/w640-h418/image2.png" width="640" /></a></div><br /><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;"><br /></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">1) ClassLoader</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading, Linking, and Initialization.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">2) Method Area</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">JVM Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">3) Heap</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">4) JVM language Stacks</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created simultaneously as the thread is created. A new frame is created whenever a method is invoked, and it is deleted when method invocation process is complete.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">5) PC Registers</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">PC register store the address of the Java virtual machine instruction which is currently executing. In Java, each thread has its separate PC register.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">6) Native Method Stacks</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">Native method stacks hold the instruction of native code depends on the native library. It is written in another language instead of Java.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">7) Execution Engine</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">It is a type of software used to test hardware, software, or complete systems. The test execution engine never carries any information about the tested product.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">8) Native Method interface</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">9) Native Method Libraries</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the Execution Engine.</p><h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 30px 0px 10px;">Software Code Compilation & Execution process</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">In order to write and execute a software program, you need the following</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">1) Editor</span> – To type your program into, a notepad could be used for this</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">2) Compiler</span> – To convert your high language program into native machine code</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">3) Linker</span> – To combine different program files reference in your main program together.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">4) Loader</span> – To load the files from your secondary storage device like Hard Disk, Flash Drive, CD into RAM for execution. The loading is automatically done when you execute your code.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">5) Execution</span> – Actual execution of the code which is handled by your OS & processor.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">With this background, refer the following video & learn the working and architecture of the Java Virtual Machine.</p><h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 30px 0px 10px;">C code Compilation and Execution process</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">To understand the Java compiling process in Java. Let’s first take a quick look to compiling and linking process in C.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">Suppose in the main, you have called two function f1 and f2. The main function is stored in file a1.c.</p><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-5T9NQvGDMsI/YTti57xgzVI/AAAAAAAAIwY/z0UBfscgfbUlvq-0FnADFKpJiYAVaibnwCLcBGAsYHQ/s442/image3.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="283" data-original-width="442" height="256" src="https://1.bp.blogspot.com/-5T9NQvGDMsI/YTti57xgzVI/AAAAAAAAIwY/z0UBfscgfbUlvq-0FnADFKpJiYAVaibnwCLcBGAsYHQ/w400-h256/image3.jpeg" width="400" /></a></div><br /><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-size: 15.008px;">Function f1 is stored in a file a2.c</span></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-size: 15.008px;"></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-UTUQd25Q0uA/YTtjG59sWwI/AAAAAAAAIwg/gy_zHKdJEc0zMOzXrRkAuyfE_kner9KegCLcBGAsYHQ/image4.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="418" data-original-width="264" height="400" src="https://lh3.googleusercontent.com/-UTUQd25Q0uA/YTtjG59sWwI/AAAAAAAAIwg/gy_zHKdJEc0zMOzXrRkAuyfE_kner9KegCLcBGAsYHQ/w253-h400/image4.jpeg" width="253" /></a></div><br /><span style="font-size: 15.008px;">Function f2 is stored in a file a3.c</span><p></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-size: 15.008px;"></span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-XV7ECeF7kMQ/YTtjRwyS-zI/AAAAAAAAIwo/qw9CJtlFrnAEhNdUIEo0nzBuZ3quf4g_gCLcBGAsYHQ/image5.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="616" data-original-width="244" height="400" src="https://lh3.googleusercontent.com/-XV7ECeF7kMQ/YTtjRwyS-zI/AAAAAAAAIwo/qw9CJtlFrnAEhNdUIEo0nzBuZ3quf4g_gCLcBGAsYHQ/w158-h400/image5.jpeg" width="158" /></a></div><br /><p></p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">All these files, i.e., a1.c, a2.c, and a3.c, is fed to the compiler. Whose output is the corresponding object files which are the machine code.</p></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-ZIkYDfibYC8/YTtjaW4xmrI/AAAAAAAAIww/kl761RilX40qkJelpqWeqthHB_6Ap4jXQCLcBGAsYHQ/image6.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="380" data-original-width="650" height="234" src="https://lh3.googleusercontent.com/-ZIkYDfibYC8/YTtjaW4xmrI/AAAAAAAAIww/kl761RilX40qkJelpqWeqthHB_6Ap4jXQCLcBGAsYHQ/w400-h234/image6.jpeg" width="400" /></a></div><br /><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">The next step is integrating all these object files into a single .exe file with the help of linker. The linker will club all these files together and produces the .exe file.</span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-yvtuy-kOee4/YTtjnAWl0kI/AAAAAAAAIw4/wRbWBVoaZ4EzCO7LBw-SFFaQbovrd1K6wCLcBGAsYHQ/8335b732b585759447af81e46a8276987cd6c484.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="344" data-original-width="700" height="157" src="https://lh3.googleusercontent.com/-yvtuy-kOee4/YTtjnAWl0kI/AAAAAAAAIw4/wRbWBVoaZ4EzCO7LBw-SFFaQbovrd1K6wCLcBGAsYHQ/8335b732b585759447af81e46a8276987cd6c484.jpeg" width="320" /></a></div><br /></span><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">During program run, a loader program will load a.exe into the RAM for the execution.</span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-tVdRpoThkio/YTtjwIx3tQI/AAAAAAAAIxA/pfpboRou6WI6VhbDqLOjXrMyPlY2lJwBQCLcBGAsYHQ/2a0cd347e0b47043795def13ad7c5df48ffd29b7.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="317" data-original-width="400" height="317" src="https://lh3.googleusercontent.com/-tVdRpoThkio/YTtjwIx3tQI/AAAAAAAAIxA/pfpboRou6WI6VhbDqLOjXrMyPlY2lJwBQCLcBGAsYHQ/w400-h317/2a0cd347e0b47043795def13ad7c5df48ffd29b7.jpeg" width="400" /></a></div><br /><br /></span></div><div><h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 30px 0px 10px;">Java code Compilation and Execution in Java VM</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">Let’s look at the process for JAVA. In your main, you have two methods f1 and f2.</p><ul style="background-color: white; clear: both; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin: 1em 0px 1em 1.25em; padding: 0px 0px 0px 1.25em;"><li>The main method is stored in file a1.java</li><li>f1 is stored in a file as a2.java</li><li>f2 is stored in a file as a3.java</li></ul><div><span style="color: #222222; font-family: Arial, sans-serif;"><span style="font-size: 15.008px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-DIeDvB5mCak/YTtj3o91KsI/AAAAAAAAIxI/TI5zY-p6-WQ8ow3Arhit0W27wVr_LiMNwCLcBGAsYHQ/6ed151b708a590eb531aa8e293561c296317834d.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="373" data-original-width="500" height="478" src="https://lh3.googleusercontent.com/-DIeDvB5mCak/YTtj3o91KsI/AAAAAAAAIxI/TI5zY-p6-WQ8ow3Arhit0W27wVr_LiMNwCLcBGAsYHQ/w640-h478/6ed151b708a590eb531aa8e293561c296317834d.jpeg" width="640" /></a></div><br /></span></span><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">The compiler will compile the three files and produces 3 corresponding .class file which consists of BYTE code. <span style="font-weight: bolder;">Unlike C, no linking is done</span> .</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">The Java VM or Java Virtual Machine resides on the RAM. During execution, using the class loader the class files are brought on the RAM. The BYTE code is verified for any security breaches.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"></p><div class="lightbox-wrapper" style="background-color: white; color: #222222; display: inline-block; font-family: Arial, sans-serif; font-size: 15.008px; outline: 0px;"></div></div></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-jztJpGBaMl8/YTtj8h2MmWI/AAAAAAAAIxM/gRoXFg0f5ms4hVUymLp-2w5M4fdstO9LgCLcBGAsYHQ/c608a462ebc36b8fb0f9449f46dda906a7a72d01.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="388" data-original-width="700" height="354" src="https://lh3.googleusercontent.com/-jztJpGBaMl8/YTtj8h2MmWI/AAAAAAAAIxM/gRoXFg0f5ms4hVUymLp-2w5M4fdstO9LgCLcBGAsYHQ/w640-h354/c608a462ebc36b8fb0f9449f46dda906a7a72d01.jpeg" width="640" /></a></div><br /></span><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">Next, the execution engine will convert the Bytecode into Native machine code. This is just in time compiling. It is one of the main reason why Java is comparatively slow.</span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><div class="separator" style="clear: both; text-align: center;"><a href="https://lh3.googleusercontent.com/-2pBgT13SV48/YTtkFq2orPI/AAAAAAAAIxU/3gRml74tgzAibpquNMCR4TIlF7MiHnPpwCLcBGAsYHQ/12c459993a4be340ab7cc0e31d659ff61f412b75.jpeg" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="354" data-original-width="563" height="402" src="https://lh3.googleusercontent.com/-2pBgT13SV48/YTtkFq2orPI/AAAAAAAAIxU/3gRml74tgzAibpquNMCR4TIlF7MiHnPpwCLcBGAsYHQ/w640-h402/12c459993a4be340ab7cc0e31d659ff61f412b75.jpeg" width="640" /></a></div><br /></span><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">NOTE:</span> <span style="font-weight: bolder;">JIT</span> or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It interprets part of the Byte Code that has similar functionality at the same time.</p><h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 30px 0px 10px;">Why is Java both Interpreted and Compiled Language?</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">Programming languages are classified as</p><ul style="background-color: white; clear: both; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin: 1em 0px 1em 1.25em; padding: 0px 0px 0px 1.25em;"><li>Higher Level Language Ex. C++, Java</li><li>Middle-Level Languages Ex. C</li><li>Low-Level Language Ex Assembly</li><li>finally the lowest level as the Machine Language.</li></ul><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">A <span style="font-weight: bolder;">compiler</span> is a program which converts a program from one level of language to another. Example conversion of C++ program into machine code.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">The java compiler converts high-level java code into bytecode (which is also a type of machine code).</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">An <span style="font-weight: bolder;">interpreter</span> is a program which converts a program at one level to another programming language at the <span style="font-weight: bolder;">same level.</span> Example conversion of Java program into C++</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">In Java, the Just In Time Code generator converts the bytecode into the native machine code which are at the same programming levels.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">Hence, Java is both compiled as well as interpreted language.</p><h2 style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: var(--font-up-2-rem); line-height: var(--line-height-medium); margin: 30px 0px 10px;">Why is Java slow?</h2><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin-top: 0px;">The two main reasons behind the slowness of Java are</p><ol style="background-color: white; clear: both; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><li><span style="font-weight: bolder;">Dynamic Linking:</span> Unlike C, linking is done at run-time, every time the program is run in Java.</li><li><span style="font-weight: bolder;">Run-time Interpreter:</span> The conversion of byte code into native machine code is done at run-time in Java which furthers slows down the speed</li></ol><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;">However, the latest version of Java has addressed the performance bottlenecks to a great extent.</p><p style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><span style="font-weight: bolder;">Summary</span> :</p><ul style="background-color: white; clear: both; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px; margin: 1em 0px 1em 1.25em; padding: 0px 0px 0px 1.25em;"><li>JVM or Java Virtual Machine is the engine that drives the Java Code. It converts Java bytecode into machines language.</li><li>In JVM, Java code is compiled to bytecode. This bytecode gets interpreted on different machines</li><li>JIT or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It is used to speed up the execution time</li><li>In comparison to other compiler machines, Java may be slow in execution.</li></ul><div><span style="color: #222222; font-family: Arial, sans-serif;"><span style="font-size: 15.008px;"><br /></span></span></div></div><div><span style="color: #222222; font-family: Arial, sans-serif;"><span style="font-size: 15.008px;">Thanks for Reading </span></span></div><div><span style="color: #222222; font-family: Arial, sans-serif;"><span style="font-size: 15.008px;">Noeik</span></span></div><div><span style="color: #222222; font-family: Arial, sans-serif;"><span style="font-size: 15.008px;"><br /></span></span></div><div><span style="color: #222222; font-family: Arial, sans-serif;"><span style="font-size: 15.008px;"><br /></span></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div><div><span style="background-color: white; color: #222222; font-family: Arial, sans-serif; font-size: 15.008px;"><br /></span></div>
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-14157631499490807532021-06-13T22:22:00.002+05:302021-06-13T22:22:40.720+05:30Top Certifications in 2021We all know that Post COVID , most of the industries has started moving to digital era and in this digital transformation, we have many new opportunities in coming future. <div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-t1HUBi3VvEQ/YMY3Y4cvOUI/AAAAAAAAIhY/26PcPqCsfmEwPw8zlEmWwsF2Nz7OE0PZQCLcBGAsYHQ/s940/Blue%2BYellow%2BSprinkles%2BQuote%2BBeer%2BFacebook%2BPost-min.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="788" data-original-width="940" src="https://1.bp.blogspot.com/-t1HUBi3VvEQ/YMY3Y4cvOUI/AAAAAAAAIhY/26PcPqCsfmEwPw8zlEmWwsF2Nz7OE0PZQCLcBGAsYHQ/s320/Blue%2BYellow%2BSprinkles%2BQuote%2BBeer%2BFacebook%2BPost-min.png" width="320" /></a></div><br /><div><br /><div><br /></div><div>When companies started working remotely, Most toughest thing is to find the right resource and right skill set. Most of the companies has tie up with different resource firm where they hire the resource on contract basis first and then make full time employment offer for them OR companies now a days also started giving more preferences to certified resources by default , it gives them the confidence that the certification can somewhat justify someones skill sets.</div><div><br /></div><div>Today I will discuss about Most demanding certification program in 2021 which can definitely help you to find a better job for you.</div><div><br /></div><div>Before I go further , I would like to recommend <a href="https://www.spoto.info/">SPOTO IT Certification</a> for any certification exam preparation.If you are looking for some certifications , you can definitely check this website and can look for the training or exam preparation resources you are looking for.</div><div><br /></div><div><b><span style="font-size: large;">1) <u>PMP</u></span><u> </u></b>- PMP stands for Project Management Professional , as the word suggest , it is now a day very high in demand and most of the projects going to digitalized and Companies need Resources who can actually make sure that there project should be successfully delivered. so The Certification is internationally recognised which can make your bright in group of peoples of same skill set.</div><div>Learn more about <a href="https://www.spoto.info/">What is PMP Certification</a>.</div><div><br /></div><div><br /></div><div style="text-align: left;"><span style="font-size: large;">2)<span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; text-align: center;"><b><u>AWS Certifications</u></b></span></span><span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; font-size: 18px; font-weight: 700; text-align: center;">-</span><span style="background-color: white; text-align: center;"><span face="HelveticaNeue-Bold" style="color: #333333;"><span style="font-size: 18px;">If you have heard about Cloud , you definitely have heard about AWS as well , AWS stands for Amazon web service. AWS itself has so many Certification and because companies now host their data on cloud , the most loved and most preferred cloud service provider is AWS because of its vast managed services and unique billing system model. AWS is hot in market and if you are planning to <b><a href="https://www.spoto.info/aws-dumps">become a cloud engineer </a></b>, I would recommend you to go and take any AWS certification. You can actually go to </span></span><span face="HelveticaNeue-Bold" style="color: #333333; font-size: 18px;">SPOTO IT Certification</span><span face="HelveticaNeue-Bold" style="color: #333333;"><span style="font-size: 18px;"> where you can find </span></span><a href="https://cciedump.spoto.net/aws-certified-developer.php" style="color: #333333; font-family: HelveticaNeue-Bold; font-size: 18px;" target="_blank">AWS Dumps</a><span face="HelveticaNeue-Bold" style="color: #333333;"><span style="font-size: 18px;"> which will helpful for you to pass the exam.</span></span></span></div><div style="text-align: left;"><span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; font-size: 18px; text-align: center;"><br /></span></div><div style="text-align: left;"><span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; text-align: center;"><span style="font-size: large;">3)</span><b><span style="font-size: large;"><u>CCIE</u>-</span> </b><span style="font-size: 18px;">Ever green in market is Cisco certification why ? Because networking is the base of every company and when we talk about Networking , CISCO is world no. 1 Networking company. this is the only certification which 90-100% ensure that you will definetely get the Job after doing it as it is very tough to crack and it ensure that the one who crack it should sharpen his/her knowledge.</span></span></div><div><span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; font-size: 18px; text-align: center;"><br /></span></div><div style="text-align: left;"><span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; font-size: 18px; text-align: center;">BTW CCIE stands for <b><a href="https://cciedump.spoto.net/200-201-certificates.php" target="_blank">Cisco Certified Internetwork Expert.</a></b> You can directly go to here and can look for dumps.</span></div><div style="text-align: left;"><span face="HelveticaNeue-Bold" style="background-color: white; color: #333333; text-align: center;"><span style="font-size: large;"><br /></span></span></div><div><span style="font-size: large;">4) <u><b>Microsoft Certifications</b></u></span>-Microsoft is one of the most known IT Company in the world and in its certifications are always considered as one of the most valuable in IT Ecosystem. There are so many Certifications as below</div><div>There are so many certifications of microsoft which you can go for, You can check all the certification at <a href="https://cciedump.spoto.net/microsoft-70-740.php" target="_blank">this URL</a>.</div><div><br /></div><div><br /></div><div>There are some of the Important URLs you can go through while you are preparing about any certifications.</div><div><br />
<a href="https://www.spoto.info/pmi-dumps/project-management-professional-pmp-exam-dumps.html">SPOTO PMP exam prep</a> </div><div><a href="https://www.spoto.info/pmi-dumps">https://www.spoto.info/pmi-dumps</a> </div><div><a href="https://www.spoto.info/">what is PMP certification</a>
</div><div><br /></div><div>I hope you will find these certification helpful for your next phase in job.</div><div>If you have any issue you can leave us a comment we will definitely try to help you.</div><div><br /></div><div><br /></div><div>Thanks for reading</div><div>Noeik</div><div><br /></div><div><br /></div></div>Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-31033663587261052002020-08-12T21:59:00.002+05:302020-08-12T21:59:16.291+05:30[Interview Question ][Data Structure] Two Sum Problem -ArrayTwo sum problem is one of the most asked data structure questions for a java developer interview. There could be one or more ways to solve the problem but i am trying to give the optimized solution to this problem.<div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-4IiGovsg07M/XzQYyX-UIpI/AAAAAAAAIP0/ZUUyIA0I8MYwteBWAyrU7GUvMwLmAJNmACLcBGAsYHQ/s940/FotoJet-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="788" data-original-width="940" src="https://1.bp.blogspot.com/-4IiGovsg07M/XzQYyX-UIpI/AAAAAAAAIP0/ZUUyIA0I8MYwteBWAyrU7GUvMwLmAJNmACLcBGAsYHQ/s640/FotoJet-min.jpg" width="640" /></a></div><div><br /><div><br /></div><div>Lets first see the Problem Statement.</div><div><br /></div><p style="text-align: left;"><span style="font-size: x-large;"><b>Problem Statement</b> - Given an array, We need to find all the possible sets of 2 elements whose sum is equal to the target sum.</span></p><p style="text-align: left;"><br /></p><p style="text-align: left;"><span style="font-size: x-large;">Solution: </span>Given an array, lets say [10,-2,5,3,1,7,4] and given a target = 8 , We need to find all the possible 2 elements whose sum is equal to the 8.</p><p style="text-align: left;">the possible output will be </p><p style="text-align: left;">[10,-2] ,[5,3],[1,7]</p><p style="text-align: left;"><br /></p><p style="text-align: left;">Pseudo code- </p><p style="text-align: left;"></p><ul style="text-align: left;"><li>Lets first sort the array.</li><li>After sorting, take 2 pointers, one is leftmost & second is rightmost.</li><li>we will start taking each element from left & right then check some of both the element.</li><li>If the sum is less then the target sum, then move the left pointer by one.</li><li>if the sum is greater than the target sum, then move the right pointer to left by one.</li><li>else if it is equal to target then put the elements one result array and move both left & right pointer by one.</li></ul><div>Now let see the Java implementations.</div><div><br /></div><p></p><div></div><div><div></div></div><p style="text-align: left;"></p><div><br /></div><p></p><p style="text-align: left;"><span style="font-size: x-large;"><br /></span></p><p style="text-align: left;"><span style="font-size: x-large;"><br /></span></p><p style="text-align: left;"><span style="font-size: x-large;"><br /></span></p><p style="text-align: left;"><br /></p><div><ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div><!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TwoSumProblem</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> arr <span style="color: #333333;">=</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">,-</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">7</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">};</span>
twoSumArray<span style="color: #333333;">(</span>arr<span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">twoSumArray</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> arr<span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// sort the array using Arrays sort</span>
Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">sort</span><span style="color: #333333;">(</span>arr<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span> size <span style="color: #333333;">=</span> arr<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> left <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> right <span style="color: #333333;">=</span>size<span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>Integer<span style="color: #333333;">>();</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>left<span style="color: #333333;"><</span>right<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> diff <span style="color: #333333;">=</span> arr<span style="color: #333333;">[</span>left<span style="color: #333333;">]+</span>arr<span style="color: #333333;">[</span>right<span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>diff<span style="color: #333333;"><</span>i<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
left<span style="color: #333333;">++;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span> <span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>diff<span style="color: #333333;">></span>i<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
right<span style="color: #333333;">--;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>arr<span style="color: #333333;">[</span>left<span style="color: #333333;">]);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>arr<span style="color: #333333;">[</span>right<span style="color: #333333;">]);</span>
left<span style="color: #333333;">++;</span>
right<span style="color: #333333;">--;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span>Integer it <span style="color: #333333;">:</span> list<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>it<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"><br /></span></pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"><br /></span></pre></div>
</div>If you run this program you will get the list of sub set whose sume is equal to 8.<div><br /></div><div><br /></div><div>Hope this will help you in your datastructre problem solving.</div><div><br /></div><div>Thanks for reading.</div><div><br /></div></div></div>Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com1tag:blogger.com,1999:blog-2631709091184991837.post-47398376587093981512020-06-25T21:31:00.000+05:302020-06-25T21:31:13.739+05:30[Multi threading Interview Question ]Rate Limiter Implementation in javaThere was one interesting problem I have encounter while preparing for a multithreading coding interview.<div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-GbMFlgwGKoo/XvTKP9BktYI/AAAAAAAAIKs/StwreWqGP2cL9iagVWT34FwVLHINwkOsACK4BGAsYHg/s1080/FotoJet-min%2B%25281%2529.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1080" data-original-width="1080" height="320" src="https://1.bp.blogspot.com/-GbMFlgwGKoo/XvTKP9BktYI/AAAAAAAAIKs/StwreWqGP2cL9iagVWT34FwVLHINwkOsACK4BGAsYHg/s320/FotoJet-min%2B%25281%2529.jpg" /></a></div><div><br /><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><div><br /></div><div><b>Question: </b>We have an application for which we need to implement RateLimiter, </div><div>Rate Limiter is an interface which will play a role to limit the number of Request client send to Server in a given time.</div><div><br /></div><div>In the Current Question, it was asked to implement a rate limit who will do 2 things.</div><div><br /></div><div><b><i>1) It will only allow one client to send 100 requests in 1 hrs. ( one client will be identified by the client Id).</i></b></div><div><b><i>2) It will allow a total of 10000 requests to be passed to the server in 1 hrs.</i></b></div><div><br /></div><div><b>Rate Limiter Interface </b></div><div><pre style="text-align: left;">interface RateLimiter{<br /><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>boolean accept(String clientId);<br />}</pre></div><div><br /></div><div><div style="text-align: left;"><font size="5">Java Implementation of Rate Limiter </font></div></div><div style="text-align: left;"><br /></div><div style="text-align: left;">When we need to Implement such things where there is any restriction of accessing the resource for limited count We always start thinking using <b>Semaphores </b> </div><div style="text-align: left;">Why Semaphores? </div><div style="text-align: left;">Because it maintains the multi-access locking on its own with defined number of threads to access the critical section.</div><div style="text-align: left;"><br /></div><div style="text-align: left;"><font size="5">MyRateLimiter.java</font></div><div style="text-align: left;"><font size="5"><br /></font></div><div style="text-align: left;"><pre style="text-align: left;">class MyRateLimiter {<br /><span style="white-space: pre;"> </span> <br /><span style="white-space: pre;"> </span> <br /> private Semaphore semaphore;<br /> private int maxPermits;<br /> private TimeUnit timePeriod;<br /> private ScheduledExecutorService scheduler;<br /> <br /> <br /> <br /> public static MyRateLimiter create(int permits, TimeUnit timePeriod) {<br /> <span style="white-space: pre;"> </span>MyRateLimiter limiter = new MyRateLimiter(permits, timePeriod);<br /> limiter.schedulePermit();<br /> return limiter;<br /> }<br /> <br /> private MyRateLimiter(int permits, TimeUnit timePeriod) {<br /> this.semaphore = new Semaphore(permits);<br /> this.maxPermits = permits;<br /> this.timePeriod = timePeriod;<br /> }<br /> <br /> public boolean tryAcquire() {<br /> return semaphore.tryAcquire();<br /> }<br /> <br /> public void stop() {<br /> scheduler.shutdownNow();<br /> }<br /> <br /> public void schedulePermit() {<br /> scheduler = Executors.newScheduledThreadPool(1);<br /> scheduler.scheduleAtFixedRate(() -> {<br /> semaphore.release(maxPermits - semaphore.availablePermits());<br /> }, 0, 1, timePeriod);<br /> <br /> }<br /><span style="white-space: pre;"> <br /></span> }</pre></div><div style="text-align: left;"><font size="5"><br /></font></div><div style="text-align: left;"><font size="5"><span> RateLimiterImpl.java</span><br /></font></div><div style="text-align: left;"><font size="5"><span><br /></span></font></div><div style="text-align: left;"><pre><span style="white-space: pre;"> </span>class RateLimiterImpl implements RateLimiter{<br /><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>private static long <font color="#d52c1f">MINUTE_TIME_LIMIT=1000*60L;</font> // update as per question <br /><span style="white-space: pre;"> </span>private static long <font color="#d52c1f">REQUEST_ALLOWED_PER_MINUTE=10000;</font> // Update as per question<br /><span style="white-space: pre;"> </span>Queue<Long> q = new LinkedList<>();<br /><span style="white-space: pre;"> </span>private static int minuteLimit=100;<br /><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>private Map<String, Optional<MyRateLimiter>> limiters = new ConcurrentHashMap<>();<br /><span style="white-space: pre;"> </span> <br /><span style="white-space: pre;"> </span>@Override<br /><span style="white-space: pre;"> </span>public boolean accept(String clientId) {<br /><span style="white-space: pre;"> </span>if(!hit(System.currentTimeMillis())) {<br /><span style="white-space: pre;"> </span>return false;<br /><span style="white-space: pre;"> </span>}<br /><span style="white-space: pre;"> </span>Optional<MyRateLimiter> rateLimiter = getRateLimiter(clientId);<br /><span style="white-space: pre;"> </span>if(rateLimiter.isPresent()) {<br /><span style="white-space: pre;"> </span>boolean canAcquire= rateLimiter.get().tryAcquire();<br /><span style="white-space: pre;"> </span>if(canAcquire)<br /><span style="white-space: pre;"> </span>return q.add(System.currentTimeMillis());<br /><span style="white-space: pre;"> </span>}<br /><span style="white-space: pre;"> </span>return false;<br /><span style="white-space: pre;"> </span>}<br /><span style="white-space: pre;"> </span>private boolean hit(long timestamp) {<br /><span style="white-space: pre;"> </span>while(!q.isEmpty() && timestamp-q.peek() >= MINUTE_TIME_LIMIT) q.poll();<br /><span style="white-space: pre;"> </span>if(q.size() < REQUEST_ALLOWED_PER_MINUTE)<br /><span style="white-space: pre;"> </span>{<br /><span style="white-space: pre;"> </span>q.offer(timestamp); <br /><span style="white-space: pre;"> </span>return true;<br /><span style="white-space: pre;"> </span>}<br /><span style="white-space: pre;"> </span>return false;<br /><span style="white-space: pre;"> </span>}<br /><span style="white-space: pre;"> </span>private Optional<MyRateLimiter> getRateLimiter(String clientId) {<br /><span style="white-space: pre;"> </span> return limiters.computeIfAbsent(clientId, id -> {<br /><span style="white-space: pre;"> </span> return Optional.of(createRateLimiter(id));<br /><span style="white-space: pre;"> </span> <br /><span style="white-space: pre;"> </span> });<br /><span style="white-space: pre;"> </span> }<br /><span style="white-space: pre;"> </span>private MyRateLimiter createRateLimiter(String clientId) {<br /><span style="white-space: pre;"> </span> return MyRateLimiter.create(minuteLimit, TimeUnit.MINUTES);<br /><span style="white-space: pre;"> </span> }<br /><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>}</pre></div><div style="text-align: left;"><br /></div><div style="text-align: left;"><font size="5">Main Class Calling</font></div><div style="text-align: left;"><font size="5"><br /></font></div><div style="text-align: left;"><pre>public class RateLimiterDemo {<br /><span style="white-space: pre;"> </span><span style="white-space: pre;"><br /></span><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>public static void main(String[] args) {<br /><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>RateLimiter limiter = new RateLimiterImpl();<br /> System.out.println("test1 " + limiter.accept("test1"));<br /> System.out.println("test1 " +limiter.accept("test1"));<br /> System.out.println("test1 " +limiter.accept("test1"));<br /> System.out.println("test1 " +limiter.accept("test1"));<br /> System.out.println("test2 " +limiter.accept("test2"));<br /> System.out.println("test2 " +limiter.accept("test2"));<br /> System.out.println("test2 " +limiter.accept("test2"));<br /> System.out.println("test2 " +limiter.accept("test2"));<br /> System.out.println("test1 " +limiter.accept("test1"));<br /><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> <br /></span><span style="white-space: pre;"> </span>}<br /><span style="white-space: pre;"> </span><span style="white-space: pre;"> </span><br />}</pre><pre><br /></pre><pre>You can also check the Code from <a href="https://gist.github.com/visparashar/4fd3b4d34ed469c083e9ee0465604437">HERE </a></pre><pre><br /></pre><pre>Hope this will help you in understanding the Code</pre></div><div><br /></div></div>Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-1366284290573238282020-06-16T13:49:00.000+05:302020-06-16T13:49:34.951+05:30101 Most Useful Websites on Internet<div><br /></div><div>The Most Useful Websites and Web Apps</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-1kKVH8yil2c/XuiAL3xnsQI/AAAAAAAAIJs/kCChNaAr5yIkMBbIdOo9ycvIuXZlb3GbQCK4BGAsYHg/s740/101usefulwebsites.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="366" data-original-width="740" height="198" src="https://1.bp.blogspot.com/-1kKVH8yil2c/XuiAL3xnsQI/AAAAAAAAIJs/kCChNaAr5yIkMBbIdOo9ycvIuXZlb3GbQCK4BGAsYHg/w400-h198/101usefulwebsites.png" width="400" /></a></div><div><br /></div><div><ol style="text-align: left;"><li><a href="archive.is">archive.is</a>— take a snapshot of any web page and it will exist forever even if the original page is gone.</li><li><a href="autodraw.com" target="_blank">autodraw.com</a> — create freehand doodles and watch them magically transform into beautiful drawings powered by maching learning.</li><li><a href="fast.com">fast.com</a> — check the current speed of your Internet connection.</li><li><a href="slides.com">slides.com</a> — create pixel-perfect slide decks and broadcast your presentations to an audience of any size from anywhere.</li><li><a href="screenshot.guru">screenshot.guru</a> — take high-resolution screenshots of web pages on mobile and desktops.</li><li><a href="dictation.io">dictation.io</a> – accurate and quick voice recognition in your browser itself.</li><li><a href="reverse.photos">reverse.photos</a> — upload an image and find similar pictures on the web.</li><li><a href="copychar.cc">copychar.cc</a> – copy special characters and emojis that aren’t on your keyboard.</li><li><a href="codeacademy.com">codeacademy.com </a> – the best place to learn coding online.</li><li><a href="noisli.com">noisli.com</a> — ambient noises to help you improve focus and boost productivity.</li><li><a href="iconfinder.com">iconfinder.com</a> – millions of icons for all kinds of projects. Also try icons8.com 39 and laticon.com 36.</li><li><a href="jotti.org">jotti.org</a> – scan any suspicious file or email attachment for viruses.</li><li><a href="wolframalpha.com">wolframalpha.com</a> – gets answers directly without searching – see more wolfram tips.</li><li><a href="flightstats.com">flightstats.com</a> – track flight status at airports worldwide.</li><li><a href="unsplash.com">unsplash.com</a> – the best place to download images absolutely free.</li><li><a href="videos.pexels.com">videos.pexels.com</a> — an online library of free HD videos you can use everywhere. Also see videvo.net 76.</li><li><a href="everytimezone.com">everytimezone.com</a> – a less confusing view of the world time zones.</li><li><a href="e.ggtimer.com">e.ggtimer.com</a> – a simple online timer for your daily needs.</li><li><a href="random.org">random.org</a> – pick random numbers, flip coins, and more.</li><li><a href="earn.com">earn.com</a> — replace your email with a mailbox that pays when you reply to someone’s email.</li><li><a href="myfonts.com/WhatTheFont">myfonts.com/WhatTheFont</a> – upload an image of any text and quickly determine the font family.</li><li><a href="fonts.google.com">fonts.google.com</a> – the best collection of open source fonts that you can use anywhere without restrictions.</li><li><a href="fontstruct.com">fontstruct.com</a> — draw and build your own fonts and use them in any application.</li><li><a href="calligraphr.com">calligraphr.com</a> — transform your handwriting into a real font.</li><li><a href="regex.info">regex.info</a> – find data hidden in your photographs – see more EXIF tools.</li><li><a href="youtube.com/webcam">youtube.com/webcam</a> — broadcast yourself live over the Internet without any complicated setup.</li><li><a href="remotedesktop.google.com">remotedesktop.google.com</a> — access other computers or allow others to remote access your computer over the Internet.</li><li><a href="homestyler.com">homestyler.com</a> – design from scratch or re-model your home in 3D.</li><li><a href="pdfescape.com">pdfescape.com</a> – lets you quickly edit PDF in the browser without Acrobat.</li><li><a href="draw.io">draw.io</a> – create diagrams, wireframe and flowcharts in the browser.</li><li><a href="web.skype.com">web.skype.com</a> — make voice and video calls in your browser with Skype.</li><li><a href="onlineocr.net">onlineocr.net</a> – recognize text from scanned PDFs – see other OCR tools.</li><li><a href="wetransfer.com">wetransfer.com</a> – for sharing really big files online.</li><li><a href="file.pizza">file.pizza</a> — peer to peer file transfer over WebRTC without any middleman.</li><li><a href="snapdrop.com">snapdrop.com</a> — like Apple AirDrop but for the web. Share files directly between devices in the same network without having to upload them to any server first.</li><li><a href="hundredzeros.com">hundredzeros.com </a> – the site lets you download free Kindle books.</li><li><a href="app.grammarly.com">app.grammarly.com</a> — check your writing for spelling, style, andgrammatical errors.</li><li><a href="noteflight.com">noteflight.com</a> – print music sheets, write your own music online ( review).</li><li><a href="translate.google.com">translate.google.com</a> – translate web pages, PDFs and Office documents.</li><li><a href="kleki.com">kleki.com</a> – create paintings and sketches with a wide variety of brushes.</li><li><a href="similarsites.com">similarsites.com</a> – discover new sites that are similar to what you like already.</li><li><a href="bubbl.us">bubbl.us</a> – create mind-maps, brainstorm ideas in the browser.</li><li><a href="color.adobe.com">color.adobe.com</a> – get color ideas, also extract colors from photographs.</li><li><a href="canva.com">canva.com</a> — make beautiful graphics, presentations, resumes and more with readymade template designs.</li><li><a href="lmgtfy.com">lmgtfy.com</a> – when your friends are too lazy to use Google on their own.</li><li><a href="midomi.com">midomi.com</a> – when you need to find the name of a song.</li><li><a href="history.google.com">history.google.com</a> — see all your past Google searches, also among most important Google RLs</li><li><a href="faxzero.com">faxzero.com</a> – send an online fax for free – see more fax services.</li><li><a href="tinychat.com">tinychat.com</a> – setup your own private chat room in micro-seconds.</li><li><a href="privnote.com">privnote.com</a> – create text notes that will self-destruct after being read.</li><li><a href="domains.google.com">domains.google.com</a> – quickly search domain names for your next big idea!</li><li><a href="downforeveryoneorjustme.com">downforeveryoneorjustme.com</a> – find if your favorite website is offline or not?</li><li><a href="gtmetrix.com">gtmetrix.com</a> – the perfect tool for measuring your site performance online.</li><li><a href="builtwith.com">builtwith.com</a> — find the web hosting company, email provider and everything else about a website.</li><li><a href="urbandictionary.com">urbandictionary.com</a> – find definitions of slangs and informal words.</li><li><a href="seatguru.com">seatguru.com</a> – consult this site before choosing a seat for your next flight.</li><li><a href="flightstats.com">flightstats.com</a> – Track flight status at airports worldwide.</li><li><a href="mymaps.google.com">mymaps.google.com</a> – create custom Google Maps with scribbles, pins and custom shapes.</li><li><a href="snopes.com">snopes.com</a> – find if that email offer you received is real or just another scam.</li><li><a href="typingweb.com">typingweb.com</a> – master touch-typing with these practice sessions.</li><li><a href="todo.microsoft.com">todo.microsoft.com</a> — a beautiful todo app and task manager. Also see Trello.</li><li><a href="minutes.io">minutes.io</a> – quickly capture effective notes during meetings.</li><li><a href="talltweets.com">talltweets.com</a> — Turn Google Slides in animated GIF presentations.</li><li><a href="ifttt.com">ifttt.com</a> – create a connection between all your online accounts.</li><li><a href="namechk.com">namechk.com</a> — search for your desired username across hundreds of social networks and omain names.</li><li><a href="gist.github.com">gist.github.com</a> — create anonymous and secret text notes and much more.</li><li><a href="flipanim.com">flipanim.com</a> — create flipbook animations, includes an onion skin tool to let you see the revious frame as you draw the next one.</li><li><a href="powtoon.com">powtoon.com</a> — create engaging whiteboard videos and presentations with your own voiceovers. lso see videoscribe.co</li><li><a href="clyp.it">clyp.it</a> — Record your own voice or upload an audio file without creating any account. Also see oundcloud.com</li><li><a href="carrd.co">carrd.co</a> — build one-page fully responsive websites that look good on every screen.</li><li><a href="spark.adobe.com">spark.adobe.com</a> — make stunning video presentations with voice narration and wow everyone.</li><li><a href="anchor.fm">anchor.fm</a> — the easiest way to record a podcast that you can distribute on iTunes without have to pay for hosting.</li><li><a href="duolingo.com">duolingo.com</a> — learn to speak Chinese, French, Spanish or any other language of your choice.</li><li><a href="webmakerapp.com">webmakerapp.com</a> — an offline playground for building web projects in HTML, CSS and javascript.</li><li><a href="pixton.com">pixton.com</a> — create your own comic strips with your own characters and move them into any one.</li><li><a href="designer.io">designer.io</a> — a full-featured vector drawing tool that works everywhere.</li><li><a href="sumopaint.com">sumopaint.com</a> – an excellent layer-based online image editor.</li><li><a href="vectr.com">vectr.com</a> — create vector graphics and export them as SVG or PNG files.</li><li><a href="twitterbots">twitterbots</a> — create your own Twitter bots that can auto-reply, DM, follow people and more.</li><li><a href="headspace.com">headspace.com</a> — learn the art of meditation and reduct stress, focus more and even sleep better.</li><li><a href="class-central.com">class-central.com</a> — a directory of free online courses offered by universities worldwide.</li><li><a href="googleartproject.com">googleartproject.com</a> — discover museums, famous paintings and art treasure from all around the world.</li><li><a href="instructables.com">instructables.com</a> — step-by-step guides on how to build anything and everything.</li><li><a href="flowgram.com">flowgram.com</a> — make data-driven graphics, charts and infographics. Also see adioma.com 18 and eas.ly.</li><li><a href="marvelapp.com">marvelapp.com</a> — create interactive wireframes and product mockups.</li><li><a href="slide.ly">slide.ly</a> — make marketing videos and branded stories for Instagram, Facebook and YouTube trailers. Also, see animoto.com 13 and biteable.com 11.</li><li><a href="gohighbrow.com">gohighbrow.com</a> — Take bite-sized courses on a variety of topics, chapters are delivered by email every morning.</li><li><a href="htmlmail.pro">htmlmail.pro</a> – send rich-text emails with gmail mail merge.</li><li><a href="wirecutter.com">wirecutter.com</a> — whether you need a vacuum cleaner or an SD card, this is the best product recommendation website on the Internet.</li><li><a href="camelcamelcamel.com">camelcamelcamel.com </a> — Create Amazon price watches and get email alerts when the prices drop.</li><li><a href="mockaroo.com">mockaroo.com</a> — download mock data to fill the rows in your Excel spreadsheet.</li><li><a href="asciiflow.com">asciiflow.com</a> — a WYSIWYG editor to draw ASCII diagrams that you can embed in emails and tweets.</li><li><a href="buffer.com">buffer.com</a> — the easily way to post and schedule updates on Twitter, Instagram, LinkedIn, Google+ and Facebook.</li><li><a href="10minutemail.com">10minutemail.com</a> — create disposable email addresses for putting inside sign-up forms.</li><li><a href="whereami">whereami</a> — find the postal address of your current location on Google maps.</li><li><a href="sway.com">sway.com</a> — create and share interactive reports, newsletters, presentations, and for storytelling.</li><li><a href="apify.com">apify.com</a> — the perfect web scraping tool that lets you extract data from nearly any website.</li><li><a href="thunkable.com">thunkable.com</a> — build your own apps for Android and iOS by dragging blocks instead of writing code. Also see: glitch.com 36</li><li><a href="zerodollarmovies.com">zerodollarmovies.com</a> — a huge collection of free movies curated from YouTube.</li><li><a href="upwork.com">upwork.com</a> — find freelancers and subject experts to work on any kind of project.</li><li><a href="duckduckgo.com">duckduckgo.com</a> – a clean alternative to google search that doesn’t track you on the Internet.</li></ol></div><div><br /></div><div>Know any useful website that is missing in the list? Please let me know via comment</div><div><br /></div><div><br /></div><div>Hope you hind these websites useful, </div><div><br /></div><div>Thanks for reading </div><div>Noeik</div><div><br /></div><div><br /></div><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-90950229687092967642019-07-07T14:17:00.000+05:302019-07-07T14:17:45.690+05:30Stock Buy & Sell Problem<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b>Question: </b>There is a given array of the stock price, each index denotes the price of the stock on that day. You need to find the max profit you will earn if you buy and sell the stock.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-2709qHokrZk/XSGxM304AiI/AAAAAAAAHUs/vtZxfaEG_-UkZJZ76kBxGNH6xb_s2ducgCLcBGAs/s1600/FotoJet-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="456" src="https://1.bp.blogspot.com/-2709qHokrZk/XSGxM304AiI/AAAAAAAAHUs/vtZxfaEG_-UkZJZ76kBxGNH6xb_s2ducgCLcBGAs/s640/FotoJet-min.jpg" width="640" /></a></div>
<br />
<br />
<div>
<br /></div>
<div>
<b>Example -</b> Given an array - <b>[10,5,6,8,9,3] </b>where index 0 shows the prices of the stock on day 1 respectively.</div>
<div>
<br /></div>
<div>
<b>Answer</b> Maxprofit=4<br />
Explanation - first, we purchase at day 2 ( price - 5) and sell at day 5 (price 9 ) profit = 9-5=4</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Code - </div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">vp</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learning</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">StockBuyAndSellProblem</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// given stock data as </span>
<span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> prices <span style="color: #333333;">=</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">9</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">};</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>getMaxProfit<span style="color: #333333;">(</span>prices<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">getMaxProfit</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> prices<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>prices<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;"><=</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span> <span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> peak <span style="color: #333333;">=</span> prices<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">];</span>
<span style="color: #333399; font-weight: bold;">int</span> valley <span style="color: #333333;">=</span> prices<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">];</span>
<span style="color: #333399; font-weight: bold;">int</span> maxProfit <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>i<span style="color: #333333;"><</span>prices<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>i <span style="color: #333333;"><</span>prices<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #333333;">&&</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">]>=</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">])</span>
i<span style="color: #333333;">++;</span>
valley <span style="color: #333333;">=</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>i <span style="color: #333333;"><</span>prices<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #333333;">&&</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">]<=</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">])</span>
i<span style="color: #333333;">++;</span>
peak <span style="color: #333333;">=</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">];</span>
maxProfit<span style="color: #333333;">+=</span>peak <span style="color: #333333;">-</span> valley <span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> maxProfit<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<br /></div>
<div>
<b>Explanation :</b><br />
<b><br /></b>
What we are doing here is as below<br />
<br />
We are using Peak Valley Approach where are first calculate valley value in array<br />
<br />
<ul style="text-align: left;">
<li>valley means lower value </li>
<li>peak means higher value</li>
</ul>
<div>
Now <span style="background-color: yellow;"><span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>i <span style="color: #333333;"><</span>prices<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">-</span><span style="color: #0000dd; font-weight: bold;">1</span> <span style="color: #333333;">&&</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">]>=</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">])</span></span></div>
<div>
<br /></div>
<div>
<span style="color: #333333;">This is calculating the valley values, means the less value index in the array.</span></div>
<div>
<span style="color: #333333;"><br /></span></div>
<div>
<span style="color: #333333;">second, while loop is calculating the peak value in the array after this valley index.</span></div>
<div>
<span style="color: #333333;"><br /></span></div>
<div>
<span style="background-color: yellow;"><span style="color: #333333;">if you see </span>prices<span style="color: #333333;">[</span>i<span style="color: #333333;">]>=</span> prices<span style="color: #333333;">[</span>i<span style="color: #333333;">+</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">] this means, price at current day is greater then the price at next day , that means the next day is valley or lower value, similarly for the next while as well, the price of current is less than the next day, so the next day will be peak value </span></span></div>
<div>
<span style="color: #333333;"><br /></span></div>
<div>
<span style="color: #333333;">and the differences between peak and valley is actually the max profit we will earn for the day.</span></div>
<div>
<span style="color: #333333;"><br /></span></div>
<div>
<span style="color: #333333;"><b>Time Complexity </b></span></div>
<div>
<span style="color: #333333;"><b><br /></b></span></div>
<div>
<span style="color: #333333;">In this case, the time complexity will be O(n)</span></div>
<div>
<span style="color: #333333;"><br /></span></div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br />
<div>
<b><br /></b>
<b>Hope this will help you in the interview. If you have any issue, please leave us a comment.</b><br />
<b><br /></b>
<b><br /></b>
<b><br /></b>
<b>Thanks for reading </b><br />
<b>Noeik</b></div>
</div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-30774313042177375402019-05-17T23:56:00.001+05:302019-05-17T23:56:58.066+05:30Algorithm to find if String contains only Unique characters without using any additional data structure<div dir="ltr" style="text-align: left;" trbidi="on">
The string is one of the most asked questions in the Interview of Java developers.<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InPostArticle-Ads -->
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-4va3RlsoFKg/XN78ndmUE2I/AAAAAAAAHQY/L4sg-JPiFpMxx1ZBHxMTTFA92I3FHVsVQCLcBGAs/s1600/FotoJet-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="285" src="https://2.bp.blogspot.com/-4va3RlsoFKg/XN78ndmUE2I/AAAAAAAAHQY/L4sg-JPiFpMxx1ZBHxMTTFA92I3FHVsVQCLcBGAs/s400/FotoJet-min.jpg" width="400" /></a></div>
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
Today we will see one of the most asked questions as to how will you find if String does not contain any duplicate characters.<br />
<br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<br /></div>
<div>
There are different ways to check them, But we will only discuss the most efficient way to implement this algorithm.</div>
<div>
<br /></div>
<div>
<h3 style="text-align: left;">
Logic.</h3>
</div>
<div>
We know that characters have only 256 ascii values and there are only 128 values for alphabet characters. </div>
<div>
So what we will do is we will create one array of boolean of size 128 and we will update the index of array as true whenever we find the char value in the string , else if we get the same value in the string , the already true value will make the loop as false.<br />
Also read - <a href="https://www.programinjava.com/2018/03/find-longest-common-ancestorlca-program.html" target="_blank">Find Longest Common Ancestor(LCA) Program in java</a></div>
<div>
<br /></div>
<div>
<b>Time Complexity - O(n)</b></div>
<div>
<b>Space Complexity - O(1)</b></div>
<div>
<br /></div>
<div>
<h3 style="text-align: left;">
Code -</h3>
</div>
<div>
<br /></div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ArraysAndStringDemo</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
String str <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"programinjava"</span><span style="color: #333333;">;</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>isUnique<span style="color: #333333;">(</span>str<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">isUnique</span><span style="color: #333333;">(</span>String str<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">boolean</span><span style="color: #333333;">[]</span> char_set <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">boolean</span><span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">128</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span>str<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">();</span>i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> value <span style="color: #333333;">=</span>str<span style="color: #333333;">.</span><span style="color: #0000cc;">charAt</span><span style="color: #333333;">(</span>i<span style="color: #333333;">); //getting the ascii value of character</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>char_set<span style="color: #333333;">[</span>value<span style="color: #333333;">]) // checking if it is already in the array</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
char_set<span style="color: #333333;">[</span>value<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<br />
If you have any issue in understanding the same , Please leave us a comment. Happy to help<br />
<br />
Thanks for reading<br />
Noeik</div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-69386006142512034512019-03-09T17:08:00.001+05:302019-03-09T17:08:15.208+05:30Real time Logs of AWS Elastic Beanstalk by Command Prompt<div dir="ltr" style="text-align: left;" trbidi="on">
Many of us know that one of the most important services of AWS is Elastic beanstalks. We deployed our application on Beanstalk and AWS itself manage most of the thing in it. One of them is Logs of Applications.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-wlO8YO_UJiE/XIOko0sx80I/AAAAAAAAHMU/oBPGQOyLaYQs0knbebXVo-kLFqKLnIvvACLcBGAs/s1600/FotoJet%2B%25282%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="285" src="https://2.bp.blogspot.com/-wlO8YO_UJiE/XIOko0sx80I/AAAAAAAAHMU/oBPGQOyLaYQs0knbebXVo-kLFqKLnIvvACLcBGAs/s400/FotoJet%2B%25282%2529-min.jpg" width="400" /></a></div>
<div>
<br /></div>
<div>
<div>
<br /></div>
<div>
People usually were not able to see the tail of the logs of the application what they need to do is to go to the beanstalk and under logs, they ask for the last 100 lines of logs or full logs (which include the of the application along with the other logs ).</div>
<div>
<br /></div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<br /></div>
<div>
Today we will learn how to connect with the beanstalk application through <b>EB CLI </b>and see the logs.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="font-size: large;">let's follow the below steps.</span></div>
<div>
<ol style="text-align: left;">
<li>Install the Python Once installed run below commands</li>
<li>python --version</li>
<li>pip --version</li>
<li>pip install awsebcli <span class="token operator" style="background: rgba(255 , 255 , 255 , 0.5); box-sizing: border-box; color: #a67f59; font-family: "consolas" , "andale mono wt" , "andale mono" , "lucida console" , "lucida sans typewriter" , "dejavu sans mono" , "bitstream vera sans mono" , "liberation mono" , "nimbus mono l" , "monaco" , "courier new" , "courier" , monospace; font-size: 14px; white-space: pre;">--</span><span style="background-color: #f3f5fc; color: #333333; font-family: "consolas" , "andale mono wt" , "andale mono" , "lucida console" , "lucida sans typewriter" , "dejavu sans mono" , "bitstream vera sans mono" , "liberation mono" , "nimbus mono l" , "monaco" , "courier new" , "courier" , monospace; font-size: 14px; white-space: pre;">upgrade </span><span class="token operator" style="background: rgba(255 , 255 , 255 , 0.5); box-sizing: border-box; color: #a67f59; font-family: "consolas" , "andale mono wt" , "andale mono" , "lucida console" , "lucida sans typewriter" , "dejavu sans mono" , "bitstream vera sans mono" , "liberation mono" , "nimbus mono l" , "monaco" , "courier new" , "courier" , monospace; font-size: 14px; white-space: pre;">--</span><span style="background-color: #f3f5fc; color: #333333; font-family: "consolas" , "andale mono wt" , "andale mono" , "lucida console" , "lucida sans typewriter" , "dejavu sans mono" , "bitstream vera sans mono" , "liberation mono" , "nimbus mono l" , "monaco" , "courier new" , "courier" , monospace; font-size: 14px; white-space: pre;">user</span></li>
<li>once ebcli installed successfully, you can check by </li>
<li>eb --version</li>
<li>define the Python Home in your Environment Variable.</li>
<li>Now Run your command prompt as Administrator and write </li>
</ol>
<blockquote>
<b>eb init --interactive</b></blockquote>
You will see the below image.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-1Kazd08pb3M/XIOedAjFEaI/AAAAAAAAHLo/sJCa-6axKIsg6sWB3D_NQGMnspQo8227gCLcBGAs/s1600/Capture.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="693" data-original-width="847" height="521" src="https://2.bp.blogspot.com/-1Kazd08pb3M/XIOedAjFEaI/AAAAAAAAHLo/sJCa-6axKIsg6sWB3D_NQGMnspQo8227gCLcBGAs/s640/Capture.JPG" width="640" /></a></div>
</div>
<div>
<ul style="text-align: left;">
<li>Now select the region you have deployed your application.</li>
<li>After that, you will be asked to Create a new Application </li>
<li>Once your application will be created you will see below screen.</li>
</ul>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-1QMlutAm9N0/XIOfDVx9apI/AAAAAAAAHLw/sB-hOvdGOwAOGToE2pndGrmPt_EZFr0KgCLcBGAs/s1600/capture1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="434" data-original-width="653" height="424" src="https://3.bp.blogspot.com/-1QMlutAm9N0/XIOfDVx9apI/AAAAAAAAHLw/sB-hOvdGOwAOGToE2pndGrmPt_EZFr0KgCLcBGAs/s640/capture1.JPG" width="640" /></a></div>
<div>
<ul style="text-align: left;">
<li>You need to select the appropriate platform you have used for your application.</li>
<li>Now Once you do it you will be asked to create the SSH </li>
</ul>
</div>
<blockquote class="tr_bq">
Select Yes </blockquote>
<div>
<ul style="text-align: left;">
<li>Then they will ask you to create the keypair </li>
</ul>
</div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<br /></div>
<div>
<ul style="text-align: left;">
<li>Provide your password and then you will see the below image </li>
</ul>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-oPZtR5YB5Rk/XIOfz7_Vl7I/AAAAAAAAHL8/SNha1JE1i5wkky1zo2I-b1dOObWRxFR2ACLcBGAs/s1600/capture2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="529" data-original-width="757" height="446" src="https://1.bp.blogspot.com/-oPZtR5YB5Rk/XIOfz7_Vl7I/AAAAAAAAHL8/SNha1JE1i5wkky1zo2I-b1dOObWRxFR2ACLcBGAs/s640/capture2.JPG" width="640" /></a></div>
<div>
<ul style="text-align: left;">
<li>Now you have successfully created your SSH and it is uploaded to your beanstalk as well.</li>
</ul>
</div>
<div>
<ul style="text-align: left;">
<li>you need to attach the SSH with the Application.</li>
</ul>
</div>
<div>
<br /></div>
<div>
<span style="font-size: large;">Follow below steps </span></div>
<div>
<br /></div>
<blockquote class="tr_bq">
eb ssh <Application-name> --settings</blockquote>
<div>
<br /></div>
<div>
<ul style="text-align: left;">
<li>You will get the list of options then it will ask for the keypair which you need to attach.</li>
<li>Select the appropraite one.</li>
</ul>
</div>
<div>
Also Read : <b><a href="https://www.programinjava.com/2018/09/remote-debugging-of-java-application.html" target="_blank">Remote Debugging of Java Application deployed on AWS Elastic Beanstalk</a></b></div>
<div>
<ul style="text-align: left;">
<li>After selecting the one you will be asked to enter the password set earlier.</li>
<li>You will then have the Applicaiton connected.</li>
<li>Once connected successfully you will be getting below image.</li>
</ul>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-UK7y80fN70w/XIOhIlBurWI/AAAAAAAAHMI/J-A_ZemngN0YG2gORrRz4-j8oY4LHXUwQCLcBGAs/s1600/caputre3.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="275" data-original-width="1075" height="161" src="https://3.bp.blogspot.com/-UK7y80fN70w/XIOhIlBurWI/AAAAAAAAHMI/J-A_ZemngN0YG2gORrRz4-j8oY4LHXUwQCLcBGAs/s640/caputre3.JPG" width="640" /></a></div>
<div>
Now go to </div>
<blockquote class="tr_bq">
<b>/var/log/tomcat8</b> ( to see the application logs, if your application is deployed on tomcat)</blockquote>
<br />
I Hope you will be able to connect your local machine to EB , If you have any issue ,Please leave us a comment we will look into it.<br />
<br />
<span style="background-color: white; color: #444444; font-family: georgia, "times new roman", times, serif; font-size: 16px;">Also Read:</span><br />
<ol style="background: rgb(255, 255, 255); border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-simple-application-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Spring Boot and Hibernate Tutorials - Application using Spring Boot</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">What is Spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/04/how-to-read-value-from-property-file-in_16.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">How to read value from property file in spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">First Rest API Application - Top Spring Framework Interview Questions</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/05/implementation-of-swagger-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Implementation of Swagger in Spring boot</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/05/how-to-use-h2-embedded-database-in.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">How to use h2 database in spring boot</a></li>
</ol>
<br />
Thanks for reading.<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-50029396209624700922019-02-16T01:26:00.001+05:302019-02-16T01:27:40.654+05:30Database Timezone issue -Solution in java<div dir="ltr" style="text-align: left;" trbidi="on">
We have at least once in our development has encountered one issue with database and client location is Timezone difference. <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-EVZFCziSY58/XGcYRpAbymI/AAAAAAAAHKI/kePXWtnvowc_hxAfcKKb9V-zlMPBhhwQwCLcBGAs/s1600/FotoJet-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="285" src="https://4.bp.blogspot.com/-EVZFCziSY58/XGcYRpAbymI/AAAAAAAAHKI/kePXWtnvowc_hxAfcKKb9V-zlMPBhhwQwCLcBGAs/s400/FotoJet-min.jpg" width="400" /></a></div>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
In most of the cases, Our Backend /Application server is in a different timezone and our client server is in a different timezone. So what actually the problem happens is the difference in data time because of Timezone.<br />
<a name='more'></a><br />
<b>Also Learn:</b> <a href="https://www.programinjava.com/2018/04/method-overloading-in-java.html" target="_blank">Method Overloading in java</a></div>
<div>
<br /></div>
<div>
<b>Problem Statement - </b>I have one table of order in my DB and I need to schedule the job to be run on specific time ( let suppose - 1:00 AM every day ) , the schedule is in IST timezone and the DB is in GMT+2 timezone. So the job runs 3.30 Hrs before the actual time it should run.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- programinjava-inarticle-new-ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="6977088426" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<br /></div>
<div>
<b>Solution - </b></div>
<div>
The Approach I have taken is to make sure that when the date in db is inserted it should be of the timezone where the scheduler is in.( Basically, both should be in same time zone .)</div>
<div>
<br /></div>
<div>
Technology /Programming language used -Java,, Spring boot , JPA</div>
<div>
<br /></div>
<div>
Now I have done a simple change in my code as below.</div>
<div>
<br /></div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #555555; font-weight: bold;">@SpringBootApplication</span>
<span style="color: #555555; font-weight: bold;">@EnableAsync</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Application</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@PostConstruct</span>
<span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">started</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
TimeZone<span style="color: #333333;">.</span><span style="color: #0000cc;">setDefault</span><span style="color: #333333;">(</span>TimeZone<span style="color: #333333;">.</span><span style="color: #0000cc;">getTimeZone</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"IST"</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
SpringApplication<span style="color: #333333;">.</span><span style="color: #0000cc;">run</span><span style="color: #333333;">(</span>Application<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">,</span> args<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<br /></div>
<div>
So in the above code, I am explicitly making the default timezone as IST for the db as well.</div>
<div>
<br /></div>
<div>
Hope this will help you in code.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- LinkAds-programinjava-belowsearch -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="link" data-ad-slot="2326470934" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<br /></div>
<div>
If you face any issue, please leave us a comment.</div>
<div>
<br /></div>
<div>
Thanks for reading </div>
<div>
Noeik </div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-45499528260260074872018-11-28T00:21:00.002+05:302019-01-02T00:19:29.198+05:30Thread Pooling In Java Using Executors Class<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Most of the Important and most of the developer found one topic in java they perform a task and then they get destroyed. Purpose of multithreading is to perform multiple tasks simultaneously for effective utilization of our resources (time and CPU).</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-xyDQv183XtM/W_2RyiSJcxI/AAAAAAAAHFM/HLec9Gmu7m0NX3NOmsPv33xhXEuo86cZQCLcBGAs/s1600/FotoJet%2B%252822%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="285" src="https://4.bp.blogspot.com/-xyDQv183XtM/W_2RyiSJcxI/AAAAAAAAHFM/HLec9Gmu7m0NX3NOmsPv33xhXEuo86cZQCLcBGAs/s400/FotoJet%2B%252822%2529-min.jpg" width="400" /></a></div>
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Imagine that there are 10 tasks to perform, so using traditional multithreading mechanism we will need 10 threads. Do you see a drawback in this approach? I see it; creation of a new thread per task will cause resource overhead.</span><br />
<a name='more'></a></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Let me try to connect it with a real-life analogy, imagine in an IT company there are 10 tasks to be done, assigning 1 person 1 task will speed up the process but at the same time it will require 10 person, so a huge resource overhead, imagine what will happen if there are 1000 tasks will the company hire 1000 people. What if 5 persons could have also done the 10 tasks in similar time ie once resource got free he could have picked the next task and coincidentally this is the approach which is used in organizations for managing work.</span></div>
<div class="MsoNormal">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- programinjava-inarticle-new-ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="6977088426" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Same concept is used in thread pooling. Thread pool, means a group of thread ready to perform tasks as they come. The main USP of threads in threads pool are that threads are not destroyed but reused.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Let us assume 7 tasks come up.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><b>In traditional thread model:</b> 7 threads will be created for handling 7 tasks.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><b>Thread Pooling</b>: Let’s assume that we created a fixed thread pool of size 3, lets call these threads A, B, C. Now task 1 comes up and A picks it, now task2 comes up while A is handling it, so task2 is assigned to B, now task3 comes and both A and B are busy so task3 is assigned to C. Now if task4 comes up at this time and if any of A, B,C are free it will be auto assigned to any of free threads , only if all threads are busy then task4 will be queued and be assigned as soon as A,B,C . Do you see how effectively thread pooling handles resource overhead.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span> <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- programinjava-inarticle-new-ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="6977088426" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<h3 style="text-align: left;">
<span style="font-family: "trebuchet ms" , sans-serif;">Lets deep dive into how Thread Pooling is implemented in Java:</span></h3>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">To use thread pooling in java we have to use Executor or ThreadPoolExecutor class, which are present in java.util.concurrent package.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">In this blog we will see Executors class</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 11.0pt;">This class has 2 factory methods which are used to implement thread pooling along with some <b>fixed configurations</b>. <o:p></o:p></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 11.0pt;">The Executors class supports both Runnable and Callable interfaces. In Callable interface we return a value instead of void.<o:p></o:p></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 11.0pt;">The runnable tasks are submitted to the executor service using either execute() or submit() methods. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">The callable tasks are always submitted to the executor service using submit() method.</span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="Default">
<b><span style="font-family: "trebuchet ms" , sans-serif; font-size: 11.0pt;">public static ExecutorService newFixedThreadPool(int poolsize) <o:p></o:p></span></b></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">This method creates thread pool of fixed size as tasks are submitted, up to the poolSize, and then attempts to keep the pool size constant (adding new threads if a thread dies due to an unexpected Exception). If additional tasks are submitted when all threads are busy, they will wait in the queue until a thread is available.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span> <span style="font-family: "trebuchet ms" , sans-serif;">Also Read: <a href="https://www.programinjava.com/2018/02/java-thread-wait-notify-and-notifyall.html" target="_blank">Java thread wait, notify and notifyAll Methods Examples</a></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Let’s see a code example:</span></div>
<div>
<!-- HTML generated using hilite.me --><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.ExecutorService</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.Executors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Task</span> <span style="color: #008800; font-weight: bold;">implements</span> Runnable<span style="color: #333333;">{</span>
String name<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">Task</span><span style="color: #333333;">(</span>String name<span style="color: #333333;">)</span>
<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">=</span>name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">run</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Stubbed business logic of "</span><span style="color: #333333;">+</span><span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">+</span><span style="background-color: #fff0f0;">" done by:"</span><span style="color: #333333;">+</span>Thread<span style="color: #333333;">.</span><span style="color: #0000cc;">currentThread</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">());</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
Thread<span style="color: #333333;">.</span><span style="color: #0000cc;">sleep</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">*</span><span style="color: #0000dd; font-weight: bold;">1000</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">catch</span><span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span>
<span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>e<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ThreadPooling</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
ExecutorService threadPool<span style="color: #333333;">=</span>Executors<span style="color: #333333;">.</span><span style="color: #0000cc;">newFixedThreadPool</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">);</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Printing"</span><span style="color: #333333;">));</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Monitoring"</span><span style="color: #333333;">));</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Tracking"</span><span style="color: #333333;">));</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Billing"</span><span style="color: #333333;">));</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Managing"</span><span style="color: #333333;">));</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Transactioning"</span><span style="color: #333333;">));</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">shutdown</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre>
</div>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Output:</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Tracking done by:pool-1-thread-3</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Printing done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Monitoring done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Billing done by:pool-1-thread-3</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Managing done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Transactioning done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<h3 style="text-align: left;">
<span style="font-family: "trebuchet ms" , sans-serif;">Code Explanation:</span></h3>
</div>
<div class="MsoNormal">
<!-- HTML generated using hilite.me --><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;">ExecutorService threadPool<span style="color: #333333;">=</span>Executors<span style="color: #333333;">.</span><span style="color: #0000cc;">newFixedThreadPool</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">);</span>
</span></pre>
</div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Here a fixed thread pool of max size 3 is created. It returns an object of ExecutorService. This object can then be used to implement thread pooling.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">ExecutorService interface methods are: execute, submit, shutdown and shutDownNow. We will see there use below</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;">threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Printing"</span><span style="color: #333333;">));</span>
</span></pre>
</div>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Here execute method is used to run a task, by thread.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Now assume</span></div>
<div class="MsoNormal">
<ol style="text-align: left;">
<li><span style="font-family: "trebuchet ms" , sans-serif;"> 3 tasks are submitted</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;"> Assume all 3 threads are created and being used</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;"> Now the 4<sup>th</sup> task is submitted</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;"> The 4<sup>th</sup> task will wait in queue</span></li>
<li><span style="font-family: "trebuchet ms" , sans-serif;"> Assume thread 2 gets free then it will take up 4<sup>th</sup> task.</span></li>
</ol>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">So thread in thread pool will never die it will be reused.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<!-- HTML generated using hilite.me --><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;">threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">shutdown</span><span style="color: #333333;">();</span>
</span></pre>
</div>
</div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 10pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 10pt; line-height: 115%;">Threads in thread pool will not be destroyed explicitly, if we want to destroy threads in thread pool explicitly then we need to use thread pool.shutdown method.It will destroy threads gracefully once all threads are idle and no task in the queue.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #6a3e3e; font-size: 10.0pt; line-height: 115%;">threadPool</span><span style="font-size: 10pt; line-height: 115%;">.shutdownNow();<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 10pt; line-height: 115%;">It will terminate the threads immediately. It does not see if tasks are in queue or thread is processing a task so it does not do graceful termination.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 10pt; line-height: 115%;"><b>Note:</b><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 10pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<!-- HTML generated using hilite.me --><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;">ExecutorService threadPool<span style="color: #333333;">=</span>Executors<span style="color: #333333;">.</span><span style="color: #0000cc;">newFixedThreadPool</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">);</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Printing"</span><span style="color: #333333;">));</span>
</span></pre>
</div>
</div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt; line-height: 115%;"><br /></span> <span style="font-size: 10pt; line-height: 115%;">In case only 1 task is submitted then at that time only 1 thread will be created. (Don’t be confused that at first time only 3 threads are created, specifying 3 means max 3 threads will be created, irrespective of a number of tasks being submitted.)<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 10pt; line-height: 115%;">2<sup>nd</sup> thread will be created only when 1<sup>st</sup> thread is busy in doing the task.<o:p></o:p></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span> <span style="font-family: "trebuchet ms" , sans-serif;">Also Read: <a href="https://www.programinjava.com/2018/02/printing-numbers-in-sequence-from.html" target="_blank">Printing Numbers in Sequence from alternating Threads in java</a></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="Default">
<b><span style="font-family: "trebuchet ms" , sans-serif; font-size: 11.0pt;">public static ExecutorService newCachedThreadPool() <o:p></o:p></span></b></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="Default">
<span style="font-family: "trebuchet ms" , sans-serif; font-size: 11.0pt;">A cached thread pool differs from fixed thread pool in 2 ways.<o:p></o:p></span></div>
<div class="Default" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="font-size: 11.0pt; mso-fareast-font-family: Calibri;">1)<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: normal; line-height: normal;"> </span></span></b><!--[endif]--><span style="font-size: 11.0pt; mso-bidi-font-weight: bold;">In fixed thread pool we had to specify no of threads eg in last case we said 3 and now if 6 tasks came 3 threads could effectively manage it, what if 60 tasks came then 3 would not have been idle, so in fixed thread pool we need to know number of tasks and then define the number of threads to manage thread pooling. What if I want to be free from this responsibility, cached thread pool offers us this flexibility.No need to specify the number of threads, it will automatically create threads. ( Analogous to Cloud, where resources grow and shrink based on demand, fixed thread pool is more like on premise systems)</span><span style="font-size: 11.0pt;"><o:p></o:p></span></span></div>
<div class="Default" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-family: "trebuchet ms" , sans-serif;"><b><span style="font-size: 11.0pt; mso-fareast-font-family: Calibri;">2)<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: normal; line-height: normal;"> </span></span></b><!--[endif]--><span style="font-size: 11.0pt; mso-bidi-font-weight: bold;">Secondly it will automatically remove idle threads (by default 60s), when there is no pending tasks in queue and thread has not been doing any task, no need to use shutdown method</span><span style="font-size: 11.0pt;"><o:p></o:p></span></span></div>
<div class="Default" style="margin-left: .5in;">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Thus it is more flexible. Let’s see a code example</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<!-- HTML generated using hilite.me --><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Task</span> <span style="color: #008800; font-weight: bold;">implements</span> Runnable<span style="color: #333333;">{</span>
String name<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">Task</span><span style="color: #333333;">(</span>String name<span style="color: #333333;">)</span>
<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">=</span>name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">run</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Stubbed business logic of "</span><span style="color: #333333;">+</span><span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span><span style="color: #333333;">+</span><span style="background-color: #fff0f0;">" done by:"</span><span style="color: #333333;">+</span>Thread<span style="color: #333333;">.</span><span style="color: #0000cc;">currentThread</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ThreadPooling</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">throws</span> InterruptedException <span style="color: #333333;">{</span>
ExecutorService threadPool<span style="color: #333333;">=</span>Executors<span style="color: #333333;">.</span><span style="color: #0000cc;">newCachedThreadPool</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><=</span><span style="color: #0000dd; font-weight: bold;">20</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span>
<span style="color: #333333;">{</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Task"</span><span style="color: #333333;">+</span>i<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre>
</div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Output</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task2 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task1 done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task4 done by:pool-1-thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task8 done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task7 done by:pool-1-thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task5 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task6 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task13 done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task12 done by:pool-1-thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task10 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task16 done by:pool-1-thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task15 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task18 done by:pool-1-thread-8</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task19 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task11 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task20 done by:pool-1-thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task17 done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task9 done by:pool-1-thread-6</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task3 done by:pool-1-thread-3</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task14 done by:pool-1-thread-7</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Here if we analyse automatically 20 tasks are handled by 8 threads in pool.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Comparing it will fixed pool, if we used the code</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;">ExecutorService threadPool<span style="color: #333333;">=</span>Executors<span style="color: #333333;">.</span><span style="color: #0000cc;">newFixedThreadPool</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><=</span><span style="color: #0000dd; font-weight: bold;">20</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span>
<span style="color: #333333;">{</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Task"</span><span style="color: #333333;">+</span>i<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
threadPool<span style="color: #333333;">.</span><span style="color: #0000cc;">shutdown</span><span style="color: #333333;">();</span>
</span></pre>
</div>
</div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Then 5 threads would have taken care of handling these 20 tasks</span></div>
<div class="MsoNormal">
<h4 style="text-align: left;">
<b><span style="font-family: "trebuchet ms" , sans-serif;">Sample Output</span></b></h4>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task2 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task5 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task7 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task8 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task9 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task10 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task11 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task12 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task13 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task14 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task15 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task16 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task17 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task18 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task19 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task20 done by:pool-1-thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task4 done by:pool-1-thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task1 done by:pool-1-thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task3 done by:pool-1-thread-3</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of Task6 done by:pool-1-thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;"><br /></span></span> <span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;"></span></span><br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;"> <!-- inArticle-Programinjava --> </span></span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;"><ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></span></span></div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">If no thread pooling had been used ( Traditional multithreading )</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<!-- HTML generated using hilite.me --><span style="font-family: "trebuchet ms" , sans-serif;"><br /></span>
<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: "trebuchet ms" , sans-serif;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><=</span><span style="color: #0000dd; font-weight: bold;">20</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
Thread t<span style="color: #333333;">=</span><span style="color: #008800; font-weight: bold;">new</span> Thread<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Task<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"task"</span><span style="color: #333333;">+</span>i<span style="color: #333333;">));</span>
t<span style="color: #333333;">.</span><span style="color: #0000cc;">start</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre>
</div>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">Then automatically 20 threads would have been created:</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task1 done by:Thread-0</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task2 done by:Thread-1</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task5 done by:Thread-4</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task3 done by:Thread-2</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task4 done by:Thread-3</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task9 done by:Thread-8</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task7 done by:Thread-6</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task8 done by:Thread-7</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task6 done by:Thread-5</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task12 done by:Thread-11</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task11 done by:Thread-10</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task10 done by:Thread-9</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task14 done by:Thread-13</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task13 done by:Thread-12</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task18 done by:Thread-17</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task15 done by:Thread-14</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task20 done by:Thread-19</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task17 done by:Thread-16</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task16 done by:Thread-15</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "trebuchet ms" , sans-serif;"><span style="font-size: 10pt;">Stubbed business logic of task19 done by:Thread-18</span><span style="font-size: 10.0pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span> <span style="font-family: "trebuchet ms" , sans-serif;"></span><br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><span style="font-family: "trebuchet ms" , sans-serif;"> <!-- inArticle-Programinjava --> </span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></span></div>
<br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">So lets tabulate the result</span></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="border-collapse: collapse; border: none; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;">
<tbody>
<tr>
<td style="border: solid black 1.0pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-themecolor: text1; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="319"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "trebuchet ms" , sans-serif;">No thread pool</span></div>
</td>
<td style="border-left: none; border: solid black 1.0pt; mso-border-alt: solid black .5pt; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-themecolor: text1; mso-border-themecolor: text1; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="319"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "trebuchet ms" , sans-serif;">20 threads created</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid black 1.0pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="319"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "trebuchet ms" , sans-serif;">With cached pool</span></div>
</td>
<td style="border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="319"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "trebuchet ms" , sans-serif;">8 threads JVM created</span></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid black 1.0pt; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="319"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "trebuchet ms" , sans-serif;">With fixed pool</span></div>
</td>
<td style="border-bottom: solid black 1.0pt; border-left: none; border-right: solid black 1.0pt; border-top: none; mso-border-alt: solid black .5pt; mso-border-bottom-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-right-themecolor: text1; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; padding: 0in 5.4pt 0in 5.4pt; width: 239.4pt;" valign="top" width="319"><div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: "trebuchet ms" , sans-serif;">We need to give no of threads eg we gave 5 for 20 and it effectively managed, so we need to use our experience to determine no of threads or use cached pool</span></div>
</td>
</tr>
</tbody></table>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<h3 style="text-align: left;">
<span style="font-family: "trebuchet ms" , sans-serif;">Number of threads:</span></h3>
</div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">So 20 (traditional) vs 8 (thread pool), seems a great performance improvement here.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">I believe you are now able to understand the power of thread pooling.</span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;">We will see more detail about <b>ThreadPoolExecutor</b> class and Callable interface in later posts.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span> <span style="font-family: "trebuchet ms" , sans-serif;">If you have any issue please leave us a comment.</span><br />
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: "trebuchet ms" , sans-serif;"><br /></span></div>
<div class="MsoNormal">
<br /></div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-80108738660317673422018-09-02T23:50:00.000+05:302018-09-02T23:50:40.567+05:30Remote Debugging of Java Application deployed on AWS Elastic Beanstalk<div dir="ltr" style="text-align: left;" trbidi="on">
Most of the time we stuck in the situation where we need to do remote debugging of the application which is running on some server or somewhere other than localhost. We need to debug the code in eclipse. To do that we need to allow the running application to allow remote debugging. <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-_OGDRMG9o2o/W4wo3xL5aZI/AAAAAAAAG7Y/RfZXRoovffQqKrjezRCrvBbjeq5MFrJQgCLcBGAs/s1600/FotoJet%2B%25288%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-_OGDRMG9o2o/W4wo3xL5aZI/AAAAAAAAG7Y/RfZXRoovffQqKrjezRCrvBbjeq5MFrJQgCLcBGAs/s320/FotoJet%2B%25288%2529-min.jpg" width="320" /></a></div>
<div>
<br /></div>
<div>
Generally, we know how to do remote debug in eclipse while the code is running on tomcat. In this post, we will see how to do remote debug when the application is deployed over AWS by elastic beanstalk.</div>
<div>
<br /></div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<br /></div>
<div>
Let's start.</div>
<div>
<br /></div>
<div>
To do Remote debug of AWS deployed the application, we need to follow below steps.</div>
<div>
<br /></div>
<div>
1)We need to Go to <b>AWS Console -> Elastic Beanstalk -> Application -> Configuration</b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-t72jB80PEHY/W4wlPTb7JFI/AAAAAAAAG7M/Gnf0IrLfjqIf3Dr-wrwkOIrm8EXUdTEPwCLcBGAs/s1600/temp.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="554" data-original-width="1600" height="220" src="https://2.bp.blogspot.com/-t72jB80PEHY/W4wlPTb7JFI/AAAAAAAAG7M/Gnf0IrLfjqIf3Dr-wrwkOIrm8EXUdTEPwCLcBGAs/s640/temp.JPG" width="640" /></a></div>
<div>
<br /></div>
<div>
2) Under Configuration you need to go to <b>Software ->Modify ->Container Options </b></div>
<div>
Now in Container Options, search for <i style="font-weight: bold;">JVM options </i> In this text box you need to write the below arguments.</div>
<div>
<pre style="color: #333333; line-height: 16.25px;"><span style="color: #555555;">-</span>Xdebug <span style="color: #555555;">-</span><span style="color: #9999ff;">Xrunjdwp:</span>transport<span style="color: #555555;">=</span>dt_socket<span style="color: #555555;">,</span>server<span style="color: #555555;">=</span>y<span style="color: #555555;">,</span>suspend<span style="color: #555555;">=</span>n<span style="color: #555555;">,</span>address<span style="color: #555555;">=</span><span style="color: #ff6600;">5005</span></pre>
</div>
<div>
Now, these arguments say that - The remote debug will be listening at 5005 port.</div>
<div>
<br /></div>
<div>
3) Now you need to Go to EC2 Instance running from AWS Console -> EC2<br />
<br /></div>
<div>
4) After that you need to check which security group is defined for that EC2 Instance, you need to allow this port to communicate outside AWS.<br />
<br /></div>
<div>
5)For that, you need to add the Port in Inbound and Outbound of the Security Group</div>
<div>
Select the <b>Custom Protocol </b>and define the Port as 5005 and then define Anywhere in Ip accessible.</div>
<div>
<br /></div>
<div>
After this, you will be able to communicate the port with the local debugger of the eclipse.</div>
<div>
<br /></div>
<div>
Now you need to configure the Local Eclipse environment for Remote debugging.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- learnspringboot-inline -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="3701464102" data-full-width-responsive="true" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<h3 style="text-align: left;">
<b>Configure Local Eclipse </b></h3>
</div>
<div>
<b><br /></b></div>
<div>
For this you need to go to <b class="">Run</b><b> </b>-><b>Debug Configuration</b> -><b> Remote debugging </b></div>
<div>
<b><br /></b></div>
<div>
Under that define the IP of the application ( <b>you need to find the IP of the application at EC2 Instance Properties </b>)</div>
<div>
<br /></div>
<div>
Define the port as 5005 ( as defined above).</div>
<div>
<br /></div>
<div>
Run the application and hurray, are now connected with the AWS application running.<br />
<br />
Now you will be able to debug remotely.<br />
<br />
Hope you like the post, If so please share it with your friends and family.<br />
<br />
Thanks for reading<br />
Noeik</div>
<div>
<br /></div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-49861074233154170002018-08-18T14:26:00.000+05:302018-08-18T14:28:00.602+05:30Upload File on AWS S3 Using Java<div dir="ltr" style="text-align: left;" trbidi="on">
Most of us know nowadays that AWS is used for almost everything because of its cheap price as well its availability.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-4m4B6xz9lps/W3fek8DNk4I/AAAAAAAAG6I/2vYh9Q1b0UwHfxGW5BB7EWhV96yKw9XQQCLcBGAs/s1600/FotoJet%2B%25283%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://3.bp.blogspot.com/-4m4B6xz9lps/W3fek8DNk4I/AAAAAAAAG6I/2vYh9Q1b0UwHfxGW5BB7EWhV96yKw9XQQCLcBGAs/s320/FotoJet%2B%25283%2529-min.jpg" width="320" /></a></div>
<br />
<div>
AWS S3 is an online storage for storing file and images and zip, literally everything you want to put. <script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<br /></div>
<div>
Most of us have some use cases where we want to upload the image to aws s3 so that it can we used anywhere we want.</div>
<div>
<br /></div>
<div>
<b>#Usercase </b>we need to get the file or Image from UI and need to upload it to AWS S3 using java.</div>
<div>
<br /></div>
<div>
<b>#Approach </b>To Achieve it </div>
<div>
<ul style="text-align: left;">
<li>first, need to add the AWS SDK for Java </li>
<li>Then we need to Get the client of AWS which is basically creating a connection with AWS </li>
<li>After successfully getting the connection we will use s3 API to put the image as an object in AWS.</li>
</ul>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<b><br /></b>
<b>Also Read : </b><a href="https://www.programinjava.com/2018/05/file-uploading-in-spring-mvc-using.html" target="_blank">File Uploading in Spring MVC using thymeleaf and spring boot</a><br />
<b>#Implementation</b></div>
<div>
We first need to Add the dependency of AWS SDK in our project ( maven dependency as below )</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f3f3; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"> <span style="color: #330099; font-weight: bold;"><dependency></span>
<span style="color: #330099; font-weight: bold;"><groupId></span>com.amazonaws<span style="color: #330099; font-weight: bold;"></groupId></span>
<span style="color: #330099; font-weight: bold;"><artifactId></span>aws-java-sdk<span style="color: #330099; font-weight: bold;"></artifactId></span>
<span style="color: #330099; font-weight: bold;"><version></span>1.11.388<span style="color: #330099; font-weight: bold;"></version></span>
<span style="color: #330099; font-weight: bold;"></dependency></span>
</pre>
</div>
</div>
<br />
After this, we need to have the AWS Account Access key and access_id<br />
you can get it from <a href="https://console.aws.amazon.com/iam/home?#/security_credential">https://console.aws.amazon.com/iam/home?#/security_credential</a><br />
<br />
Access key file will look like below<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f3f3; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">[default]
aws_access_key_id=<Your-Access-key_id>
aws_secret_access_key=<Your Secret_access_key>
</pre>
</div>
Note change <b><Your-Access-key_id></b> and <b><Your Secret_access_key></b> values with your access key.<br />
<br />
Now you need to save the access key file at a location below.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f3f3; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">~/.aws/credentials on Linux, macOS, or Unix
C:\Users\<span style="color: red;">USERNAME</span> \.aws\credentials on Windows
</pre>
</div>
<br />
Now let see the Code for the Uploading of Image /File on AWS. (Ref : <a href="https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html" target="_blank">here</a> )<br />
we will create one jsp file where we create upload file and call the spring controller , under the controller, you can see the file and it will store to the aws s3.<br />
<br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br />
<br />
Thyemleaf Html Code<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><!DOCTYPE html></span>
<span style="color: #007700;"><html</span> <span style="color: #0000cc;">xmlns:th=</span><span style="background-color: #fff0f0;">"http://thymeleaf.org"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><head></span>
<span style="color: #007700;"><title></span>File Upload Example<span style="color: #007700;"></title></span>
<span style="color: #007700;"><link</span> <span style="color: #0000cc;">href=</span><span style="background-color: #fff0f0;">"webjars/bootstrap/3.3.7/css/bootstrap.min.css"</span>
<span style="color: #0000cc;">rel=</span><span style="background-color: #fff0f0;">"stylesheet"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"><script </span><span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"text/javascript"</span> <span style="color: #0000cc;">src=</span><span style="background-color: #fff0f0;">"webjars/jquery/3.3.1/jquery.min.js"</span><span style="color: #007700;">></script></span>
<span style="color: #007700;"><script </span><span style="color: #0000cc;">src=</span><span style="background-color: #fff0f0;">"webjars/bootstrap/3.3.7/js/bootstrap.min.js"</span><span style="color: #007700;">></script></span>
<span style="color: #007700;"></head></span>
<span style="color: #007700;"><script </span><span style="color: #0000cc;">th:inline=</span><span style="background-color: #fff0f0;">"javascript"</span><span style="color: #007700;">></span>
<span style="color: #888888;">/*<![CDATA[*/</span>
<span style="color: #008800; font-weight: bold;">var</span> _validFileExtensions <span style="color: #333333;">=</span> [ <span style="background-color: #fff0f0;">".csv"</span>, <span style="background-color: #fff0f0;">".zip"</span> ,<span style="background-color: #fff0f0;">".png"</span> ];
<span style="color: #008800; font-weight: bold;">function</span> Validate(oForm) {
<span style="color: #008800; font-weight: bold;">var</span> arrInputs <span style="color: #333333;">=</span> oForm.getElementsByTagName(<span style="background-color: #fff0f0;">"input"</span>);
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #008800; font-weight: bold;">var</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>; i <span style="color: #333333;"><</span> arrInputs.length; i<span style="color: #333333;">++</span>) {
<span style="color: #008800; font-weight: bold;">var</span> oInput <span style="color: #333333;">=</span> arrInputs[i];
<span style="color: #008800; font-weight: bold;">if</span> (oInput.type <span style="color: #333333;">==</span> <span style="background-color: #fff0f0;">"file"</span>) {
<span style="color: #008800; font-weight: bold;">var</span> sFileName <span style="color: #333333;">=</span> oInput.value;
<span style="color: #008800; font-weight: bold;">if</span> (sFileName.length <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span>) {
alert(<span style="background-color: #fff0f0;">"Please select a file to upload"</span>);
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;
}
<span style="color: #008800; font-weight: bold;">if</span> (sFileName.length <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">0</span>) {
<span style="color: #008800; font-weight: bold;">var</span> blnValid <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">false</span>;
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #008800; font-weight: bold;">var</span> j <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>; j <span style="color: #333333;"><</span> _validFileExtensions.length; j<span style="color: #333333;">++</span>) {
<span style="color: #008800; font-weight: bold;">var</span> sCurExtension <span style="color: #333333;">=</span> _validFileExtensions[j];
<span style="color: #008800; font-weight: bold;">if</span> (sFileName.substr(
sFileName.length <span style="color: #333333;">-</span> sCurExtension.length,
sCurExtension.length).toLowerCase() <span style="color: #333333;">==</span> sCurExtension
.toLowerCase()) {
blnValid <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">true</span>;
<span style="color: #008800; font-weight: bold;">break</span>;
}
}
<span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333333;">!</span>blnValid) {
alert(<span style="background-color: #fff0f0;">"Invalid File Extension"</span>);
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;
}
}
}
}
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span>;
}
<span style="color: #888888;">/*]]>*/</span>
<span style="color: #007700;"></script></span>
<span style="color: #007700;"><body></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"container-fluid padding-0"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"row padding-0"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-4"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><h2></span>File Upload Example<span style="color: #007700;"></h2></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-4"</span> <span style="color: #0000cc;">align=</span><span style="background-color: #fff0f0;">"right"</span><span style="color: #007700;">></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><nav</span> <span style="color: #0000cc;">role=</span><span style="background-color: #fff0f0;">"navigation"</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"trainingset-container-id"</span>
<span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"navbar navbar-default"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"row"</span> <span style="color: #0000cc;">style=</span><span style="background-color: #fff0f0;">"margin-top: 10px;"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-2"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><B></span>Upload File<span style="color: #007700;"></B></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-6"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><form</span> <span style="color: #0000cc;">method=</span><span style="background-color: #fff0f0;">"POST"</span> <span style="color: #0000cc;">action=</span><span style="background-color: #fff0f0;">"/upload"</span>
<span style="color: #0000cc;">onsubmit=</span><span style="background-color: #fff0f0;">"return Validate(this);"</span> <span style="color: #0000cc;">enctype=</span><span style="background-color: #fff0f0;">"multipart/form-data"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-sm-6"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"file"</span> <span style="color: #0000cc;">name=</span><span style="background-color: #fff0f0;">"file"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-sm-6"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"submit"</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"btn btn-success btn-sm"</span> <span style="color: #0000cc;">value=</span><span style="background-color: #fff0f0;">"Upload data"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></form></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></nav></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"messageboxid"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"uploadstatus"</span> <span style="color: #0000cc;">th:if=</span><span style="background-color: #fff0f0;">"${message}"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><B></span>Status Of Uploaded File<span style="color: #007700;"></B></span>
<span style="color: #007700;"><h6</span> <span style="color: #0000cc;">th:text=</span><span style="background-color: #fff0f0;">"${message}"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></body></span>
<span style="color: #007700;"></html></span>
</pre>
</div>
This will see like this<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-wMHFDgC_FsM/W3fcbzcFofI/AAAAAAAAG5w/7Kw-dUi0vwQu0m2keDv2WkKlgxm_erxKQCLcBGAs/s1600/webscreen-1.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="735" data-original-width="1600" height="292" src="https://3.bp.blogspot.com/-wMHFDgC_FsM/W3fcbzcFofI/AAAAAAAAG5w/7Kw-dUi0vwQu0m2keDv2WkKlgxm_erxKQCLcBGAs/s640/webscreen-1.JPG" width="640" /></a></div>
<br />
Now we will see the Upload Controller Code.<br />
<br />
<b>Upload Controller.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f3f3; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #006699; font-weight: bold;">package</span> com<span style="color: #555555;">.</span><span style="color: #330099;">programinjava</span><span style="color: #555555;">.</span><span style="color: #330099;">learn</span><span style="color: #555555;">.</span><span style="color: #330099;">controller</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">java.io.InputStream</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.stereotype.Controller</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.ui.ModelMap</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.web.bind.annotation.GetMapping</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.web.bind.annotation.PostMapping</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.web.bind.annotation.RequestParam</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.web.multipart.MultipartFile</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">org.springframework.web.servlet.mvc.support.RedirectAttributes</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.auth.profile.ProfileCredentialsProvider</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.regions.Regions</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.services.s3.AmazonS3</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.services.s3.AmazonS3ClientBuilder</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.services.s3.model.CannedAccessControlList</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.services.s3.model.ObjectMetadata</span><span style="color: #555555;">;</span>
<span style="color: #006699; font-weight: bold;">import</span> <span style="color: #00ccff; font-weight: bold;">com.amazonaws.services.s3.model.PutObjectRequest</span><span style="color: #555555;">;</span>
<span style="color: #9999ff;">@Controller</span>
<span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">class</span> <span style="color: #00aa88; font-weight: bold;">UploadController</span> <span style="color: #555555;">{</span>
<span style="color: #9999ff;">@PostMapping</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"/upload"</span><span style="color: #555555;">)</span>
<span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #cc00ff;">singleFileUpload</span><span style="color: #555555;">(</span><span style="color: #9999ff;">@RequestParam</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"file"</span><span style="color: #555555;">)</span> MultipartFile file<span style="color: #555555;">,</span> RedirectAttributes redirectAttributes<span style="color: #555555;">)</span> <span style="color: #555555;">{</span>
<span style="color: #006699; font-weight: bold;">if</span> <span style="color: #555555;">(</span>file<span style="color: #555555;">.</span><span style="color: #330099;">isEmpty</span><span style="color: #555555;">())</span> <span style="color: #555555;">{</span>
redirectAttributes<span style="color: #555555;">.</span><span style="color: #330099;">addFlashAttribute</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"message"</span><span style="color: #555555;">,</span> <span style="color: #cc3300;">"Please select a file to upload"</span><span style="color: #555555;">);</span>
<span style="color: #006699; font-weight: bold;">return</span> <span style="color: #cc3300;">"redirect:uploadStatus"</span><span style="color: #555555;">;</span>
<span style="color: #555555;">}</span>
<span style="color: #0099ff; font-style: italic;">// bucket name </span>
String bucketName <span style="color: #555555;">=</span><span style="color: #cc3300;">"atserve-photos"</span><span style="color: #555555;">;</span>
<span style="color: #0099ff; font-style: italic;">// get it from user or change it </span>
String nameOffile <span style="color: #555555;">=</span><span style="color: #cc3300;">"myPhoto"</span><span style="color: #555555;">;</span>
<span style="color: #0099ff; font-style: italic;">// getting aws access </span>
AmazonS3 s3Client <span style="color: #555555;">=</span> AmazonS3ClientBuilder<span style="color: #555555;">.</span><span style="color: #330099;">standard</span><span style="color: #555555;">()</span>
<span style="color: #555555;">.</span><span style="color: #330099;">withRegion</span><span style="color: #555555;">(</span>Regions<span style="color: #555555;">.</span><span style="color: #330099;">AP_SOUTH_1</span><span style="color: #555555;">)</span>
<span style="color: #555555;">.</span><span style="color: #330099;">withCredentials</span><span style="color: #555555;">(</span><span style="color: #006699; font-weight: bold;">new</span> ProfileCredentialsProvider<span style="color: #555555;">())</span>
<span style="color: #555555;">.</span><span style="color: #330099;">build</span><span style="color: #555555;">();</span>
<span style="color: #007788; font-weight: bold;">boolean</span> isBucketExist <span style="color: #555555;">=</span>s3Client<span style="color: #555555;">.</span><span style="color: #330099;">doesBucketExist</span><span style="color: #555555;">(</span>bucketName<span style="color: #555555;">);</span>
<span style="color: #006699; font-weight: bold;">if</span><span style="color: #555555;">(!</span>isBucketExist<span style="color: #555555;">)</span> <span style="color: #555555;">{</span>
s3Client<span style="color: #555555;">.</span><span style="color: #330099;">createBucket</span><span style="color: #555555;">(</span>bucketName<span style="color: #555555;">);</span>
<span style="color: #555555;">}</span>
<span style="color: #006699; font-weight: bold;">try</span> <span style="color: #555555;">{</span>
InputStream is <span style="color: #555555;">=</span> file<span style="color: #555555;">.</span><span style="color: #330099;">getInputStream</span><span style="color: #555555;">();</span>
s3Client<span style="color: #555555;">.</span><span style="color: #330099;">putObject</span><span style="color: #555555;">(</span><span style="color: #006699; font-weight: bold;">new</span> PutObjectRequest<span style="color: #555555;">(</span>bucketName<span style="color: #555555;">,</span>nameOffile<span style="color: #555555;">,</span>is<span style="color: #555555;">,</span><span style="color: #006699; font-weight: bold;">new</span> ObjectMetadata<span style="color: #555555;">()).</span><span style="color: #330099;">withCannedAcl</span><span style="color: #555555;">(</span>CannedAccessControlList<span style="color: #555555;">.</span><span style="color: #330099;">PublicRead</span><span style="color: #555555;">));</span>
redirectAttributes<span style="color: #555555;">.</span><span style="color: #330099;">addFlashAttribute</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"message"</span><span style="color: #555555;">,</span> <span style="color: #cc3300;">"SuccessFully Uploaded On AWS S3"</span><span style="color: #555555;">);</span>
<span style="color: #555555;">}</span><span style="color: #006699; font-weight: bold;">catch</span><span style="color: #555555;">(</span>Exception e<span style="color: #555555;">)</span> <span style="color: #555555;">{</span>
e<span style="color: #555555;">.</span><span style="color: #330099;">printStackTrace</span><span style="color: #555555;">();</span>
<span style="color: #555555;">}</span>
<span style="color: #006699; font-weight: bold;">return</span> <span style="color: #cc3300;">"redirect:/uploadStatus"</span><span style="color: #555555;">;</span>
<span style="color: #555555;">}</span>
<span style="color: #9999ff;">@GetMapping</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"/uploadStatus"</span><span style="color: #555555;">)</span>
<span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #cc00ff;">uploadStatus</span><span style="color: #555555;">(</span>ModelMap m<span style="color: #555555;">)</span> <span style="color: #555555;">{</span>
<span style="color: #006699; font-weight: bold;">return</span> <span style="color: #cc3300;">"Homepage"</span><span style="color: #555555;">;</span>
<span style="color: #555555;">}</span>
<span style="color: #9999ff;">@GetMapping</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"/upload"</span><span style="color: #555555;">)</span>
<span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #cc00ff;">displayHomePageForAlarm</span><span style="color: #555555;">()</span> <span style="color: #555555;">{</span>
<span style="color: #006699; font-weight: bold;">return</span> <span style="color: #cc3300;">"Homepage"</span><span style="color: #555555;">;</span>
<span style="color: #555555;">}</span>
<span style="color: #555555;">}</span>
</pre>
</div>
<br />
Let see what we get on AWS S3<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-QACAk74y-XE/W3fck36pE1I/AAAAAAAAG54/ECDtGIP7iPQPsMzfDhTTk--gyYDc4cp3gCLcBGAs/s1600/webscreen-2.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="428" data-original-width="1600" height="170" src="https://3.bp.blogspot.com/-QACAk74y-XE/W3fck36pE1I/AAAAAAAAG54/ECDtGIP7iPQPsMzfDhTTk--gyYDc4cp3gCLcBGAs/s640/webscreen-2.JPG" width="640" /></a></div>
<br />
So this is how we can see the myPhoto is now uploaded on AWS S3.<br />
<br />
DOWNOAD THE CODE :<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://github.com/programinjava/FileUploadInAWSS3Demo.git" target="_blank"><img border="0" data-original-height="440" data-original-width="880" height="160" src="https://3.bp.blogspot.com/-IDpo5NRDdjM/WfiSwQ8yTJI/AAAAAAAAF9o/FwJ4NZcqP4YtSQI7wtPdwGmfOfOXu0IugCPcBGAYYCw/s320/Pure-CSS-Download-Button-Hover-Animation.gif" width="320" /></a></div>
<br />
<br />
Also, learn<br />
<ul style="background: rgb(255, 255, 255); border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; list-style-type: disc; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;">LinkedList</li>
<ul style="background: transparent; border: 0px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; list-style-type: disc; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/03/program-to-find-nth-element-of-linkedlist-from-end.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Program to find the Nth Element of a Singly Linked list from the end in java</a></li>
<li style="background: transparent; border: 0px; list-style-type: disc; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/01/programgeneric-implementation-of-custom.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">[Program]Generic Implementation of custom linked list in java</a></li>
<li style="background: transparent; border: 0px; list-style-type: disc; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2017/07/program-of-linked-list-insertion-and.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Program of Linked List Insertion and Deletion Operations in java</a></li>
</ul>
<li style="background: transparent; border: 0px; list-style-type: disc; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2016/05/stack-implementation-program-in-java_31.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Stack Implementation program in java</a></li>
<li style="background: transparent; border: 0px; list-style-type: disc; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2017/02/breadth-first-traversalbft-and-depth.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Breadth first Traversal(BFT) AND Depth First Traversal(DST) Program in java</a></li>
</ul>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-70848876481717056992018-08-08T00:08:00.003+05:302018-08-08T00:08:32.708+05:30Mocking of static method in unit testing<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-TDZ02tPb9sM/W2nmyclNL-I/AAAAAAAAG4s/OgcZJhwzkT0Z_-Ty3PV2RUWU8nMuAe4DQCLcBGAs/s1600/FotoJet%2B%25281%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-TDZ02tPb9sM/W2nmyclNL-I/AAAAAAAAG4s/OgcZJhwzkT0Z_-Ty3PV2RUWU8nMuAe4DQCLcBGAs/s320/FotoJet%2B%25281%2529-min.jpg" width="320" /></a></div>
<br />
While we do unit testing, we mostly encounter the situation where we need to do the unit testing of a method which is calling a static method. if we are using mockito for doing mocking, We will have to suffer as mockito don't provide Static method mocking.<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
Let's see in a simple manner what is required and how we achieve it.</div>
<div>
<br /></div>
<div>
<b># Objective: </b>We need to mock the static class which is being used in a method for which we are writing unit test.</div>
<div>
<br /></div>
<div>
<b># Solution: </b>We have to use <b>Powermockito </b>for that </div>
<div>
<br /></div>
<div>
<b>What is PowerMockito?</b></div>
<div>
PowerMockito is a PowerMock’s extension API to support Mockito. It provides capabilities to work with the Java Reflection API in a simple way to overcome the problems of Mockito, such as the lack of ability to mock final, static or private methods.</div>
<div>
<br /></div>
<div>
Getting Start with Powermockito<br />
<br />
Also read: <a href="https://www.programinjava.com/2017/11/junit-tests-basics-and-how-to-do-unit.html" target="_blank">JUnit Tests Basics and how to do unit testing in java</a></div>
<div>
<br /></div>
<div>
First, you need to add the dependency in pom.xml</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.powermock<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>powermock-module-junit4<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>1.6.4<span style="color: #007700;"></version></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.powermock<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>powermock-api-mockito<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>1.6.4<span style="color: #007700;"></version></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
</pre>
</div>
</div>
Now as you are using powermockito , you need to change the JUNIT Runner class as<br />
<pre style="line-height: 16.25px;"><span style="color: #f6b26b;">@RunWith(PowerMockRunner.class)
@PrepareForTest(<StaticClassName>.class)</span></pre>
<StaticClassName> will be replace with you static class Name.<br />
<br />
Now let see the example.<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Project Structure<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-gudlG637dgg/W2nkVbOY7RI/AAAAAAAAG4g/XPrX6EAWudw3YIhWQnWyZGbsTOhNdwgxQCLcBGAs/s1600/temp.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="442" data-original-width="472" height="299" src="https://3.bp.blogspot.com/-gudlG637dgg/W2nkVbOY7RI/AAAAAAAAG4g/XPrX6EAWudw3YIhWQnWyZGbsTOhNdwgxQCLcBGAs/s320/temp.JPG" width="320" /></a></div>
Now let see the class for with we will write test case.<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Rectangle</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">getAreaOfRectangle</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> length <span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> breadth<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> AreaFinder<span style="color: #333333;">.</span><span style="color: #0000cc;">findArea</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"rectangle"</span><span style="color: #333333;">,</span> length<span style="color: #333333;">,</span> breadth<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
The Static Class<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">AreaFinder</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">findArea</span><span style="color: #333333;">(</span>String type <span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> length <span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> breadth<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>type<span style="color: #333333;">.</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"rectangle"</span><span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> length<span style="color: #333333;">*</span>breadth<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span> <span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>type<span style="color: #333333;">.</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"square"</span><span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> length<span style="color: #333333;">*</span>length<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
Test Class<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #f0f3f3; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #9999ff;">@RunWith</span><span style="color: #555555;">(</span>PowerMockRunner<span style="color: #555555;">.</span><span style="color: #330099;">class</span><span style="color: #555555;">)</span>
<span style="color: #9999ff;">@PrepareForTest</span><span style="color: #555555;">(</span>AreaFinder<span style="color: #555555;">.</span><span style="color: #330099;">class</span><span style="color: #555555;">)</span> <span style="color: #0099ff; font-style: italic;">// need to add the static class for which you mock the method</span>
<span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">class</span> <span style="color: #00aa88; font-weight: bold;">RectangleTest</span> <span style="color: #555555;">{</span>
<span style="color: #9999ff;">@Test</span>
<span style="color: #006699; font-weight: bold;">public</span> <span style="color: #007788; font-weight: bold;">void</span> <span style="color: #cc00ff;">getAreaOfRectangleTest</span><span style="color: #555555;">()</span> <span style="color: #555555;">{</span>
Rectangle rec <span style="color: #555555;">=</span> <span style="color: #006699; font-weight: bold;">new</span> Rectangle<span style="color: #555555;">();</span>
<span style="color: #007788; font-weight: bold;">int</span> length <span style="color: #555555;">=</span><span style="color: #ff6600;">10</span><span style="color: #555555;">;</span>
<span style="color: #007788; font-weight: bold;">int</span> breadth <span style="color: #555555;">=</span><span style="color: #ff6600;">20</span><span style="color: #555555;">;</span>
PowerMockito<span style="color: #555555;">.</span><span style="color: #330099;">mockStatic</span><span style="color: #555555;">(</span>AreaFinder<span style="color: #555555;">.</span><span style="color: #330099;">class</span><span style="color: #555555;">);</span> <span style="color: #0099ff; font-style: italic;">// mocking of static class</span>
PowerMockito<span style="color: #555555;">.</span><span style="color: #330099;">when</span><span style="color: #555555;">(</span>AreaFinder<span style="color: #555555;">.</span><span style="color: #330099;">findArea</span><span style="color: #555555;">(</span><span style="color: #cc3300;">"rectangle"</span><span style="color: #555555;">,</span> length<span style="color: #555555;">,</span> breadth<span style="color: #555555;">)).</span><span style="color: #330099;">thenReturn</span><span style="color: #555555;">(</span><span style="color: #ff6600;">200</span><span style="color: #555555;">);</span> <span style="color: #0099ff; font-style: italic;">// defining behaviour</span>
Assert<span style="color: #555555;">.</span><span style="color: #330099;">assertEquals</span><span style="color: #555555;">(</span><span style="color: #ff6600;">200</span><span style="color: #555555;">,</span> rec<span style="color: #555555;">.</span><span style="color: #330099;">getAreaOfRectangle</span><span style="color: #555555;">(</span>length<span style="color: #555555;">,</span> breadth<span style="color: #555555;">));</span>
<span style="color: #555555;">}</span>
<span style="color: #555555;">}</span>
</pre>
</div>
<br />
When you run this you will be able to see the test will be run successfully and it will get passed.<br />
<br />
If you have any issue regarding the above concept, please let us know.<br />
<br />
I hope this will help you in understand how to do mocking of static method in unit testing.<br />
<br />
Thanks for reading<br />
Noeik</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-75248862934317734132018-07-20T11:44:00.004+05:302018-07-20T11:44:54.553+05:30producer consumer problem implementation using wait notify methods.<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-FI0jbFh2lyI/W1F9mxMge2I/AAAAAAAAG24/jGklfT7FcnU4WeuAQzcGBYeL13e28dNKwCLcBGAs/s1600/FotoJet%2B%25283%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="producer-consumer problem using wait notify" border="0" data-original-height="481" data-original-width="672" height="228" src="https://2.bp.blogspot.com/-FI0jbFh2lyI/W1F9mxMge2I/AAAAAAAAG24/jGklfT7FcnU4WeuAQzcGBYeL13e28dNKwCLcBGAs/s320/FotoJet%2B%25283%2529-min.jpg" title="" width="320" /></a></div>
<br />
Most of you have heard about the Producer-Consumer problem where one is producing something and another is consuming it.<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
So basically what actually the producer-consumer problem says </div>
<div>
<br /></div>
<div>
<b>Producer-Consumer Problem</b> says that if we have a shared resource among 2 threads, and producer's job is to generate the data and put it in the buffer and consumer's job is to take the data from the buffer but then where is the problem? So problem is there should not be a scenario where the producer has produced the data more than the buffer size, which results in overflow problem also consumer should not consume all the data in the buffer and again try, which result in underflow problem. </div>
<div>
So basically its a multi-thread synchronization problem.<br />
<br />
<b>Read </b><b class="">about</b>: <b><a href="https://www.programinjava.com/2018/06/concurrency-countdownlatch-example-in.html" target="_blank">Concurrency CountDownLatch in java</a></b><br />
<br /></div>
<div>
<b>Now, what is the solution?</b></div>
<div>
We can use wait-notify mechanism to solve this problem of synchronization.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<br /></div>
<div>
<b>Approach </b></div>
<div>
<ol style="text-align: left;">
<li>We will have 2 thread one is to produce random data and second will get the random data from the shared LinkedList (or and queue).</li>
<li>we will write on a class called processor where there will be 2 methods one is produce() and second will be consume () </li>
<li>In produce() method we will write random generator and also be checking whether the size of linked list is less than 10 or not if greater we will call wait method over the object called lock which we are using for the locking mechanism.</li>
<li>when produce() put the data in linkedlist it will call lock.notify() which will notify the second thread that there are some data stored in the linked list.</li>
<li>now in consume() we will have a check for the linked list size should not be 0 if so we will call lock.wait() else we will take the data from the list and notify the producer().</li>
</ol>
<div>
Now let see the implementation of the above approach.<br />
<br />
<b><a href="https://www.programinjava.com/2018/02/java-thread-wait-notify-and-notifyall.html" target="_blank">learn more about wait notify ()</a></b></div>
</div>
<div>
<b><br /></b> <b>#Implemenation</b></div>
<div>
<b><br /></b> <b>Processor.java (</b>this class will have the produce() and consume() method in it )</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Processor</span> <span style="color: #333333;">{</span>
LinkedList<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> LinkedList<span style="color: #333333;"><>();</span>
Object lock <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Object<span style="color: #333333;">();</span>
<span style="color: #333399; font-weight: bold;">int</span> value <span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">produce</span><span style="color: #333333;">()</span> <span style="color: #008800; font-weight: bold;">throws</span> InterruptedException<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">)</span>
<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">synchronized</span> <span style="color: #333333;">(</span>lock<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>list<span style="color: #333333;">.</span><span style="color: #0000cc;">size</span><span style="color: #333333;">()</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">)</span>
lock<span style="color: #333333;">.</span><span style="color: #0000cc;">wait</span><span style="color: #333333;">();</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>value<span style="color: #333333;">++);</span>
lock<span style="color: #333333;">.</span><span style="color: #0000cc;">notify</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">consume</span><span style="color: #333333;">()</span> <span style="color: #008800; font-weight: bold;">throws</span> InterruptedException <span style="color: #333333;">{</span>
Random random <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Random<span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">){</span>
<span style="color: #008800; font-weight: bold;">synchronized</span> <span style="color: #333333;">(</span>lock<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>list<span style="color: #333333;">.</span><span style="color: #0000cc;">size</span><span style="color: #333333;">()</span> <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">)</span>
lock<span style="color: #333333;">.</span><span style="color: #0000cc;">wait</span><span style="color: #333333;">();</span>
<span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span>list<span style="color: #333333;">.</span><span style="color: #0000cc;">removeFirst</span><span style="color: #333333;">();</span>
lock<span style="color: #333333;">.</span><span style="color: #0000cc;">notify</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Got the value "</span><span style="color: #333333;">+</span>i <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"now the list size is "</span><span style="color: #333333;">+</span>list<span style="color: #333333;">.</span><span style="color: #0000cc;">size</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
Thread<span style="color: #333333;">.</span><span style="color: #0000cc;">sleep</span><span style="color: #333333;">(</span>random<span style="color: #333333;">.</span><span style="color: #0000cc;">nextInt</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">1000</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<br /></div>
<div>
Now we will see the 2 thread class and how we are calling them.<br />
<br />
<b>ProducerConsumerWithWaitNotify.Java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ProducerConsumerWithWaitNotify</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Processor pro <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Processor<span style="color: #333333;">();</span>
Thread t1 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Thread<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Runnable<span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">run</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
pro<span style="color: #333333;">.</span><span style="color: #0000cc;">produce</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated catch block</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">});</span>
Thread t2 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Thread<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Runnable<span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">run</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
pro<span style="color: #333333;">.</span><span style="color: #0000cc;">consume</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated catch block</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">});</span>
t1<span style="color: #333333;">.</span><span style="color: #0000cc;">start</span><span style="color: #333333;">();</span>
t2<span style="color: #333333;">.</span><span style="color: #0000cc;">start</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<br />
After executing the code what will be the result, It will be like below<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">Got the value 0now the list size is 9
Got the value 1now the list size is 9
Got the value 2now the list size is 9
Got the value 3now the list size is 9
Got the value 4now the list size is 9
Got the value 5now the list size is 9
Got the value 6now the list size is 9
Got the value 7now the list size is 9
Got the value 8now the list size is 9
Got the value 9now the list size is 9
Got the value 10now the list size is 9
Got the value 11now the list size is 9
Got the value 12now the list size is 9
Got the value 13now the list size is 9
Got the value 14now the list size is 9
</pre>
</div>
<br />
I hope this will help you in understanding the producer-consumer problem implementation using wait notify method in java<br />
<br />
If you have any issue or concern, please leave us a comment, will be happy to help<br />
<br />
Thanks for reading<br />
noeik</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-82027501210997344492018-07-19T16:46:00.000+05:302018-07-19T16:47:01.436+05:30Comparator interface and its example in java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-oGNhLXctZoQ/W1ByqQVPBdI/AAAAAAAAG2s/gzJpOvrkXW4fP3sxSsykZt9XEn7WcY4eACLcBGAs/s1600/FotoJet%2B%25282%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://3.bp.blogspot.com/-oGNhLXctZoQ/W1ByqQVPBdI/AAAAAAAAG2s/gzJpOvrkXW4fP3sxSsykZt9XEn7WcY4eACLcBGAs/s320/FotoJet%2B%25282%2529-min.jpg" width="320" /></a></div>
<br />
In our <a href="https://www.programinjava.com/2018/07/comparable-and-its-example-in-java.html" target="_blank">previous </a>post we have learn about what is comparable , In this article we will see basics about Comparator.<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
<b>#What is Comparator ?</b></div>
<div>
Comparator is a interface which is used to order the different user defined objects. When we have 2 object of different classes we want to order them we will use comparator.</div>
<div>
<br /></div>
<div>
Learn about <b><a href="https://www.programinjava.com/2018/07/comparable-and-its-example-in-java.html" target="_blank">What is Comparable?</a></b></div>
<div>
<br /></div>
<div>
<b>#UserCase :</b></div>
<div>
We have an array of person , we want to sort them on different basis , like on the basis of age of the person , or on the basis of name of the person. there are 2 questions comes in our mind , can we use comparable , - No ( as we can only compare on one of the 2 basis not both) , can we use comparator - Yes .</div>
<div>
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<br /></div>
<div>
<b>Approach </b></div>
<div>
<b><br /></b></div>
<div>
<ol style="text-align: left;">
<li>We will first create one person class .</li>
<li>Now we will create 2 different Custom Comparator classes which will implements Comparator<person></li>
<ol>
<li>One will be AgeComparator.</li>
<li>Second will be NameComparator.</li>
</ol>
<li>Now we will create one Main class where we will create list of person. and then we will use Collections.sort and pass the list along with the comparator , we want to use for sorting.</li>
</ol>
<div>
<b>Implementation</b></div>
</div>
<div>
<b><br /></b></div>
<div>
<b>Person.java</b></div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Person</span><span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> age<span style="color: #333333;">;</span>
String name<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">getAge</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> age<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setAge</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> age<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">age</span> <span style="color: #333333;">=</span> age<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setName</span><span style="color: #333333;">(</span>String name<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span> <span style="color: #333333;">=</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">toString</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"Person [age="</span> <span style="color: #333333;">+</span> age <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">", name="</span> <span style="color: #333333;">+</span> name <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"]"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<b>AgeComparator.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">AgeComparator</span> <span style="color: #008800; font-weight: bold;">implements</span> Comparator<span style="color: #333333;"><</span>Person<span style="color: #333333;">>{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">compare</span><span style="color: #333333;">(</span>Person arg0<span style="color: #333333;">,</span> Person arg1<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated method stub</span>
<span style="color: #008800; font-weight: bold;">return</span> arg0<span style="color: #333333;">.</span><span style="color: #0000cc;">getAge</span><span style="color: #333333;">()</span> <span style="color: #333333;">-</span> arg1<span style="color: #333333;">.</span><span style="color: #0000cc;">getAge</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<b>NameComparator.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">NameComparator</span> <span style="color: #008800; font-weight: bold;">implements</span> Comparator<span style="color: #333333;"><</span>Person<span style="color: #333333;">>{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">compare</span><span style="color: #333333;">(</span>Person o1<span style="color: #333333;">,</span> Person o2<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated method stub</span>
<span style="color: #008800; font-weight: bold;">return</span> o1<span style="color: #333333;">.</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">().</span><span style="color: #0000cc;">compareTo</span><span style="color: #333333;">(</span>o2<span style="color: #333333;">.</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<br />
<b>Main Class</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ComparatorDemo</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Person<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
Person p1 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Person<span style="color: #333333;">();</span>
p1<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">35</span><span style="color: #333333;">);</span>
p1<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"vishal"</span><span style="color: #333333;">);</span>
Person p2 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Person<span style="color: #333333;">();</span>
p2<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">45</span><span style="color: #333333;">);</span>
p2<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"rohan"</span><span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>p2<span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>p1<span style="color: #333333;">);</span>
<span style="color: #888888;">// Collections.sort(list,new AgeComparator());</span>
Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">sort</span><span style="color: #333333;">(</span>list<span style="color: #333333;">,</span><span style="color: #008800; font-weight: bold;">new</span> NameComparator<span style="color: #333333;">());</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
<b>Result </b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">Person [age=45, name=rohan]
Person [age=35, name=vishal]
</pre>
</div>
<br />
Here we can see that the Objects are sorted on the basis of name , we can also do it by using age comparator as well.<br />
<br />
If you have any issue , Please let us know , if you want any other topic to be covered in our next article please leave us a comment.<br />
<br />
If you like this article please share it with your friends as well.<br />
<br />
Thanks for reading<br />
noeik</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-19582435067233507492018-07-14T19:42:00.000+05:302018-07-14T19:42:07.116+05:30Comparable and its example in java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-mihdfiAw4rI/W0oD3MZ22vI/AAAAAAAAG2M/7W54P0L6QoMxUjOLIQwLOxwgdLlCT4TvQCLcBGAs/s1600/FotoJet%2B%252895%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="comparable interface and its example" border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-mihdfiAw4rI/W0oD3MZ22vI/AAAAAAAAG2M/7W54P0L6QoMxUjOLIQwLOxwgdLlCT4TvQCLcBGAs/s320/FotoJet%2B%252895%2529-min.jpg" title="" width="320" /></a></div>
<br />
Most of us have heard about comparable and most of you also know what is comparable,<br />
<div>
<div>
<b>What is a comparable interface?</b></div>
<div>
Comparable interface is used to sort the objects on the basis of any one variable, It is found in java.lang package and contain only one method <i><span style="color: orange;">compareTo(Object o).</span></i></div>
<div>
<br /></div>
<div>
<div>
In java When we have a scenario where we need to sort the array of objects on the basis of there member variable, or to eligible the objects to compare we use Comparable.</div>
<div>
<br /></div>
<div>
There are some <a href="https://www.programinjava.com/search/label/Interview%20Topics" target="_blank">good interview questions</a> are there related to the comparable interface.</div>
<div>
<br /></div>
<div>
In this article, we will see one example of the implementation of the comparable interface and its explanation.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<br /></div>
<div>
<b>#Objective - We have a list of student and we need to sort the list on the bases of student ages in ascending order.</b></div>
<div>
<b><br /></b></div>
<div>
<b>#Approach</b></div>
<div>
<ul style="text-align: left;">
<li>We will implement the Comparable interface in the student class.</li>
<li>we will override the compareTo() method and write the logic for that </li>
<li>As compareTo() method return </li>
<ul>
<li>1 when own variable is greater than compared object variable.</li>
<li>0 when both are equal</li>
<li>-1 when the own variable is less than compared object variable.</li>
</ul>
</ul>
<div>
Now let see the implementation.<br />
Below is the Student class</div>
</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learning</span><span style="color: #333333;">.</span><span style="color: #0000cc;">comparable</span><span style="color: #333333;">;</span>
<span style="color: #888888;">//implementing the comparable interface</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Student</span> <span style="color: #008800; font-weight: bold;">implements</span> Comparable<span style="color: #333333;"><</span>Student<span style="color: #333333;">>{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">int</span> age<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String name<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">getAge</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> age<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setAge</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> age<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">age</span> <span style="color: #333333;">=</span> age<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setName</span><span style="color: #333333;">(</span>String name<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span> <span style="color: #333333;">=</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">compareTo</span><span style="color: #333333;">(</span>Student o<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">getAge</span><span style="color: #333333;">()</span> <span style="color: #333333;">-</span> o<span style="color: #333333;">.</span><span style="color: #0000cc;">getAge</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">toString</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"Student [age="</span> <span style="color: #333333;">+</span> age <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">", name="</span> <span style="color: #333333;">+</span> name <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"]"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
Now we will see the Main Demo class<br />
<br />
<b>ComparableDemo.java</b><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learning</span><span style="color: #333333;">.</span><span style="color: #0000cc;">comparable</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Collections</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ComparableDemo</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// creating list of student with different name and age</span>
List<span style="color: #333333;"><</span>Student<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
Student s1 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Student<span style="color: #333333;">();</span>
s1<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">);</span>
s1<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"student 1"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// second</span>
Student s2 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Student<span style="color: #333333;">();</span>
s2<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">20</span><span style="color: #333333;">);</span>
s2<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"student 2"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// third</span>
Student s3 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Student<span style="color: #333333;">();</span>
s3<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">30</span><span style="color: #333333;">);</span>
s3<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"student 3"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// fourth</span>
Student s4 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Student<span style="color: #333333;">();</span>
s4<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">40</span><span style="color: #333333;">);</span>
s4<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"student 4"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// fifth</span>
Student s5 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Student<span style="color: #333333;">();</span>
s5<span style="color: #333333;">.</span><span style="color: #0000cc;">setAge</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">50</span><span style="color: #333333;">);</span>
s5<span style="color: #333333;">.</span><span style="color: #0000cc;">setName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"student 5"</span><span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>s2<span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>s1<span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>s4<span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>s3<span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>s5<span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Printing before sorting the list"</span><span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span>System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #888888;">// sorting the list </span>
Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">sort</span><span style="color: #333333;">(</span>list<span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Printing after sorting the list"</span><span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span>System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
Result Looks like below<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;">Printing before sorting the list
Student [age=20, name=student 2]
Student [age=10, name=student 1]
Student [age=40, name=student 4]
Student [age=30, name=student 3]
Student [age=50, name=student 5]
Printing after sorting the list
Student [age=10, name=student 1]
Student [age=20, name=student 2]
Student [age=30, name=student 3]
Student [age=40, name=student 4]
Student [age=50, name=student 5]
</pre>
</div>
<br />
I hope this will help you in understand how to implement comparable and when to implement it.<br />
<br />
There are some other topics if you want to explore<br />
<br />
<br />
<a href="https://www.programinjava.com/2018/03/conditional-logic-in-java.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Conditional Logic in java</a><br />
<a href="https://www.programinjava.com/2018/03/loops-in-java.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Loops in java</a><br />
<a href="https://www.programinjava.com/2018/03/arrays-in-java.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Arrays in java</a><br />
<a href="https://www.programinjava.com/2018/03/wrapper-classes-in-java.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Wrapper classes in java</a><br />
<a href="https://www.programinjava.com/2018/04/methods-in-java.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Methods in java</a><br />
<a href="https://www.programinjava.com/2018/04/what-are-object-oriented-programming.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Object Oriented Practices</a><br />
<a href="https://www.programinjava.com/2018/04/inheritance-in-java.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: #e01222; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Inheritance in java</a></div>
<div>
<br /></div>
<div>
If you have any issue, please leave us a comment, if you like it, please share it with your friends</div>
<div>
<br /></div>
<div>
Thanks for reading </div>
<div>
noeik</div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-71634582288152735322018-06-13T21:24:00.001+05:302018-06-13T21:24:22.714+05:30Concurrency CountDownLatch Example in java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-bXNXaL79qms/WyE-EEOFaSI/AAAAAAAAG00/1dlTc_VCdYwHwxaiV_zJlT4hugyTw6CpACLcBGAs/s1600/FotoJet%2B%252882%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://3.bp.blogspot.com/-bXNXaL79qms/WyE-EEOFaSI/AAAAAAAAG00/1dlTc_VCdYwHwxaiV_zJlT4hugyTw6CpACLcBGAs/s320/FotoJet%2B%252882%2529-min.jpg" width="320" /></a></div>
<br />
In this article, we will learn about the concurrency package <b><i>CountDownLatch class</i></b> which is actually used when we want to start the thread after some operation already completed. (ex-start a thread after 2 threads already finished some operation ).<br />
<a name='more'></a><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<a href="https://www.programinjava.com/search?q=multithreading" target="_blank">Also Read: Multithreading in java</a><br />
<div>
<br /></div>
<div>
I will try to explain Count Down Latch using the below problem statement.</div>
<div>
<br /></div>
<div>
<h3 style="text-align: left;">
<b>#Question</b></h3>
</div>
<div>
<b>Write a multithreaded program in which there are 10 threads and we need to start the threads and when all threads are started then only we need to stop the threads.</b></div>
<div>
<b><br /></b></div>
<div>
<b><br /></b></div>
<div>
<h3 style="text-align: left;">
<b>#Solution</b></h3>
</div>
<div>
The very first thing comes in my mind is <b>CountDownLatch</b>, and why it comes has a valid reason as we have a case where we have to wait for all the threads to start then only we will going to stop the threads.</div>
<div>
<br /></div>
<div>
<b>Why CountDownLatch here?</b></div>
<div>
When we say we need to start 10 threads what we will do is to just run a loop till 10 counter and start initialize each thread and start the thread and then what we will do again we loop the thread for 10 and we start to stop the threads but the problem is we are not sure whether all the 10 threads are already started or not. As it not compulsory that we start the thread when actually JVM start it actually.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<br /></div>
<div>
So we need something which will assure that yes all the threads are started now you can go ahead to stop all of them.</div>
<div>
<br /></div>
<div>
Now let's see the program implementation and what will be its output.</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.CountDownLatch</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ThreadArray</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
CountDownLatch latch <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> CountDownLatch<span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">);</span>
Thread<span style="color: #333333;">[]</span> t <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Thread<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">];</span>
MyThread<span style="color: #333333;">[]</span> mt <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> MyThread<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
mt<span style="color: #333333;">[</span>i<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> MyThread<span style="color: #333333;">(</span>latch<span style="color: #333333;">);</span>
t<span style="color: #333333;">[</span>i<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Thread<span style="color: #333333;">(</span>mt<span style="color: #333333;">[</span>i<span style="color: #333333;">]);</span>
t<span style="color: #333333;">[</span>i<span style="color: #333333;">].</span><span style="color: #0000cc;">start</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
latch<span style="color: #333333;">.</span><span style="color: #0000cc;">await</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>latch<span style="color: #333333;">.</span><span style="color: #0000cc;">getCount</span><span style="color: #333333;">());</span>
<span style="color: #008800; font-weight: bold;">for</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span>i<span style="color: #333333;"><</span><span style="color: #0000dd; font-weight: bold;">10</span><span style="color: #333333;">;</span>i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
mt<span style="color: #333333;">[</span>i<span style="color: #333333;">].</span><span style="color: #0000cc;">stop</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated catch block</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">MyThread</span> <span style="color: #008800; font-weight: bold;">implements</span> Runnable<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">volatile</span> <span style="color: #333399; font-weight: bold;">boolean</span> exit <span style="color: #333333;">=</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
CountDownLatch latch <span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">MyThread</span><span style="color: #333333;">(</span>CountDownLatch latch<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated constructor stub</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">latch</span> <span style="color: #333333;">=</span>latch<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">run</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Running Thread "</span><span style="color: #333333;">+</span>Thread<span style="color: #333333;">.</span><span style="color: #0000cc;">currentThread</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">());</span>
latch<span style="color: #333333;">.</span><span style="color: #0000cc;">countDown</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">while</span><span style="color: #333333;">(</span>exit<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// System.out.println("Running Thread "+Thread.currentThread().getName());</span>
<span style="color: #333333;">}</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"stoping thread "</span><span style="color: #333333;">+</span>Thread<span style="color: #333333;">.</span><span style="color: #0000cc;">currentThread</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getName</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">stop</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
exit<span style="color: #333333;">=</span><span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<br /></div>
<div>
In Above program, we are actually using Count Down Latch.<br />
<br />
We are sharing the latch in every thread and when the thread is running we are decreasing the count of the latch by one.<br />
<br />
Here we are using 2 important methods of CountDownLatch class.<br />
<br />
<blockquote class="tr_bq">
latch.countDown(); // this will decrease the count by 1<br />
latch.await() // this will wait till the count of the latch becomes 0;</blockquote>
There may be some other examples of CountDownLatch were present over the internet but I have shared this because it was asked to me in one <a href="https://www.programinjava.com/search/label/Interview%20Topics" target="_blank">interview </a>I was given for Java Developer.<br />
<br />
If you have any other example in your mind for CountDownLatch, Please share it with us as well by comment below.<br />
<br />
Thanks for reading<br />
Noeik<br />
<br />
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-55524430515193936042018-05-29T23:04:00.000+05:302018-05-29T23:07:55.782+05:30Hibernate Mapping - OneToMany Unidirectional Relationship Using JPA + Springboot<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-TucT-8SD8LU/Ww2OY9XUQ5I/AAAAAAAAGz4/f_dD3vWrEq83EOcCH2uhWIO6U-kUL7nAQCLcBGAs/s1600/FotoJet%2B%252877%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://4.bp.blogspot.com/-TucT-8SD8LU/Ww2OY9XUQ5I/AAAAAAAAGz4/f_dD3vWrEq83EOcCH2uhWIO6U-kUL7nAQCLcBGAs/s320/FotoJet%2B%252877%2529-min.jpg" width="320" /></a></div>
Hibernate OneToMany Relationship is one of the most used relationship when we do coding l, as why i am saying this is that , most of the scenario and use cases have the situation where we have one entity related with many other entity.<br />
<a name='more'></a><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InPostArticle-Ads -->
<br />
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
before we start taking about OneToMany using JPA , would like to cover the question what is OnetoMany Relationship in Hibernate.</div>
<div>
Also read : <a href="https://www.programinjava.com/2017/07/hibernate-mapping-onetoone-relationship.html" target="_blank">OneToOne Hibernate Relationship</a></div>
<div>
OneToMany Relationship : When one entity ( table) is assotiated with more than one other entity (tables) , It is called OneToMany Mapping </div>
<div>
Mapping can be </div>
<div>
Unidirectional : One Way Directional</div>
<div>
Bidirectional : Both Way Directional.</div>
<div>
<br /></div>
<div>
In this article we will see how we implement One To Many Mapping Unidirectional with JPA in Spring Boot.</div>
<div>
<br /></div>
<div>
Let start.</div>
<div>
<br /></div>
<div>
First we will discuss the Scenario then we will talk about the Solution.</div>
<div>
<br /></div>
<div>
<b>#Scenario</b></div>
<div>
We are having 2 entity as vendor and ratings , We need to establish a relationship between vendor and ratings as OneToMany Unidirectional<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<br />
<b>#Solution</b><br />
In this use case ratings itself dont have any importance if there is no vendor associated with it , that means if we are deleting the vendors , ratings should also be deleted associated to particular vendor.<br />
this is called as one to many unidirectional mapping.<br />
<br />
Now let see the Implementation.<br />
<br />
<b>Implementation (You can download the project from <a href="https://github.com/programinjava/OneToManyRelationDemo.git" target="_blank">GitHub</a>)</b><br />
We will have <b>one Rest End point </b>where we are saving the vendor along with the rating of the vendor.<br />
<br />
We are using below dependencies<br />
1) Spring Web ( For Rest APIs )<br />
2) H2 Database ( For InMemory Database)<br />
3) JPA ( As ORM )<br />
* can see the pom.xml in above github link<br />
<br />
We will first see the Models used here<br />
<b>Vendor.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">model</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.CascadeType</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.Entity</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.GeneratedValue</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.GenerationType</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.Id</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.JoinColumn</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.OneToMany</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Entity</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Vendor</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Id</span>
<span style="color: #555555; font-weight: bold;">@GeneratedValue</span><span style="color: #333333;">(</span>strategy<span style="color: #333333;">=</span>GenerationType<span style="color: #333333;">.</span><span style="color: #0000cc;">IDENTITY</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Integer id<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String name<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String companyName<span style="color: #333333;">;</span>
<span style="color: #888888;">// other attributes of vendors</span>
<span style="color: #555555; font-weight: bold;">@OneToMany</span><span style="color: #333333;">(</span>cascade<span style="color: #333333;">=</span>CascadeType<span style="color: #333333;">.</span><span style="color: #0000cc;">ALL</span><span style="color: #333333;">)</span>
<span style="color: #555555; font-weight: bold;">@JoinColumn</span><span style="color: #333333;">(</span>name<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">"vendor_id"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> List<span style="color: #333333;"><</span>Rating<span style="color: #333333;">></span> ratings <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
<span style="color: #008800; font-weight: bold;">public</span> Integer <span style="color: #0066bb; font-weight: bold;">getId</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> id<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setId</span><span style="color: #333333;">(</span>Integer id<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">id</span> <span style="color: #333333;">=</span> id<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setName</span><span style="color: #333333;">(</span>String name<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span> <span style="color: #333333;">=</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getCompanyName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> companyName<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setCompanyName</span><span style="color: #333333;">(</span>String companyName<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">companyName</span> <span style="color: #333333;">=</span> companyName<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> List<span style="color: #333333;"><</span>Rating<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">getRatings</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> ratings<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setRatings</span><span style="color: #333333;">(</span>List<span style="color: #333333;"><</span>Rating<span style="color: #333333;">></span> ratings<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">ratings</span> <span style="color: #333333;">=</span> ratings<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
Now we will see the other entity used in this use case<br />
<b>Rating.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">model</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.Entity</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.GeneratedValue</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.GenerationType</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.persistence.Id</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Entity</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Rating</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Id</span>
<span style="color: #555555; font-weight: bold;">@GeneratedValue</span><span style="color: #333333;">(</span>strategy<span style="color: #333333;">=</span>GenerationType<span style="color: #333333;">.</span><span style="color: #0000cc;">AUTO</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Integer id<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> Integer rating<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String comment<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">Rating</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated constructor stub</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> Integer <span style="color: #0066bb; font-weight: bold;">getId</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> id<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setId</span><span style="color: #333333;">(</span>Integer id<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">id</span> <span style="color: #333333;">=</span> id<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> Integer <span style="color: #0066bb; font-weight: bold;">getRating</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> rating<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setRating</span><span style="color: #333333;">(</span>Integer rating<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">rating</span> <span style="color: #333333;">=</span> rating<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getComment</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> comment<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setComment</span><span style="color: #333333;">(</span>String comment<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">comment</span> <span style="color: #333333;">=</span> comment<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<div>
If we see the Database schema generated for the above entity.<br />
<div style="text-align: center;">
Vendor Table</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-ikDHJ4olf2Q/Ww2Kdh8ir1I/AAAAAAAAGzk/VCVXb86fKqIHDHDIPrjZHpvur7JBjWXXACLcBGAs/s1600/vendor.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="224" data-original-width="399" height="111" src="https://3.bp.blogspot.com/-ikDHJ4olf2Q/Ww2Kdh8ir1I/AAAAAAAAGzk/VCVXb86fKqIHDHDIPrjZHpvur7JBjWXXACLcBGAs/s200/vendor.JPG" width="200" /></a></div>
<br />
<div style="text-align: center;">
Rating Table</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-l1GoruJ6uXA/Ww2KkagsnJI/AAAAAAAAGzo/AQpl1q3QCTUut-Iomhq69BOeuWSzyyqlgCLcBGAs/s1600/rating.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="222" data-original-width="510" height="139" src="https://3.bp.blogspot.com/-l1GoruJ6uXA/Ww2KkagsnJI/AAAAAAAAGzo/AQpl1q3QCTUut-Iomhq69BOeuWSzyyqlgCLcBGAs/s320/rating.JPG" width="320" /></a></div>
<div style="text-align: center;">
<br /></div>
Now we will see the service Impl Class.<br />
<b>VendorServiceImpl.java</b><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Optional</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.Rating</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.Vendor</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.repository.VendorRepository</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Service</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">VendorServiceImpl</span> <span style="color: #008800; font-weight: bold;">implements</span> VendorService<span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
VendorRepository vendorRepository<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">add</span><span style="color: #333333;">(</span>Vendor vendor<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">throws</span> Exception <span style="color: #333333;">{</span>
Vendor vendor1 <span style="color: #333333;">=</span>vendorRepository<span style="color: #333333;">.</span><span style="color: #0000cc;">save</span><span style="color: #333333;">(</span>vendor<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>vendor1 <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">Exception</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Vendor not saved"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> vendor1<span style="color: #333333;">.</span><span style="color: #0000cc;">getId</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">getAll</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated method stub</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">provideRating</span><span style="color: #333333;">(</span>Rating rating<span style="color: #333333;">,</span> Integer vendorId<span style="color: #333333;">)</span> <span style="color: #008800; font-weight: bold;">throws</span> Exception <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>vendorId <span style="color: #333333;">!=</span><span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span>
<span style="color: #333333;">{</span>
Optional<span style="color: #333333;"><</span>Vendor<span style="color: #333333;">></span> vendor1 <span style="color: #333333;">=</span>vendorRepository<span style="color: #333333;">.</span><span style="color: #0000cc;">findById</span><span style="color: #333333;">(</span>vendorId<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>vendor1 <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">Exception</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Vendor not found"</span><span style="color: #333333;">);</span>
Vendor ve <span style="color: #333333;">=</span>vendor1<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">();</span>
ve<span style="color: #333333;">.</span><span style="color: #0000cc;">getRatings</span><span style="color: #333333;">().</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>rating<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<b>VendorController.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">controller</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.net.URI</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">javax.servlet.http.HttpServletRequest</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.http.ResponseEntity</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.validation.BindingResult</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.GetMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.PostMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.RequestBody</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.RestController</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.util.UriTemplate</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.Vendor</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.service.VendorService</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@RestController</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">VendorController</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
VendorService vendorService<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@PostMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/vendor"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> ResponseEntity<span style="color: #333333;"><</span>Void<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">addVendor</span><span style="color: #333333;">(</span><span style="color: #555555; font-weight: bold;">@RequestBody</span> Vendor vendor<span style="color: #333333;">,</span>BindingResult errors <span style="color: #333333;">,</span> HttpServletRequest request<span style="color: #333333;">){</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>errors<span style="color: #333333;">.</span><span style="color: #0000cc;">hasErrors</span><span style="color: #333333;">())</span>
<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> ResponseEntity<span style="color: #333333;">.</span><span style="color: #0000cc;">badRequest</span><span style="color: #333333;">().</span><span style="color: #0000cc;">build</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
Integer id <span style="color: #333333;">=</span>vendorService<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>vendor<span style="color: #333333;">);</span>
URI location <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> UriTemplate<span style="color: #333333;">(</span>request<span style="color: #333333;">.</span><span style="color: #0000cc;">getRequestURI</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"/{id}"</span><span style="color: #333333;">).</span><span style="color: #0000cc;">expand</span><span style="color: #333333;">(</span>id<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> ResponseEntity<span style="color: #333333;">.</span><span style="color: #0000cc;">created</span><span style="color: #333333;">(</span>location<span style="color: #333333;">).</span><span style="color: #0000cc;">build</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>Exception e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// TODO Auto-generated catch block</span>
<span style="color: #008800; font-weight: bold;">return</span> ResponseEntity<span style="color: #333333;">.</span><span style="color: #0000cc;">badRequest</span><span style="color: #333333;">().</span><span style="color: #0000cc;">build</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@GetMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/ping"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">ping</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"Pong"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
Now when we see the Rest Endpoint Exposed is <a href="http://localhost/vendor" target="_blank">http://localhost/vendor </a>( which is post type )<br />
We will use <b>POSTMAN </b>to hit the rest endpoint with below RequestBody<br />
<span style="font-family: "courier new" , "courier" , monospace;">{</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "name": "vendor1",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "companyName": "programinjava",</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "ratings": [</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "rating": 4,</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> "comment": "the vendor is ok ok "</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ]</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">}</span><br />
<br />
<b>Important Things :</b><br />
<ul style="text-align: left;">
<li>The Rating table don't have any vendor_id column defination ( if you have checked ).</li>
<li>If Vendor is deleted , the ratings of the vendor will also be deleted from the table.</li>
<li>We can fetch the rating wrt vendor but we can't fetch the vendor wrt rating as it is Unidirectional Mapping.</li>
</ul>
<div>
I hope this article will help you in understanding OneToMany relationship in hibernate using spring boot and JPA.<br />
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
<br /></div>
<div>
Thanks for reading</div>
<div>
<a href="http://www.noeik.com/" target="_blank">noeik</a></div>
<br />
<br /></div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-54468251976140513712018-05-22T23:28:00.004+05:302018-05-23T10:41:39.813+05:30Why Python & its popular libraries<div dir="ltr" style="text-align: left;" trbidi="on">
This is second article in the series of python programming language by <a href="https://www.linkedin.com/in/zachary-farley-a6939798" target="_blank">Zachary Farley </a>, you can check the first article from <a href="https://www.programinjava.com/2018/04/the-making-of-python-app.html" target="_blank">here</a>.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-XTAOjt1mkEk/WwRYiTSmOSI/AAAAAAAAGzI/HxHcBM2CsBAzCG6M8qeXZu8sGFeTfZ17gCLcBGAs/s1600/FotoJet%2B%252865%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://2.bp.blogspot.com/-XTAOjt1mkEk/WwRYiTSmOSI/AAAAAAAAGzI/HxHcBM2CsBAzCG6M8qeXZu8sGFeTfZ17gCLcBGAs/s320/FotoJet%2B%252865%2529-min.jpg" width="320" /></a></div>
<div>
<br /></div>
<div>
<br />
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<div>
<b>History of python?</b></div>
<div>
In 1999 Van Rossum submitted a funding proposal to <a href="https://www.darpa.mil/" target="_blank">DARPA</a> titled <b><i>“Computer Programming for Everybody”</i></b> where he defined the clear goals for Python: </div>
<div>
</div>
<div>
1)<span style="white-space: pre;"> </span> An easy intuitive language</div>
<div>
2)<span style="white-space: pre;"> </span>Code understandable as plain English</div>
<div>
3)<span style="white-space: pre;"> </span>Suitability for everyday tasks, allowing short development times</div>
<div>
<br />
<a name='more'></a><br /></div>
<div>
Now Python has become one of the most popular programming languages to learn, next to Java and JavaScript.</div>
</div>
<div>
<div>
A little more background into Python: it was first released in 1991 by a Dutch programmer named <a href="https://en.wikipedia.org/wiki/Guido_van_Rossum#Python" target="_blank">Guido van Rossum</a>. Being known as the BDFL (Benevolent Dictator For Life) he continues to oversee language development. A fun fact about Rossum is that he worked at Google from 2005 to 2012 and spent half of his time developing Python.</div>
<div>
<b>What is Python? </b></div>
<div>
Some of the two most common types of languages are compiled and interpreted languages. A few examples of compiled languages include C, C++ and COBOL. A couple interpreted languages are Ruby and JavaScript. Python belongs to the latter as an interpreted programming language. In layman’s terms, when executing a Python script, each line must be read in by the PC and translated before executing the script. There are different advantages and disadvantages to a compiled language versus an interpreted one. </div>
<div>
<span style="white-space: pre;"> </span></div>
<div>
For a <a href="https://www.ibm.com/support/knowledgecenter/zosbasics/com.ibm.zos.zappldev/zappldev_85.html" target="_blank">compiled programming language</a>, when the source code is ran through a compiler you obtain a very efficient program that can be executed a number of times rapidly. Compared to <a href="https://www.ibm.com/support/knowledgecenter/zosbasics/com.ibm.zos.zappldev/zappldev_85.htm" target="_blank">interpreted languages</a> these need to be parsed, interpreted and then executed – making them less efficient than a compiled language. However, there are advantages to interpreted languages as well. Since they are less labor intensive, it’s easy to build, test and change when needed. It’s often good to use for prototyping applications.</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-46BbFr_7OLA/WwRTbT0yp8I/AAAAAAAAGyM/RtQ_gnZRO8QMLyN-U7YErWAaOBxjiXXNwCLcBGAs/s1600/fig1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="360" data-original-width="656" height="175" src="https://4.bp.blogspot.com/-46BbFr_7OLA/WwRTbT0yp8I/AAAAAAAAGyM/RtQ_gnZRO8QMLyN-U7YErWAaOBxjiXXNwCLcBGAs/s320/fig1.png" width="320" /></a></div>
<div style="text-align: center;">
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span lang="EN" style="font-size: 10.0pt;">Figure 1: Pros and Con list ( source: </span><span lang="EN"><a href="https://learntocodewith.me/programming/source-code/"><span style="color: #1155cc; font-size: 10.0pt;">https://learntocodewith.me/programming/source-code/</span></a></span><span lang="EN" style="font-size: 10.0pt;"> )<o:p></o:p></span></div>
</div>
<div>
<br /></div>
<div>
The type of project you’re working on will guide you to which language you need to learn. One unique advantage python holds over other languages is the highly readable syntax that’s very close to the English language. See below a Python and Java function to check if a number is even or odd</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-KMEub1qr4QE/WwRT1_6Q9_I/AAAAAAAAGyU/szy0iPyHb342ZFP3HBYRPmNQoCQGg3DEACLcBGAs/s1600/fig2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="153" data-original-width="256" height="190" src="https://4.bp.blogspot.com/-KMEub1qr4QE/WwRT1_6Q9_I/AAAAAAAAGyU/szy0iPyHb342ZFP3HBYRPmNQoCQGg3DEACLcBGAs/s320/fig2.png" width="320" /></a></div>
<div style="text-align: center;">
<span lang="EN" style="font-family: "arial" , "sans-serif"; font-size: 10.0pt; line-height: 115%;">Figure 2: function
created in python to tell even and odd numbers</span></div>
<div>
<pre style="color: #333333; line-height: 16.25px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span>(String[] args) {
System.<span style="color: #0000cc;">out</span>.<span style="color: #0000cc;">println</span>(<span style="background-color: #fff0f0;">"Enter a number to check if it is even or odd"</span>); System.<span style="color: #0000cc;">out</span>.<span style="color: #0000cc;">println</span>(<span style="background-color: #fff0f0;">"Your number is "</span> + (((<span style="color: #008800; font-weight: bold;">new</span> Scanner(System.<span style="color: #0000cc;">in</span>).<span style="color: #0000cc;">nextInt</span>() & <span style="color: #0000dd; font-weight: bold;">1</span>) == <span style="color: #0000dd; font-weight: bold;">0</span>) ? <span style="background-color: #fff0f0;">"even"</span> : <span style="background-color: #fff0f0;">"odd"</span>));
} (<span style="color: #0000dd; font-weight: bold;">3</span>)</pre>
<pre style="color: #333333; line-height: 16.25px;">Figure 3: Function created in Java to tell even and odd numbers</pre>
</div>
<div>
As you can see, when compared to a standard language such as Java, the Python script is easier to understand to the untrained eye. Another key point to make is that Python doesn’t require brackets as it uses the <a href="https://en.wikipedia.org/wiki/Off-side_rule" target="_blank">off-side rule</a>, which is expressed using indentations.</div>
<div>
<div>
<br /></div>
<div>
<b>What are the uses?</b></div>
<div>
Here are some companies and what they use Python for:</div>
<div>
<br /></div>
<div>
<ul style="text-align: left;">
<li><a href="http://www.dummies.com/programming/python/10-major-uses-of-python/" target="_blank">D-Link</a> uses Python to create a multi threaded application that allows one machine to service multiple devices when upgrading firmware over a network connection. In this case Python provides an easy-to-use serial communication code when completing the task.</li>
<li><a href="http://www.dummies.com/programming/python/10-major-uses-of-python/" target="_blank">ForecastWatch</a> uses Python for data analysis by collecting thousands of weather forecasters and comparing against actual climatological data to determine the accuracy of their weather predictions. They also use pythons enhanced multi threading abilities to collect forecasts from around 5,000 sources daily.</li>
<li><a href="http://www.dummies.com/programming/python/10-major-uses-of-python/" target="_blank">Industrial Light and Magic</a> uses Python in the production process of scripting complex, computer graphic-intensive films. This was done by integrating Python within larger software systems (even if written in C or C++), allowing Python to interact with the systems in situations where other languages can’t.</li>
<li><a href="http://www.dummies.com/programming/python/10-major-uses-of-python/" target="_blank">United Space Alliance</a> uses Python’s dynamic typing and pseudo code syntax to unit test each piece of an Oracle database within a workflow automation system. The simplicity of Python allows this company to develop the tool faster.</li>
<li><a href="https://medium.com/@mindfiresolutions.usa/advantages-and-disadvantages-of-python-programming-language-fd0b394f2121" target="_blank">Advantages of Python</a> include the extensive support libraries for areas like string operations; web service tools; OS interfaces; and protocols. It also includes a powerful integration feature making it easy to develop web services and processing XML with the simple readability.</li>
</ul>
</div>
<div>
Of course, no language is without fault. In Python’s case it has not solved many enterprise solutions, users of Python find it difficult to using other languages. They become so accustomed to its features and library collection when switching to languages that need variable type declaration, or even adding curly braces for loops the task proves difficulty. </div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InArticle-Big-Horizontal-Ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-slot="5735601746" style="display: inline-block; height: 250px; width: 970px;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<div>
Another common limitation with Python is that it’s <a href="https://medium.com/@mindfiresolutions.usa/advantages-and-disadvantages-of-python-programming-language-fd0b394f2121" target="_blank">weak for mobile computing</a> and is only known for desktop and server platforms. Python also has underdeveloped database access layers. For example, common enterprise solutions use Java database connectivity (JDBC) or open database connectivity (ODBC). Lastly, as mentioned above, being an interpreted language, Python eventually slows down during compilation. However, it’s seen as fast for other web applications.</div>
<div>
<b><br /></b></div>
<div>
<b>What are popular libraries?</b></div>
<div>
<b><u>Django</u></b></div>
<div>
<a href="https://www.djangoproject.com/start/overview/" target="_blank">Django</a> is a Python framework for web development. It allows developers to take web application to launch in hours. There are also numerous extra support such as built-in RSS feeds, site maps, user authentication, and even content administration. Django websites are also highly secure and protect against common security mistakes such as SQL injections.</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-oXEEAVoE-EI/WwRV0xN8vYI/AAAAAAAAGyg/t73e_uQlIBgrFOrGQ_wrcmbwy7ZsoKSIgCLcBGAs/s1600/fig3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="426" data-original-width="431" height="395" src="https://3.bp.blogspot.com/-oXEEAVoE-EI/WwRV0xN8vYI/AAAAAAAAGyg/t73e_uQlIBgrFOrGQ_wrcmbwy7ZsoKSIgCLcBGAs/s400/fig3.png" width="400" /></a></div>
<div style="text-align: center;">
<div class="MsoNormal" style="line-height: normal;">
<span lang="EN" style="font-size: 10.0pt;">Figure 4: Youtube website that’s run and powered by Django (source: </span><span lang="EN"><a href="https://www.shuup.com/blog/25-of-the-most-popular-python-and-django-websites/"><span style="color: #1155cc; font-size: 10.0pt;">https://www.shuup.com/blog/25-of-the-most-popular-python-and-django-websites/</span></a></span><span lang="EN" style="font-size: 10.0pt;"> )<o:p></o:p></span></div>
</div>
<div>
<br /></div>
<div>
<b>Pandas and MatPlotlib</b></div>
<div>
<div>
Pandas is an open source Python library that provides <a href="http://www.data-analysis-in-python.org/why_python.html" target="_blank">high-performance</a> , easy-to-use data structures for analyzing data. With built-in support for numerous file formats, it creates data frame objects to perform easy data manipulation. Working in tandem with Pandas, the library Numpy adds fundamental data types such as vectors and matrices for increased support into heavy data analysis. The library MatPlotlib provides the visualization for the analysis to see results outside of a table format.</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-YB9jt2Ua-iM/WwRWM50kcNI/AAAAAAAAGyo/YV7IuzZnIhEz3eyHUS9Pq8JJ3F9Xt_YWQCLcBGAs/s1600/fig4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="301" data-original-width="379" height="317" src="https://1.bp.blogspot.com/-YB9jt2Ua-iM/WwRWM50kcNI/AAAAAAAAGyo/YV7IuzZnIhEz3eyHUS9Pq8JJ3F9Xt_YWQCLcBGAs/s400/fig4.png" width="400" /></a></div>
<div style="text-align: center;">
<div class="MsoNormal" style="line-height: normal;">
<span lang="EN" style="font-size: 10.0pt;">Figure 5: matplotlib contour plot example (source: </span><span lang="EN"><a href="https://matplotlib.org/tutorials/introductory/sample_plots.html#sphx-glr-tutorials-introductory-sample-plots-py"><span style="color: #1155cc; font-size: 10.0pt;">https://matplotlib.org/tutorials/introductory/sample_plots.html#sphx-glr-tutorials-introductory-sample-plots-py</span></a></span><span lang="EN" style="font-size: 10.0pt;"> )<o:p></o:p></span></div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-Jk00lsvb3bk/WwRWYg8RSHI/AAAAAAAAGys/4kWlXqgSqL0HWAL6mZc2C_-MTodpaheggCLcBGAs/s1600/qwe.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="293" data-original-width="296" src="https://4.bp.blogspot.com/-Jk00lsvb3bk/WwRWYg8RSHI/AAAAAAAAGys/4kWlXqgSqL0HWAL6mZc2C_-MTodpaheggCLcBGAs/s1600/qwe.png" /></a></div>
<div style="text-align: center;">
<span lang="EN" style="font-family: "arial" , "sans-serif"; font-size: 10.0pt; line-height: 115%;">Figure 6: Matplotlib CT
scan plot example (source: </span><span lang="EN" style="font-family: "arial" , "sans-serif"; font-size: 11.0pt; line-height: 115%;"><a href="https://matplotlib.org/tutorials/introductory/sample_plots.html#sphx-glr-tutorials-introductory-sample-plots-py"><span style="color: #1155cc; font-size: 10.0pt; line-height: 115%;">https://matplotlib.org/tutorials/introductory/sample_plots.html#sphx-glr-tutorials-introductory-sample-plots-py</span></a></span><span lang="EN" style="font-family: "arial" , "sans-serif"; font-size: 10.0pt; line-height: 115%;"> )</span></div>
<div>
<br /></div>
<div>
<div>
<b>Tensorflow</b></div>
<div>
Another good library that Python can use is Tensorflow. Tensorflow is an open source library meant for machine learning. Created by Google, Tensorflow offers an easy overlay with Python to implement its many computational features. Machine learning has rapidly increased in presence recently, with well-known applications including <a href="https://medium.com/app-affairs/9-applications-of-machine-learning-from-day-to-day-life-112a47a429d0" target="_blank">Google Maps’ traffic predictions</a>. By performing a congestion analysis Google Maps will offer an alternate route in hopes of a shorter ETA. </div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br /></div>
<div>
Facebook's own feature of offering <a href="https://medium.com/app-affairs/9-applications-of-machine-learning-from-day-to-day-life-112a47a429d0" target="_blank">friend request recommendations</a> is another result of machine learning. By comparing your profile information with other users, Facebook makes a “guess” at who you most likely know and would want to connect with. For example, if you and another coworker list your job under workplace, Facebook will most likely suggest them to add as a new friend. </div>
<div>
<br /></div>
<div>
Lastly are <a href="https://medium.com/app-affairs/9-applications-of-machine-learning-from-day-to-day-life-112a47a429d0" target="_blank">product recommendations</a>. Ever wonder why you get emails offering coupons of websites you just visited? Well, now you know it’s all thanks to machine learning. If you stay on a web page for a certain amount of time or even look for a certain product on multiple web pages, it’s all factored in when you see a new email with a coupon on the new video game you were looking to buy.</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-PniZQDuhQvE/WwRW9MwYtPI/AAAAAAAAGy8/fOizSoDnTMgpqVtyQKkNtP3idQIiiG72QCLcBGAs/s1600/qw.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="174" data-original-width="427" height="162" src="https://1.bp.blogspot.com/-PniZQDuhQvE/WwRW9MwYtPI/AAAAAAAAGy8/fOizSoDnTMgpqVtyQKkNtP3idQIiiG72QCLcBGAs/s400/qw.png" width="400" /></a></div>
<div style="text-align: center;">
<div class="MsoNormal" style="line-height: normal;">
<span lang="EN" style="font-size: 10.0pt;">Figure 7: Tensorflow image processing example (source: </span><span lang="EN"><a href="https://www.tensorflow.org/tutorials/image_recognition"><span style="color: #1155cc; font-size: 10.0pt;">https://www.tensorflow.org/tutorials/image_recognition</span></a></span><span lang="EN" style="font-size: 10.0pt;"> )<a href="https://www.blogger.com/null" name="_h70i6j88rfso"></a><o:p></o:p></span></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
I hope this will definitely help you , and if you like it please share it with your friends and colleagues as well.</div>
<div>
<br /></div>
<div>
Thanks for reading</div>
<div>
<a href="http://www.noeik.com/" target="_blank">Noeik</a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-49346234529424779742018-05-21T00:34:00.002+05:302018-05-22T19:55:00.386+05:30Implementing Redis with Spring boot<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-4SycXZprU3g/WwHGX1tWqtI/AAAAAAAAGx8/NKNtslZvEdUHqwJldgC89ixfn99SK5igACLcBGAs/s1600/FotoJet%2B%252861%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-4SycXZprU3g/WwHGX1tWqtI/AAAAAAAAGx8/NKNtslZvEdUHqwJldgC89ixfn99SK5igACLcBGAs/s320/FotoJet%2B%252861%2529-min.jpg" width="320" /></a></div>
<br />
<b>Redis</b> is one of the most used inmemory cache server used in most of the distributed system for saving the key value data structures in memory for defined time interval along with some other features like pub sub and more.<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- fresherjobfinder-response-txt-ad -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="6852425377" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
So In this article you will learn how to use redis server and store the data in it while working on spring boot.</div>
<div>
<br />
<a name='more'></a><br /></div>
<div>
So lets start (You can Download the project from <span style="background-color: yellow;"><a href="https://github.com/programinjava/RedisWithSpringBoot.git" target="_blank">github</a></span></div>
<div>
First we will create simple <a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" target="_blank">spring boot project </a></div>
<div>
<br /></div>
<div>
<b>Adding Dependency</b></div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color: #007700;"><project</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0"</span>
<span style="color: #0000cc;">xmlns:xsi=</span><span style="background-color: #fff0f0;">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color: #0000cc;">xsi:schemaLocation=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><modelVersion></span>4.0.0<span style="color: #007700;"></modelVersion></span>
<span style="color: #007700;"><groupId></span>com.programinjava.learn<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>RedisWithSpringBoot<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>0.0.1-SNAPSHOT<span style="color: #007700;"></version></span>
<span style="color: #007700;"><packaging></span>jar<span style="color: #007700;"></packaging></span>
<span style="color: #007700;"><name></span>RedisWithSpringBoot<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>Demo project for Spring Boot<span style="color: #007700;"></description></span>
<span style="color: #007700;"><parent></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-parent<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.0.2.RELEASE<span style="color: #007700;"></version></span>
<span style="color: #007700;"><relativePath</span> <span style="color: #007700;">/></span> <span style="color: #888888;"><!-- lookup parent from repository --></span>
<span style="color: #007700;"></parent></span>
<span style="color: #007700;"><properties></span>
<span style="color: #007700;"><project.build.sourceEncoding></span>UTF-8<span style="color: #007700;"></project.build.sourceEncoding></span>
<span style="color: #007700;"><project.reporting.outputEncoding></span>UTF-8<span style="color: #007700;"></project.reporting.outputEncoding></span>
<span style="color: #007700;"><java.version></span>1.8<span style="color: #007700;"></java.version></span>
<span style="color: #007700;"></properties></span>
<span style="color: #007700;"><dependencies></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-data-redis<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-thymeleaf<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-web<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>redis.clients<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>jedis<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><type></span>jar<span style="color: #007700;"></type></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-test<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"></dependencies></span>
<span style="color: #007700;"><build></span>
<span style="color: #007700;"><plugins></span>
<span style="color: #007700;"><plugin></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-maven-plugin<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></plugin></span>
<span style="color: #007700;"></plugins></span>
<span style="color: #007700;"></build></span>
<span style="color: #007700;"></project></span>
</pre>
</div>
</div>
<div>
</div>
In pom.xml you will see there are 2 dependency for redis ,<br />
<br />
<pre style="line-height: 16.25px;"><span style="color: #007700;"> <dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-data-redis<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span></pre>
<pre style="line-height: 16.25px;"><span style="color: #007700;"> <dependency></span>
<span style="color: #007700;"><groupId></span>redis.clients<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>jedis<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><type></span>jar<span style="color: #007700;"></type></span>
<span style="color: #007700;"></dependency></span></pre>
After that you need to define configurations<br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br />
<br />
<b>RedisConfigurations.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">config</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.context.annotation.Bean</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.context.annotation.Configuration</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.data.redis.connection.jedis.JedisConnectionFactory</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.data.redis.core.RedisTemplate</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.User</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Configuration</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">RedisConfigurations</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Bean</span>
<span style="color: #008800; font-weight: bold;">public</span> JedisConnectionFactory <span style="color: #0066bb; font-weight: bold;">jedisConnectionFactory</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
JedisConnectionFactory jedisConFactory
<span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> JedisConnectionFactory<span style="color: #333333;">();</span>
jedisConFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">setHostName</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"localhost"</span><span style="color: #333333;">);</span>
jedisConFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">setPort</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">6379</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> jedisConFactory<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@Bean</span>
<span style="color: #008800; font-weight: bold;">public</span> RedisTemplate<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> User<span style="color: #333333;">></span> redisTemplate<span style="color: #333333;">(){</span>
RedisTemplate<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span>User<span style="color: #333333;">></span> redisTempate <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> RedisTemplate<span style="color: #333333;"><>();</span>
redisTempate<span style="color: #333333;">.</span><span style="color: #0000cc;">setConnectionFactory</span><span style="color: #333333;">(</span>jedisConnectionFactory<span style="color: #333333;">());</span>
<span style="color: #008800; font-weight: bold;">return</span> redisTempate<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
In above Configuration we have defined 2 beans<br />
<b>JedisConfigurationFactory </b>: it will show where the redis server is running and where it need to connect with the server.<br />
<br />
<b>RedisTemplate: </b>is to do execution and do crud operation for redis .<br />
<br />
<b>Adding Repository</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #555555; font-weight: bold;">@Repository</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">interface</span> <span style="color: #bb0066; font-weight: bold;">UserRespository</span> <span style="color: #008800; font-weight: bold;">extends</span> CrudRepository<span style="color: #333333;"><</span>User<span style="color: #333333;">,</span> Integer<span style="color: #333333;">></span> <span style="color: #333333;">{</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
<b>User Model Class </b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">model</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.io.Serializable</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.data.redis.core.RedisHash</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@RedisHash</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"User"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">User</span> <span style="color: #008800; font-weight: bold;">implements</span> Serializable<span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> Integer id<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String name<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> Integer age<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> Integer <span style="color: #0066bb; font-weight: bold;">getId</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> id<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setId</span><span style="color: #333333;">(</span>Integer id<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">id</span> <span style="color: #333333;">=</span> id<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getName</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setName</span><span style="color: #333333;">(</span>String name<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">name</span> <span style="color: #333333;">=</span> name<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> Integer <span style="color: #0066bb; font-weight: bold;">getAge</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> age<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setAge</span><span style="color: #333333;">(</span>Integer age<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">age</span> <span style="color: #333333;">=</span> age<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
Now we will add Service class<br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br />
<br />
<b>UserService.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.User</span><span style="color: #333333;">;</span>
<span style="color: #888888;">//import com.programinjava.learn.repository.UserRespositoryImpl;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.repository.UserRespository</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Service</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">UserService</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
UserRespository userRespository<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">saveUser</span><span style="color: #333333;">(</span>User user<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
userRespository<span style="color: #333333;">.</span><span style="color: #0000cc;">save</span><span style="color: #333333;">(</span>user<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
After this we will see the Controller file<br />
<b>UserController.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">controller</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Controller</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.ui.ModelMap</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.GetMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.ModelAttribute</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.PostMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.RequestMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.User</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.service.UserService</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Controller</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">UserController</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
UserService userService<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@PostMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/userprocess"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">saveUser</span><span style="color: #333333;">(</span><span style="color: #555555; font-weight: bold;">@ModelAttribute</span> User user <span style="color: #333333;">,</span>ModelMap map<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
userService<span style="color: #333333;">.</span><span style="color: #0000cc;">saveUser</span><span style="color: #333333;">(</span>user<span style="color: #333333;">);</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">addAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"message"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Successfully Added User"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"index"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@GetMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/user"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">getIndex</span><span style="color: #333333;">(</span>ModelMap map<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">addAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"user"</span><span style="color: #333333;">,</span><span style="color: #008800; font-weight: bold;">new</span> User<span style="color: #333333;">());</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"index"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
Till now we have added all the files but to create user and store it in redis server , we have created one index.html<br />
in this we are using thymeleaf template enginer.<br />
let see <b>index.html</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><!DOCTYPE html></span>
<span style="color: #007700;"><html</span> <span style="color: #0000cc;">xmlns:th=</span><span style="background-color: #fff0f0;">"http://www.thymeleaf.org"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><head></span>
<span style="color: #007700;"><meta</span> <span style="color: #0000cc;">charset=</span><span style="background-color: #fff0f0;">"ISO-8859-1"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><title></span>Index<span style="color: #007700;"></title></span>
<span style="color: #007700;"></head></span>
<span style="color: #007700;"><body></span>
<span style="color: #007700;"><form</span> <span style="color: #0000cc;">action=</span><span style="background-color: #fff0f0;">"/userprocess"</span> <span style="color: #0000cc;">th:object=</span><span style="background-color: #fff0f0;">"${user}"</span> <span style="color: #0000cc;">method=</span><span style="background-color: #fff0f0;">"post"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"text"</span> <span style="color: #0000cc;">th:field=</span><span style="background-color: #fff0f0;">"*{name}"</span> <span style="color: #0000cc;">placeholder=</span><span style="background-color: #fff0f0;">"Enter your name"</span><span style="color: #007700;">></input></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"number"</span> <span style="color: #0000cc;">th:field=</span><span style="background-color: #fff0f0;">"*{age}"</span> <span style="color: #0000cc;">placeholder=</span><span style="background-color: #fff0f0;">"Enter your age"</span><span style="color: #007700;">></input></span>
<span style="color: #007700;"><br/></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"submit"</span><span style="color: #007700;">/></span>
<span style="color: #007700;"></form></span>
<span style="color: #007700;"><span</span> <span style="color: #0000cc;">th:if=</span><span style="background-color: #fff0f0;">${message}</span><span style="color: #007700;">></span>
<span style="color: #007700;"><h2</span> <span style="color: #0000cc;">th:text=</span><span style="background-color: #fff0f0;">"${message}"</span><span style="color: #007700;">></h2></span>
<span style="color: #007700;"></span></span>
<span style="color: #007700;"></body></span>
<span style="color: #007700;"></html></span>
</pre>
</div>
<br />
Thats it<br />
After deploy it and start redis server we will hit the URL<br />
<b>http://localhost:8080/userprocess</b><br />
<b><br /></b>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-g6evG8Gn338/WwHDz2IWUuI/AAAAAAAAGxo/WJhXxFOBqTcAocF7Y0-kYeKcyBBUDzvXQCLcBGAs/s1600/view.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="239" data-original-width="666" height="142" src="https://4.bp.blogspot.com/-g6evG8Gn338/WwHDz2IWUuI/AAAAAAAAGxo/WJhXxFOBqTcAocF7Y0-kYeKcyBBUDzvXQCLcBGAs/s400/view.JPG" width="400" /></a></div>
After that when we see the redis console , we will be able to see it like below image<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-EQR16ORrH9Y/WwHD-0NIZUI/AAAAAAAAGxs/9J8GjmIwwrkkbxXEp6L9h3M423IomRisACLcBGAs/s1600/redis.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="536" data-original-width="1600" height="214" src="https://2.bp.blogspot.com/-EQR16ORrH9Y/WwHD-0NIZUI/AAAAAAAAGxs/9J8GjmIwwrkkbxXEp6L9h3M423IomRisACLcBGAs/s640/redis.JPG" width="640" /></a></div>
<b><br /></b>
Hope this will help you in implementing <b>redis </b>with <b>spring boot</b><br />
<br />
<span style="background-color: white; color: #444444; font-family: "georgia" , "times new roman" , "times" , serif; font-size: 16px;">Read Also</span><br />
<ol style="background: rgb(255, 255, 255); border: 0px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-simple-application-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; outline: none; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Spring Boot and Hibernate Tutorials - Application using Spring Boot</a></li>
<li style="background: transparent; border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">What is Spring boot ?</a></li>
<li style="background: transparent; border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/04/how-to-read-value-from-property-file-in_16.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">How to read value from property file in spring boot ?</a></li>
<li style="background: transparent; border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">First Rest API Application - Top Spring Framework Interview Questions</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;"><a href="https://www.programinjava.com/2018/05/file-uploading-in-spring-mvc-using.html" target="_blank">File Uploading in Spring MVC using thymeleaf and spring boot</a></span></li>
</ol>
<div style="background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="background: transparent; border: 0px; font-family: "georgia" , "times new roman" , "times" , serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Thanks for reading</span></div>
<div style="background: rgb(255, 255, 255); border: 0px; box-sizing: border-box; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="background: transparent; border: 0px; font-family: "georgia" , "times new roman" , "times" , serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="http://www.noeik.com/" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Noeik</a></span></div>
<br /></div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-50725226641595511192018-05-17T16:20:00.003+05:302018-05-17T16:20:51.914+05:30File Uploading in Spring MVC using thymeleaf and spring boot<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-8LDHqb5KktQ/Wv1d-2BlQMI/AAAAAAAAGww/Br7lHyJTHqYrmw_e5ZYLEAwG2zTjqll2QCLcBGAs/s1600/FotoJet%2B%252856%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-8LDHqb5KktQ/Wv1d-2BlQMI/AAAAAAAAGww/Br7lHyJTHqYrmw_e5ZYLEAwG2zTjqll2QCLcBGAs/s320/FotoJet%2B%252856%2529-min.jpg" width="320" /></a></div>
<br />
File uploading is one of the very basic requirement now a day where user required to upload a file on server.<br />
<div>
There are so many ways to upload a file but in this tutorial we will talk about the file upload in spring mvc using thymeleaf.<br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<br /></div>
<div>
So lets start.</div>
<div>
<br /></div>
<div>
<b>Pseudo code: (Only CSV and Zip are allowed to upload)</b></div>
<div>
In this tutorial we will first create on web page </div>
<div>
this page will have a form whose action will call a spring controller</div>
<div>
under this spring controller we will have the file upload logic.</div>
<div>
we also define the server directory where we want to upload the file.</div>
<div>
After file successfully uploaded , we will show the message on webpage.</div>
<div>
<br /></div>
<div>
<b>Implementation code: ( You can download the source code from <a href="https://github.com/programinjava/FileUploadExample.git" target="_blank">Github</a> )</b></div>
<div>
<br /></div>
<div>
First we will see the project Structure </div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-QQ1alKHw0Qo/Wv1ZuBN08TI/AAAAAAAAGwk/tW4s7_VF_9Io7RVesfRbvQQCgPljkaozgCLcBGAs/s1600/Capture.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="591" data-original-width="554" height="400" src="https://2.bp.blogspot.com/-QQ1alKHw0Qo/Wv1ZuBN08TI/AAAAAAAAGwk/tW4s7_VF_9Io7RVesfRbvQQCgPljkaozgCLcBGAs/s400/Capture.JPG" width="373" /></a></div>
<div>
<br /></div>
<div>
<b><br /></b></div>
<div>
Lets first see the <b>pom.xml</b> to see all the dependency</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color: #007700;"><project</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0"</span> <span style="color: #0000cc;">xmlns:xsi=</span><span style="background-color: #fff0f0;">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color: #0000cc;">xsi:schemaLocation=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><modelVersion></span>4.0.0<span style="color: #007700;"></modelVersion></span>
<span style="color: #007700;"><groupId></span>com.programinjava.learn<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>FileUploadExample<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>0.0.1-SNAPSHOT<span style="color: #007700;"></version></span>
<span style="color: #007700;"><packaging></span>jar<span style="color: #007700;"></packaging></span>
<span style="color: #007700;"><name></span>FileUploadExample<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>Demo project for Spring Boot<span style="color: #007700;"></description></span>
<span style="color: #007700;"><parent></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-parent<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.0.2.RELEASE<span style="color: #007700;"></version></span>
<span style="color: #007700;"><relativePath/></span> <span style="color: #888888;"><!-- lookup parent from repository --></span>
<span style="color: #007700;"></parent></span>
<span style="color: #007700;"><properties></span>
<span style="color: #007700;"><project.build.sourceEncoding></span>UTF-8<span style="color: #007700;"></project.build.sourceEncoding></span>
<span style="color: #007700;"><project.reporting.outputEncoding></span>UTF-8<span style="color: #007700;"></project.reporting.outputEncoding></span>
<span style="color: #007700;"><java.version></span>1.8<span style="color: #007700;"></java.version></span>
<span style="color: #007700;"></properties></span>
<span style="color: #007700;"><dependencies></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-thymeleaf<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-web<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-test<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.webjars<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>jquery<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>3.3.1<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.webjars<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>bootstrap<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>3.3.7<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"></dependencies></span>
<span style="color: #007700;"><build></span>
<span style="color: #007700;"><plugins></span>
<span style="color: #007700;"><plugin></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-maven-plugin<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></plugin></span>
<span style="color: #007700;"></plugins></span>
<span style="color: #007700;"></build></span>
<span style="color: #007700;"></project></span>
</pre>
</div>
</div>
<div>
<b><br /></b></div>
<div>
</div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
Here we have used below dependencies<br />
<br />
<ol style="text-align: left;">
<li>Thymeleaf</li>
<li>Bootstrap</li>
<li>Spring web</li>
</ol>
<div>
After this we will see the <b>Controller </b>class</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">controller</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.regex.Pattern</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Controller</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.ui.ModelMap</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.GetMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.PostMapping</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.bind.annotation.RequestParam</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.multipart.MultipartFile</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.servlet.mvc.support.RedirectAttributes</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.service.StorageService</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Controller</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">UploadController</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
StorageService storageService<span style="color: #333333;">;</span>
<span style="color: #888888;">// CHANGE IT ACCORDING TO YOUR LOCATION</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> String UPLOAD_FILE_LOCATION<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">"C:\\programinjava_workspace"</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@PostMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/upload"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">singleFileUpload</span><span style="color: #333333;">(</span><span style="color: #555555; font-weight: bold;">@RequestParam</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"file"</span><span style="color: #333333;">)</span> MultipartFile file<span style="color: #333333;">,</span> RedirectAttributes redirectAttributes<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>file<span style="color: #333333;">.</span><span style="color: #0000cc;">isEmpty</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
redirectAttributes<span style="color: #333333;">.</span><span style="color: #0000cc;">addFlashAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"message"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Please select a file to upload"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"redirect:uploadStatus"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
String UploadedFolderLocation <span style="color: #333333;">=</span> UPLOAD_FILE_LOCATION<span style="color: #333333;">+</span><span style="background-color: #fff0f0;">"/"</span><span style="color: #333333;">;</span>
String fileName <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #888888;">// this is done to work on IE as well</span>
String pattern <span style="color: #333333;">=</span> Pattern<span style="color: #333333;">.</span><span style="color: #0000cc;">quote</span><span style="color: #333333;">(</span>System<span style="color: #333333;">.</span><span style="color: #0000cc;">getProperty</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"file.separator"</span><span style="color: #333333;">));</span>
String<span style="color: #333333;">[]</span> str <span style="color: #333333;">=</span> file<span style="color: #333333;">.</span><span style="color: #0000cc;">getOriginalFilename</span><span style="color: #333333;">().</span><span style="color: #0000cc;">split</span><span style="color: #333333;">(</span>pattern<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>str<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span> <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">)</span>
fileName <span style="color: #333333;">=</span> str<span style="color: #333333;">[</span>str<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span> <span style="color: #333333;">-</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">else</span>
fileName <span style="color: #333333;">=</span> str<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(!</span>storageService<span style="color: #333333;">.</span><span style="color: #0000cc;">store</span><span style="color: #333333;">(</span>file<span style="color: #333333;">,</span> fileName<span style="color: #333333;">,</span>UPLOAD_FILE_LOCATION<span style="color: #333333;">))</span>
<span style="color: #333333;">{</span>
redirectAttributes<span style="color: #333333;">.</span><span style="color: #0000cc;">addFlashAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"message"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Error occurred while uploading the file"</span><span style="color: #333333;">);</span>
redirectAttributes<span style="color: #333333;">.</span><span style="color: #0000cc;">addFlashAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"status"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"false"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"redirect:/uploadStatus"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
redirectAttributes<span style="color: #333333;">.</span><span style="color: #0000cc;">addFlashAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"message"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"You successfully uploaded '"</span> <span style="color: #333333;">+</span> fileName <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"'"</span><span style="color: #333333;">);</span>
redirectAttributes<span style="color: #333333;">.</span><span style="color: #0000cc;">addFlashAttribute</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"status"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"true"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"redirect:/uploadStatus"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@GetMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/uploadStatus"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">uploadStatus</span><span style="color: #333333;">(</span>ModelMap m<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"Homepage"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #555555; font-weight: bold;">@GetMapping</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/upload"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">public</span> String <span style="color: #0066bb; font-weight: bold;">displayHomePageForAlarm</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">"Homepage"</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
<div>
<pre style="line-height: 16.25px;"><span style="color: #888888;">// CHANGE IT ACCORDING TO YOUR LOCATION</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> String UPLOAD_FILE_LOCATION<span style="color: #333333;">=</span><span style="background-color: #fff0f0;">"C:\\programinjava_workspace"</span><span style="color: #333333;">;</span></pre>
<b>NOTE : Change the location according to your local settings.</b><br />
Now we will see the Service class : In this class we have created method called store will will actually store the data from client side to server side<br />
<b>StoreService.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.net.MalformedURLException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.nio.file.Files</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.nio.file.Path</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.nio.file.Paths</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.nio.file.StandardCopyOption</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.core.io.Resource</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.core.io.UrlResource</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.util.FileSystemUtils</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.web.multipart.MultipartFile</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Service</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">StorageService</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> Path rootLocation <span style="color: #333333;">=</span> Paths<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"data/input_data"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">store</span><span style="color: #333333;">(</span>MultipartFile file<span style="color: #333333;">,</span>String fileName <span style="color: #333333;">,</span>String fileLocation<span style="color: #333333;">){</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
rootLocation <span style="color: #333333;">=</span> Paths<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>fileLocation<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>Files<span style="color: #333333;">.</span><span style="color: #0000cc;">copy</span><span style="color: #333333;">(</span>file<span style="color: #333333;">.</span><span style="color: #0000cc;">getInputStream</span><span style="color: #333333;">(),</span> <span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">rootLocation</span><span style="color: #333333;">.</span><span style="color: #0000cc;">resolve</span><span style="color: #333333;">(</span>fileName<span style="color: #333333;">),</span>StandardCopyOption<span style="color: #333333;">.</span><span style="color: #0000cc;">REPLACE_EXISTING</span><span style="color: #333333;">)></span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>Exception e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">RuntimeException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"FAIL!"</span> <span style="color: #333333;">+</span>e<span style="color: #333333;">.</span><span style="color: #0000cc;">getMessage</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> Resource <span style="color: #0066bb; font-weight: bold;">loadFile</span><span style="color: #333333;">(</span>String filename<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
Path file <span style="color: #333333;">=</span> rootLocation<span style="color: #333333;">.</span><span style="color: #0000cc;">resolve</span><span style="color: #333333;">(</span>filename<span style="color: #333333;">);</span>
Resource resource <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> UrlResource<span style="color: #333333;">(</span>file<span style="color: #333333;">.</span><span style="color: #0000cc;">toUri</span><span style="color: #333333;">());</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>resource<span style="color: #333333;">.</span><span style="color: #0000cc;">exists</span><span style="color: #333333;">()</span> <span style="color: #333333;">||</span> resource<span style="color: #333333;">.</span><span style="color: #0000cc;">isReadable</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> resource<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span><span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">RuntimeException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"FAIL!"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>MalformedURLException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">RuntimeException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"FAIL!"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">deleteAll</span><span style="color: #333333;">(</span>String path<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>path<span style="color: #333333;">!=</span><span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">&&</span> <span style="color: #333333;">!</span>path<span style="color: #333333;">.</span><span style="color: #0000cc;">isEmpty</span><span style="color: #333333;">())</span>
<span style="color: #333333;">{</span>
FileSystemUtils<span style="color: #333333;">.</span><span style="color: #0000cc;">deleteRecursively</span><span style="color: #333333;">(</span>Paths<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>path<span style="color: #333333;">).</span><span style="color: #0000cc;">toFile</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">else</span>
FileSystemUtils<span style="color: #333333;">.</span><span style="color: #0000cc;">deleteRecursively</span><span style="color: #333333;">(</span>rootLocation<span style="color: #333333;">.</span><span style="color: #0000cc;">toFile</span><span style="color: #333333;">());</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
Now let see the HTML template file<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><!DOCTYPE html></span>
<span style="color: #007700;"><html</span> <span style="color: #0000cc;">xmlns:th=</span><span style="background-color: #fff0f0;">"http://thymeleaf.org"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><head></span>
<span style="color: #007700;"><title></span>File Upload Example<span style="color: #007700;"></title></span>
<span style="color: #007700;"><link</span> <span style="color: #0000cc;">href=</span><span style="background-color: #fff0f0;">"webjars/bootstrap/3.3.7/css/bootstrap.min.css"</span>
<span style="color: #0000cc;">rel=</span><span style="background-color: #fff0f0;">"stylesheet"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"><script </span><span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"text/javascript"</span> <span style="color: #0000cc;">src=</span><span style="background-color: #fff0f0;">"webjars/jquery/3.3.1/jquery.min.js"</span><span style="color: #007700;">></script></span>
<span style="color: #007700;"><script </span><span style="color: #0000cc;">src=</span><span style="background-color: #fff0f0;">"webjars/bootstrap/3.3.7/js/bootstrap.min.js"</span><span style="color: #007700;">></script></span>
<span style="color: #007700;"></head></span>
<span style="color: #007700;"><script </span><span style="color: #0000cc;">th:inline=</span><span style="background-color: #fff0f0;">"javascript"</span><span style="color: #007700;">></span>
<span style="color: #888888;">/*<![CDATA[*/</span>
<span style="color: #008800; font-weight: bold;">var</span> _validFileExtensions <span style="color: #333333;">=</span> [ <span style="background-color: #fff0f0;">".csv"</span>, <span style="background-color: #fff0f0;">".zip"</span> ];
<span style="color: #008800; font-weight: bold;">function</span> Validate(oForm) {
<span style="color: #008800; font-weight: bold;">var</span> arrInputs <span style="color: #333333;">=</span> oForm.getElementsByTagName(<span style="background-color: #fff0f0;">"input"</span>);
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #008800; font-weight: bold;">var</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>; i <span style="color: #333333;"><</span> arrInputs.length; i<span style="color: #333333;">++</span>) {
<span style="color: #008800; font-weight: bold;">var</span> oInput <span style="color: #333333;">=</span> arrInputs[i];
<span style="color: #008800; font-weight: bold;">if</span> (oInput.type <span style="color: #333333;">==</span> <span style="background-color: #fff0f0;">"file"</span>) {
<span style="color: #008800; font-weight: bold;">var</span> sFileName <span style="color: #333333;">=</span> oInput.value;
<span style="color: #008800; font-weight: bold;">if</span> (sFileName.length <span style="color: #333333;">==</span> <span style="color: #0000dd; font-weight: bold;">0</span>) {
alert(<span style="background-color: #fff0f0;">"Please select a file to upload"</span>);
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;
}
<span style="color: #008800; font-weight: bold;">if</span> (sFileName.length <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">0</span>) {
<span style="color: #008800; font-weight: bold;">var</span> blnValid <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">false</span>;
<span style="color: #008800; font-weight: bold;">for</span> (<span style="color: #008800; font-weight: bold;">var</span> j <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span>; j <span style="color: #333333;"><</span> _validFileExtensions.length; j<span style="color: #333333;">++</span>) {
<span style="color: #008800; font-weight: bold;">var</span> sCurExtension <span style="color: #333333;">=</span> _validFileExtensions[j];
<span style="color: #008800; font-weight: bold;">if</span> (sFileName.substr(
sFileName.length <span style="color: #333333;">-</span> sCurExtension.length,
sCurExtension.length).toLowerCase() <span style="color: #333333;">==</span> sCurExtension
.toLowerCase()) {
blnValid <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">true</span>;
<span style="color: #008800; font-weight: bold;">break</span>;
}
}
<span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #333333;">!</span>blnValid) {
alert(<span style="background-color: #fff0f0;">"Invalid File Extension"</span>);
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;
}
}
}
}
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span>;
}
<span style="color: #888888;">/*]]>*/</span>
<span style="color: #007700;"></script></span>
<span style="color: #007700;"><body></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"container-fluid padding-0"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"row padding-0"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-4"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><h2></span>File Upload Example<span style="color: #007700;"></h2></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-4"</span> <span style="color: #0000cc;">align=</span><span style="background-color: #fff0f0;">"right"</span><span style="color: #007700;">></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><nav</span> <span style="color: #0000cc;">role=</span><span style="background-color: #fff0f0;">"navigation"</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"trainingset-container-id"</span>
<span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"navbar navbar-default"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"row"</span> <span style="color: #0000cc;">style=</span><span style="background-color: #fff0f0;">"margin-top: 10px;"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-2"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><B></span>Upload File<span style="color: #007700;"></B></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-md-6"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><form</span> <span style="color: #0000cc;">method=</span><span style="background-color: #fff0f0;">"POST"</span> <span style="color: #0000cc;">action=</span><span style="background-color: #fff0f0;">"/upload"</span>
<span style="color: #0000cc;">onsubmit=</span><span style="background-color: #fff0f0;">"return Validate(this);"</span> <span style="color: #0000cc;">enctype=</span><span style="background-color: #fff0f0;">"multipart/form-data"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-sm-6"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"file"</span> <span style="color: #0000cc;">name=</span><span style="background-color: #fff0f0;">"file"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"col-sm-6"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><input</span> <span style="color: #0000cc;">type=</span><span style="background-color: #fff0f0;">"submit"</span> <span style="color: #0000cc;">class=</span><span style="background-color: #fff0f0;">"btn btn-success btn-sm"</span> <span style="color: #0000cc;">value=</span><span style="background-color: #fff0f0;">"Upload data"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></form></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></nav></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"messageboxid"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><div</span> <span style="color: #0000cc;">id=</span><span style="background-color: #fff0f0;">"uploadstatus"</span> <span style="color: #0000cc;">th:if=</span><span style="background-color: #fff0f0;">"</span><span style="color: #557799;">${</span>message<span style="color: #557799;">}</span><span style="background-color: #fff0f0;">"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><B></span>Status Of Uploaded File<span style="color: #007700;"></B></span>
<span style="color: #007700;"><h6</span> <span style="color: #0000cc;">th:text=</span><span style="background-color: #fff0f0;">"</span><span style="color: #557799;">${</span>message<span style="color: #557799;">}</span><span style="background-color: #fff0f0;">"</span> <span style="color: #007700;">/></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></div></span>
<span style="color: #007700;"></body></span>
<span style="color: #007700;"></html></span>
</pre>
</div>
Thats it ,<br />
<br />
<b>How to run the webpage</b><br />
<b><br /></b>
you need to hit the below URL<br />
<b><i><span style="color: orange;">http://localhost:8080/upload</span></i></b><br />
<br />
you will be able to see the homepage and then you just need to upload the file and you will get the successful message once uploaded.<br />
<br />
Hope this will help you !!<br />
<br />
Read Also<br />
<ol style="background: rgb(255, 255, 255); border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-simple-application-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; outline: none; padding: 0px; vertical-align: baseline;" target="_blank">Spring Boot and Hibernate Tutorials - Application using Spring Boot</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">What is Spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/04/how-to-read-value-from-property-file-in_16.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">How to read value from property file in spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">First Rest API Application - Top Spring Framework Interview Questions</a></li>
</ol>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;">Thanks for reading</span></div>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;"><a href="http://www.noeik.com/" target="_blank">Noeik</a></span></div>
<br /></div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-30102978480298265802018-05-07T21:02:00.001+05:302018-05-07T21:02:12.565+05:30Password Encoding in Spring Security using Spring boot & JPA<div dir="ltr" style="text-align: left;" trbidi="on">
Password is one of the most sensitive information of user , which we need to secure at our first priority as a developer,<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-v12Rt1kNJFo/WvBw8sL63tI/AAAAAAAAGv4/Dic3QmnmCh0JsIbiivn59_DEIRqGkxSGwCLcBGAs/s1600/FotoJet%2B%252852%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-v12Rt1kNJFo/WvBw8sL63tI/AAAAAAAAGv4/Dic3QmnmCh0JsIbiivn59_DEIRqGkxSGwCLcBGAs/s320/FotoJet%2B%252852%2529-min.jpg" width="320" /></a></div>
<br />
<div>
Storing password as a plain text in database can always make a security breach as well as if someone get the access of database , he/she will be able to see the plain text of password , Its always recommended to store password in encrypted form or in array byte so that a normal person can't able to understand password.<br />
<a name='more'></a></div>
<div>
Also read : <a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" target="_blank">What is Spring boot ?</a></div>
<div>
In this article we will see how we can encrypt simply the password in spring boot using spring boot inbuild library.</div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
So lets start</div>
<div>
<br /></div>
<div>
<b><br /></b>
<b>Implementation : (</b>you can download it from <a href="https://github.com/programinjava/PasswordEncodingExampleInSpringBoot.git" target="_blank">GitHub</a> as well)</div>
<div>
<br /></div>
<div>
We first create the project <a href="http://start.spring.io/">start.spring.io</a></div>
<div>
<br /></div>
<div>
Now we will see project structure</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-Q2vPKk2VDcQ/WvBsiCuDNHI/AAAAAAAAGvY/tN2IHfGb-28aYHs_dIS_fHHvHv3U5J3ngCLcBGAs/s1600/passwordstrucute.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="661" data-original-width="544" height="400" src="https://4.bp.blogspot.com/-Q2vPKk2VDcQ/WvBsiCuDNHI/AAAAAAAAGvY/tN2IHfGb-28aYHs_dIS_fHHvHv3U5J3ngCLcBGAs/s400/passwordstrucute.JPG" width="328" /></a></div>
<div>
<br /></div>
<div>
we will now see the pom.xml and see all the dependencies we have added</div>
<div>
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color: #007700;"><project</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0"</span>
<span style="color: #0000cc;">xmlns:xsi=</span><span style="background-color: #fff0f0;">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color: #0000cc;">xsi:schemaLocation=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><modelVersion></span>4.0.0<span style="color: #007700;"></modelVersion></span>
<span style="color: #007700;"><groupId></span>com.programinjava.learn<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>PasswordEncodingInSpringBoot<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>0.0.1-SNAPSHOT<span style="color: #007700;"></version></span>
<span style="color: #007700;"><packaging></span>jar<span style="color: #007700;"></packaging></span>
<span style="color: #007700;"><name></span>PasswordEncodingInSpringBoot<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>Demo project for Spring Boot<span style="color: #007700;"></description></span>
<span style="color: #007700;"><parent></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-parent<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>1.5.12.RELEASE<span style="color: #007700;"></version></span>
<span style="color: #007700;"><relativePath</span> <span style="color: #007700;">/></span> <span style="color: #888888;"><!-- lookup parent from repository --></span>
<span style="color: #007700;"></parent></span>
<span style="color: #007700;"><properties></span>
<span style="color: #007700;"><project.build.sourceEncoding></span>UTF-8<span style="color: #007700;"></project.build.sourceEncoding></span>
<span style="color: #007700;"><project.reporting.outputEncoding></span>UTF-8<span style="color: #007700;"></project.reporting.outputEncoding></span>
<span style="color: #007700;"><java.version></span>1.8<span style="color: #007700;"></java.version></span>
<span style="color: #007700;"></properties></span>
<span style="color: #007700;"><dependencies></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-data-jpa<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-web<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-security<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.security<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-security-jwt<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #888888;"><!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 --></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.security.oauth<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-security-oauth2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #888888;"><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security --></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-security<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>com.h2database<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>h2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>runtime<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-test<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"></dependencies></span>
<span style="color: #007700;"><build></span>
<span style="color: #007700;"><plugins></span>
<span style="color: #007700;"><plugin></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-maven-plugin<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></plugin></span>
<span style="color: #007700;"></plugins></span>
<span style="color: #007700;"></build></span>
<span style="color: #007700;"></project></span>
</pre>
</div>
</div>
<br />
Here we have used the <b>BCryptPasswordEncoder</b> which is provided by spring security<br />
<ol style="text-align: left;">
<li><b>How to encode the password </b></li>
</ol>
We have added the @Bean in Service class of User.<br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.context.annotation.Bean</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.security.core.userdetails.UsernameNotFoundException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.security.crypto.password.PasswordEncoder</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Service</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.model.User</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.programinjava.learn.repository.UserRepository</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Service</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">UserService</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
UserRepository userRepository<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Bean</span>
<span style="color: #008800; font-weight: bold;">public</span> PasswordEncoder <span style="color: #0066bb; font-weight: bold;">getEncoder</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">BCryptPasswordEncoder</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> User <span style="color: #0066bb; font-weight: bold;">save</span><span style="color: #333333;">(</span>User user<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// Encoding the password</span>
user<span style="color: #333333;">.</span><span style="color: #0000cc;">setPassword</span><span style="color: #333333;">(</span>getEncoder<span style="color: #333333;">().</span><span style="color: #0000cc;">encode</span><span style="color: #333333;">(</span>user<span style="color: #333333;">.</span><span style="color: #0000cc;">getPassword</span><span style="color: #333333;">()));</span>
<span style="color: #008800; font-weight: bold;">return</span> userRepository<span style="color: #333333;">.</span><span style="color: #0000cc;">save</span><span style="color: #333333;">(</span>user<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">// this method is for match the password with entered one</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">matchDbPasswordWithEntered</span><span style="color: #333333;">(</span>String username<span style="color: #333333;">,</span>String enteredPassword<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
User user <span style="color: #333333;">=</span> userRepository<span style="color: #333333;">.</span><span style="color: #0000cc;">findByUsername</span><span style="color: #333333;">(</span>username<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>user <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">UsernameNotFoundException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"user not found"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getEncoder<span style="color: #333333;">().</span><span style="color: #0000cc;">matches</span><span style="color: #333333;">(</span>enteredPassword<span style="color: #333333;">,</span>user<span style="color: #333333;">.</span><span style="color: #0000cc;">getPassword</span><span style="color: #333333;">()))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Password Match"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// can change the passwrod as well and store in db</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// can throw the exception</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Password not matched"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
Here are are actually below saving the user to database , encoding the password .<br />
<br />
<b>2. How to Match the entered password with encrypted password stored in db.</b><br />
<pre style="line-height: 16.25px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">boolean</span> <span style="color: #0066bb; font-weight: bold;">matchDbPasswordWithEntered</span><span style="color: #333333;">(</span>String username<span style="color: #333333;">,</span>String enteredPassword<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
User user <span style="color: #333333;">=</span> userRepository<span style="color: #333333;">.</span><span style="color: #0000cc;">findByUsername</span><span style="color: #333333;">(</span>username<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>user <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">throw</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">UsernameNotFoundException</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"user not found"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span><span style="color: #333333;">(</span>getEncoder<span style="color: #333333;">().</span><span style="color: #0000cc;">matches</span><span style="color: #333333;">(</span>enteredPassword<span style="color: #333333;">,</span>user<span style="color: #333333;">.</span><span style="color: #0000cc;">getPassword</span><span style="color: #333333;">()))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Password Match"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// can change the passwrod as well and store in db</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// can throw the exception</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Password not matched"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></pre>
<br />
In this we are using <b>matches() </b>of Password Encoder.<br />
That it ,<br />
One more thing we have added the encoder in the Server config as well (see the github link)<br />
<pre style="color: #333333; line-height: 16.25px;"> <span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">protected</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">configure</span>(AuthenticationManagerBuilder auth) <span style="color: #008800; font-weight: bold;">throws</span> Exception {
auth.<span style="color: #0000cc;">userDetailsService</span>(userDetailsService)
<span style="color: #888888;">// .passwordEncoder(new ShaPasswordEncoder(encodingStrength));</span>
.<span style="color: #0000cc;">passwordEncoder</span>(getEncoder());
}</pre>
this is used for checking authentication of current user , with the help of its role.<br />
<br />
Now Hit the Rest API and see the password in database<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-kN_4i_Lcnis/WvBu8IkEBDI/AAAAAAAAGvk/7R6-XuxeDKo3xRsU-4IaQYsAUkOl7ZaDgCLcBGAs/s1600/restresult.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="773" data-original-width="1432" height="344" src="https://4.bp.blogspot.com/-kN_4i_Lcnis/WvBu8IkEBDI/AAAAAAAAGvk/7R6-XuxeDKo3xRsU-4IaQYsAUkOl7ZaDgCLcBGAs/s640/restresult.JPG" width="640" /></a></div>
<br />
Here we can see the Response Status is 201 ( which shows that the user is stored in db)<br />
<br />
Now let see the DB row.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-4X58peMAAzc/WvBveR400VI/AAAAAAAAGvs/VyKHUk4CXDcMDkRBCwPbvlJ121X6GfNowCLcBGAs/s1600/db.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="77" data-original-width="488" height="62" src="https://4.bp.blogspot.com/-4X58peMAAzc/WvBveR400VI/AAAAAAAAGvs/VyKHUk4CXDcMDkRBCwPbvlJ121X6GfNowCLcBGAs/s400/db.JPG" width="400" /></a></div>
<br />
Here you can see the Password is encoded . which is quiet secure as compare to plain text.<br />
<br />
<span style="background-color: white; color: #444444; font-family: "georgia" , "times new roman" , "times" , serif; font-size: 16px;">Also Read:</span><br />
<ol style="background: rgb(255, 255, 255); border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-simple-application-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Spring Boot and Hibernate Tutorials - Application using Spring Boot</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">What is Spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/04/how-to-read-value-from-property-file-in_16.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">How to read value from property file in spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">First Rest API Application - Top Spring Framework Interview Questions</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/05/implementation-of-swagger-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Implementation of Swagger in Spring boot</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/05/how-to-use-h2-embedded-database-in.html" target="_blank">How to use h2 database in spring boot</a></li>
</ol>
<div>
<span style="color: #444444; font-family: georgia, times new roman, times, serif;">Thanks for reading </span></div>
<div>
<span style="color: #444444; font-family: georgia, times new roman, times, serif;"><a href="http://www.noeik.com/" target="_blank">noeik</a></span></div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-74833520142927307232018-05-02T21:31:00.002+05:302018-05-02T21:31:34.085+05:30How to Use H2 Embedded database in spring boot <div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-v0l1oWcsMJk/WunggaceJQI/AAAAAAAAGu8/i90BfLA5IGozodg4kEk6rMYvje2XAUYnACLcBGAs/s1600/FotoJet%2B%252850%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://2.bp.blogspot.com/-v0l1oWcsMJk/WunggaceJQI/AAAAAAAAGu8/i90BfLA5IGozodg4kEk6rMYvje2XAUYnACLcBGAs/s320/FotoJet%2B%252850%2529-min.jpg" width="320" /></a></div>
<br />
H2 database is a inmemory data base which is embedded and we can use it as in memory database. it is quiet easy to use H2 database if you are working with small scale application when you just need to dumb data in db and use it later in the computation.<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
<b>Advantage of using H2</b></div>
<div>
<ul style="text-align: left;">
<li>It will not used application memory and will use the separate memory on same host.</li>
<li>It is less cheaper to use than database which are not in memory.( network databases are expensive in term of speed ) when there are not much data need to store.</li>
</ul>
</div>
<div>
Now let see the <b>implementation of H2 Database</b> in <a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" target="_blank">spring boot</a></div>
<div>
You can download the project from <a href="https://github.com/programinjava/H2DbInSpringBootDemo.git" target="_blank">GitHub</a></div>
<div>
Lets first see the project structure</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/--YDIWIu3pis/WunZ2EFJBuI/AAAAAAAAGuU/X0FGsLzyE1kuZqA-_xIhy2HFEFPJIHvmQCLcBGAs/s1600/h2structure.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="600" data-original-width="494" height="320" src="https://1.bp.blogspot.com/--YDIWIu3pis/WunZ2EFJBuI/AAAAAAAAGuU/X0FGsLzyE1kuZqA-_xIhy2HFEFPJIHvmQCLcBGAs/s320/h2structure.JPG" width="263" /></a></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Now if we see the <b>pom.xml</b> what all the dependency we have added in the project</div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color: #007700;"><project</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0"</span> <span style="color: #0000cc;">xmlns:xsi=</span><span style="background-color: #fff0f0;">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color: #0000cc;">xsi:schemaLocation=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><modelVersion></span>4.0.0<span style="color: #007700;"></modelVersion></span>
<span style="color: #007700;"><groupId></span>com.programinjava.learn<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>H2DbInSpringBootDemo<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>0.0.1-SNAPSHOT<span style="color: #007700;"></version></span>
<span style="color: #007700;"><packaging></span>jar<span style="color: #007700;"></packaging></span>
<span style="color: #007700;"><name></span>H2DbInSpringBootDemo<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>Demo project for Spring Boot<span style="color: #007700;"></description></span>
<span style="color: #007700;"><parent></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-parent<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.0.1.RELEASE<span style="color: #007700;"></version></span>
<span style="color: #007700;"><relativePath</span> <span style="color: #007700;">/></span> <span style="color: #888888;"><!-- lookup parent from repository --></span>
<span style="color: #007700;"></parent></span>
<span style="color: #007700;"><properties></span>
<span style="color: #007700;"><project.build.sourceEncoding></span>UTF-8<span style="color: #007700;"></project.build.sourceEncoding></span>
<span style="color: #007700;"><project.reporting.outputEncoding></span>UTF-8<span style="color: #007700;"></project.reporting.outputEncoding></span>
<span style="color: #007700;"><java.version></span>1.8<span style="color: #007700;"></java.version></span>
<span style="color: #007700;"></properties></span>
<span style="color: #007700;"><dependencies></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-data-jpa<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-web<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>com.h2database<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>h2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>runtime<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-test<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>com.h2database<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>h2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"></dependencies></span>
<span style="color: #007700;"><build></span>
<span style="color: #007700;"><plugins></span>
<span style="color: #007700;"><plugin></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-maven-plugin<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></plugin></span>
<span style="color: #007700;"></plugins></span>
<span style="color: #007700;"></build></span>
<span style="color: #007700;"></project></span>
</pre>
</div>
</div>
So we have add below dependency in project<br />
<div style="text-align: left;">
</div>
<ol>
<li><span style="color: #ffd966;">Spring- Web</span></li>
<li><span style="color: #ffd966;">JPA</span></li>
<li><span style="color: #ffd966;">H2 database</span></li>
</ol>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br />
Now let see the application.properties what we have added in it to configure the H2 Database<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;"># H2</span>
<span style="color: #0000cc;">spring.h2.console.enabled</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">true</span>
<span style="color: #0000cc;">spring.h2.console.path</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">/h2</span>
<span style="color: #888888;"># Datasource</span>
<span style="color: #0000cc;">spring.datasource.url</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">jdbc:h2:file:~/test</span>
<span style="color: #0000cc;">spring.datasource.username</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">sa</span>
<span style="color: #0000cc;">spring.datasource.password</span><span style="color: #333333;">=</span>
<span style="color: #0000cc;">spring.datasource.driver-class-name</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">org.h2.Driver</span>
</pre>
</div>
<br />
Now there are new things we need to understand here<br />
<pre style="line-height: 16.25px;"><span style="color: #0000cc;">spring.h2.console.path</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">/h2</span></pre>
Here we can provide what path we want to give to see the H2 Console ex- /h2-console , /h2<br />
<pre style="line-height: 16.25px;"><span style="color: #0000cc;">spring.h2.console.enabled</span><span style="color: #333333;">=</span><span style="background-color: #fff0f0;">true</span></pre>
if this value will be true then only we will be able to see the H2 Console in browser.<br />
<br />
<b>Now let see how to access the H2 Console </b><br />
- Once you run the spring boot project , you can just go to the below url to see the h2 console.<br />
<br />
<div style="text-align: center;">
<a href="http://localhost:8080/h2">http://localhost:8080/h2</a></div>
<div style="text-align: center;">
<br /></div>
When you hit this url you will be able to see the below screen<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-Nkxt61YcDCg/WuncEOT3vPI/AAAAAAAAGuo/-sec0xcIrV0xd6ZQS0VaUAq4uUY1s1O2gCLcBGAs/s1600/h2console.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="603" data-original-width="756" height="255" src="https://3.bp.blogspot.com/-Nkxt61YcDCg/WuncEOT3vPI/AAAAAAAAGuo/-sec0xcIrV0xd6ZQS0VaUAq4uUY1s1O2gCLcBGAs/s320/h2console.JPG" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<b>Note : </b>Please ensure that the JDBC URL should be same as what you have mentioned in application.properties.<br />
<br />
After clicking on Connect button , you just see the below DB Console. when you can do all the operations.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://2.bp.blogspot.com/-lDOFbq8SuCM/WunceHIiYqI/AAAAAAAAGuw/H1IvV8cj1qMuHXMskBPfz3ID3X33Y38IgCLcBGAs/s1600/dfg.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="774" data-original-width="1600" height="308" src="https://2.bp.blogspot.com/-lDOFbq8SuCM/WunceHIiYqI/AAAAAAAAGuw/H1IvV8cj1qMuHXMskBPfz3ID3X33Y38IgCLcBGAs/s640/dfg.JPG" width="640" /></a></div>
<br />
That's all you need to do , Congratulations you are now using H2 database in your spring boot application.<br />
<br />
Also Read:<br />
<ol style="background: rgb(255, 255, 255); border: 0px; color: #444444; font-family: Georgia, "Times New Roman", Times, serif; font-size: 16px; line-height: 1.5; list-style: none outside; margin: 1.5em 2.2em; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-simple-application-in-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">Spring Boot and Hibernate Tutorials - Application using Spring Boot</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">What is Spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/04/how-to-read-value-from-property-file-in_16.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">How to read value from property file in spring boot ?</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); background: transparent; color: #e01222; margin: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">First Rest API Application - Top Spring Framework Interview Questions</a></li>
<li style="background: transparent; border: 0px; list-style-type: decimal; margin: 0px 0px 0.75em; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://www.programinjava.com/2018/05/implementation-of-swagger-in-spring-boot.html" target="_blank">Implementation of Swagger in Spring boot</a></li>
</ol>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;">Hope this will help you in using H2 in spring boot.</span></div>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;"><br /></span></div>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;">Please share it with your friends and colleagues .</span></div>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;"><br /></span></div>
<div>
<span style="color: #444444; font-family: "georgia" , "times new roman" , "times" , serif;">Thanks for reading</span></div>
<div>
<a href="http://www.noeik.com/" target="_blank">noeik</a></div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-20223833115907556432018-05-01T21:46:00.001+05:302018-05-01T21:46:07.979+05:30Implementation of Swagger in spring boot<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Swagger</b> is one of the most used documentation tool for the software engineering , when you have the APIs exposed to the outer world and you need to maintain the documents of Input and Output Contract or you need to see the sample input and expected sample output of the api , swagger is worth use.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-mhDS-KEimX0/WuiREqlhPOI/AAAAAAAAGuE/fHbZSSYsHCgYyg1LusH5obzCJ-zpQsgqgCLcBGAs/s1600/FotoJet%2B%252849%2529-min.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="481" data-original-width="672" height="228" src="https://1.bp.blogspot.com/-mhDS-KEimX0/WuiREqlhPOI/AAAAAAAAGuE/fHbZSSYsHCgYyg1LusH5obzCJ-zpQsgqgCLcBGAs/s320/FotoJet%2B%252849%2529-min.jpg" width="320" /></a></div>
<div>
<br /></div>
<div>
<b>What is Swagger ?</b></div>
<div>
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.<a class="q ruhjFe NJLBac fl" data-ved="0ahUKEwiSxs7b4eTaAhUHmZQKHUb4DXcQmhMI_wEwFg" href="https://en.wikipedia.org/wiki/Swagger_(software)" ping="/url?sa=t&source=web&rct=j&url=https://en.wikipedia.org/wiki/Swagger_(software)&ved=0ahUKEwiSxs7b4eTaAhUHmZQKHUb4DXcQmhMI_wEwFg" saprocessedanchor="true" style="color: #1a0dab; cursor: pointer; font-family: arial, sans-serif; font-size: small; text-decoration-line: none; white-space: nowrap;">Wikipedia</a></div>
<div>
<br /></div>
<div>
<b>Implementation of swagger in spring boot :</b></div>
<div>
<a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" target="_blank">Also Read : What is spring boot?</a></div>
<div>
<a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" target="_blank">spring boot</a> provide very easy implementation of swagger tool and you just need to write few line of code along with few jars , your documentation tool is ready to use.</div>
<div>
<br /></div>
<div>
<b>Now let see the coding implementation</b></div>
<div>
<br /></div>
<div>
<b><u>Pseudo Code :</u></b></div>
<div>
First we will create 2 rest apis , one will be get and second will be post</div>
<div>
post endpoint will take input as user dto and get will use to fetch the user.</div>
<div>
then after we will add the swagger dependency in pom.xml </div>
<div>
Now we will implement swagger code.</div>
<div>
after that we are done and will run the server and will show you the swagger ui.</div>
<div>
<br />
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
<br />
<br /></div>
<div>
<b>Implementation Code : </b>( Can download it from <a href="https://github.com/programinjava/SwaggerInSpringBootDemo.git" target="_blank">GitHub</a> as well)</div>
<div>
First we will see the <b>code structure</b> of projects</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://1.bp.blogspot.com/-mwN4eUt_hDQ/WuiI1LyXxHI/AAAAAAAAGts/8IE0HcL9tyc5kAdXcp0FC6EIEc1X58YHwCLcBGAs/s1600/structure.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="486" data-original-width="434" height="320" src="https://1.bp.blogspot.com/-mwN4eUt_hDQ/WuiI1LyXxHI/AAAAAAAAGts/8IE0HcL9tyc5kAdXcp0FC6EIEc1X58YHwCLcBGAs/s320/structure.JPG" width="285" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div>
Now we will see the pom.xml how it looks</div>
<div>
<b>pom.xml</b></div>
<div>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #557799;"><?xml version="1.0" encoding="UTF-8"?></span>
<span style="color: #007700;"><project</span> <span style="color: #0000cc;">xmlns=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0"</span> <span style="color: #0000cc;">xmlns:xsi=</span><span style="background-color: #fff0f0;">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span style="color: #0000cc;">xsi:schemaLocation=</span><span style="background-color: #fff0f0;">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span style="color: #007700;">></span>
<span style="color: #007700;"><modelVersion></span>4.0.0<span style="color: #007700;"></modelVersion></span>
<span style="color: #007700;"><groupId></span>com.programinjava.learn<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>SwaggerDemo<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>0.0.1-SNAPSHOT<span style="color: #007700;"></version></span>
<span style="color: #007700;"><packaging></span>jar<span style="color: #007700;"></packaging></span>
<span style="color: #007700;"><name></span>SwaggerDemo<span style="color: #007700;"></name></span>
<span style="color: #007700;"><description></span>Demo project for Spring Boot<span style="color: #007700;"></description></span>
<span style="color: #007700;"><parent></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-parent<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.0.1.RELEASE<span style="color: #007700;"></version></span>
<span style="color: #007700;"><relativePath</span> <span style="color: #007700;">/></span> <span style="color: #888888;"><!-- lookup parent from repository --></span>
<span style="color: #007700;"></parent></span>
<span style="color: #007700;"><properties></span>
<span style="color: #007700;"><project.build.sourceEncoding></span>UTF-8<span style="color: #007700;"></project.build.sourceEncoding></span>
<span style="color: #007700;"><project.reporting.outputEncoding></span>UTF-8<span style="color: #007700;"></project.reporting.outputEncoding></span>
<span style="color: #007700;"><java.version></span>1.8<span style="color: #007700;"></java.version></span>
<span style="color: #007700;"></properties></span>
<span style="color: #007700;"><dependencies></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-web<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-data-jpa<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #888888;"><!-- added for swagger --></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>io.springfox<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>springfox-swagger2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.7.0<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>io.springfox<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>springfox-swagger-ui<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.7.0<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #888888;"><!-- swagger added end --></span>
<span style="color: #888888;"><!-- H2 Database --></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>com.h2database<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>h2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-starter-test<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><scope></span>test<span style="color: #007700;"></scope></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"></dependencies></span>
<span style="color: #007700;"><build></span>
<span style="color: #007700;"><plugins></span>
<span style="color: #007700;"><plugin></span>
<span style="color: #007700;"><groupId></span>org.springframework.boot<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>spring-boot-maven-plugin<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"></plugin></span>
<span style="color: #007700;"></plugins></span>
<span style="color: #007700;"></build></span>
<span style="color: #007700;"></project></span>
</pre>
</div>
</div>
The main import dependency in this pom is below one<br />
<pre style="line-height: 16.25px;"><span style="color: #888888;"><!-- added for swagger --></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>io.springfox<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>springfox-swagger2<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.7.0<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #007700;"><dependency></span>
<span style="color: #007700;"><groupId></span>io.springfox<span style="color: #007700;"></groupId></span>
<span style="color: #007700;"><artifactId></span>springfox-swagger-ui<span style="color: #007700;"></artifactId></span>
<span style="color: #007700;"><version></span>2.7.0<span style="color: #007700;"></version></span>
<span style="color: #007700;"></dependency></span>
<span style="color: #888888;"><!-- swagger added end --></span></pre>
These will add the Swagger dependency in file.<br />
Now we will add new class in project which is our swagger configuration class .<br />
package name : <b>com.programinjava.learn.config</b><br />
class name :<b> SwaggerConfig.java</b><br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">programinjava</span><span style="color: #333333;">.</span><span style="color: #0000cc;">learn</span><span style="color: #333333;">.</span><span style="color: #0000cc;">config</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">static</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">google</span><span style="color: #333333;">.</span><span style="color: #0000cc;">common</span><span style="color: #333333;">.</span><span style="color: #0000cc;">base</span><span style="color: #333333;">.</span><span style="color: #0000cc;">Predicates</span><span style="color: #333333;">.</span><span style="color: #0000cc;">or</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">static</span> springfox<span style="color: #333333;">.</span><span style="color: #0000cc;">documentation</span><span style="color: #333333;">.</span><span style="color: #0000cc;">builders</span><span style="color: #333333;">.</span><span style="color: #0000cc;">PathSelectors</span><span style="color: #333333;">.</span><span style="color: #0000cc;">regex</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.context.annotation.Bean</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.context.annotation.Configuration</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.google.common.base.Predicate</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.builders.ApiInfoBuilder</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.builders.PathSelectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.builders.RequestHandlerSelectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.service.ApiInfo</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.spi.DocumentationType</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.spring.web.plugins.Docket</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">springfox.documentation.swagger2.annotations.EnableSwagger2</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Configuration</span>
<span style="color: #555555; font-weight: bold;">@EnableSwagger2</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SwaggerConfig</span> <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Bean</span>
<span style="color: #008800; font-weight: bold;">public</span> Docket <span style="color: #0066bb; font-weight: bold;">api</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">Docket</span><span style="color: #333333;">(</span>DocumentationType<span style="color: #333333;">.</span><span style="color: #0000cc;">SWAGGER_2</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">select</span><span style="color: #333333;">()</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">apis</span><span style="color: #333333;">(</span>RequestHandlerSelectors<span style="color: #333333;">.</span><span style="color: #0000cc;">any</span><span style="color: #333333;">())</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">paths</span><span style="color: #333333;">(</span>PathSelectors<span style="color: #333333;">.</span><span style="color: #0000cc;">any</span><span style="color: #333333;">())</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">build</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> Predicate<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">postPaths</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #0066bb; font-weight: bold;">or</span><span style="color: #333333;">(</span>regex<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"/api/**"</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> ApiInfo <span style="color: #0066bb; font-weight: bold;">apiInfo</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">ApiInfoBuilder</span><span style="color: #333333;">().</span><span style="color: #0000cc;">title</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Program in java APIs"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">description</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Program in java APIs reference for developers"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">termsOfServiceUrl</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"https://www.programinjava.com"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">contact</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"admin@programinjava.com"</span><span style="color: #333333;">).</span><span style="color: #0000cc;">license</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Free License"</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">licenseUrl</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"admin@programinjava.com"</span><span style="color: #333333;">).</span><span style="color: #0000cc;">version</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"1.0"</span><span style="color: #333333;">).</span><span style="color: #0000cc;">build</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
<br />
Thats it , you have just implemented the swagger in your project. No further action required.<br />
<br />
<b>How to see the swagger UI ?</b><br />
You check the endpoint docs you need to hit the below URL<br />
<a href="http://localhost:8080/swagger-ui.html">http://localhost:8080/swagger-ui.html</a><br />
<br />
and your swagger page will look like this :<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://4.bp.blogspot.com/-HQ_fVQyYU9U/WuiJgGaOhKI/AAAAAAAAGt0/8WHvfI-DjkU5oAeaFTlBgnyKkxr31Z7NACLcBGAs/s1600/swagger.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="655" data-original-width="1600" height="260" src="https://4.bp.blogspot.com/-HQ_fVQyYU9U/WuiJgGaOhKI/AAAAAAAAGt0/8WHvfI-DjkU5oAeaFTlBgnyKkxr31Z7NACLcBGAs/s640/swagger.JPG" width="640" /></a></div>
Hope this will help you in implementing swagger in your project.<br />
Read More:<br />
<div style="text-align: left;">
</div>
<ol>
<li><a href="https://www.programinjava.com/2018/02/first-simple-application-in-spring-boot.html" target="_blank">Spring Boot and Hibernate Tutorials - Application using Spring Boot</a></li>
<li><a href="https://www.programinjava.com/2018/02/what-is-spring-boot.html" target="_blank">What is Spring boot ?</a></li>
<li><a href="https://www.programinjava.com/2018/04/how-to-read-value-from-property-file-in_16.html" target="_blank">How to read value from property file in spring boot ?</a></li>
<li><a href="https://www.programinjava.com/2018/02/first-rest-web-service-application.html" target="_blank">First Rest API Application - Top Spring Framework Interview Questions</a></li>
</ol>
<br />
<div style="text-align: left;">
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div style="text-align: left;">
If you have any issue while implementing the swagger , leave us a comment , will be happy to help</div>
If you like this post please share<br />
<br />
Thanks for reading<br />
<a href="http://www.noeik.com/" target="_blank">noeik</a><br />
<br /></div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0tag:blogger.com,1999:blog-2631709091184991837.post-45734542023073002632018-04-26T02:17:00.005+05:302018-04-26T02:17:56.453+05:30Memory Management in Java Program Quiz<div dir="ltr" style="text-align: left;" trbidi="on">
Memory Management is one of the important aspect of core java , a good programmer is always the one who always think about memory utilization while doing coding. so lets see how good you are in memory management basic concepts.<br />
<a name='more'></a><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- InPostArticle-Ads -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="8317756148" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<div>
<br /></div>
<div>
Before we move you can check previous quiz we have posted on our site.</div>
<div>
<ol style="text-align: left;">
<li><a href="https://www.programinjava.com/2018/04/top-java-interview-objective-questions.html" target="_blank">Top Java Interview objective questions quiz for fresher</a></li>
<li><a href="https://www.programinjava.com/2018/04/exception-handling-quiz-for-java.html" target="_blank">Exception Handling Quiz for Java Developers</a></li>
</ol>
<div>
Lets start the Quiz</div>
</div>
<div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
<div>
<br />
<script src="//www.quibblo.com/Scripts/qwidget2.js?id=c3Yj-NEU" type="text/javascript"></script><br />
<div class="quibblo_widget" id="quibblo_quiz_c3Yj_NEU" style="height: 300px; width: 100%;">
<div class="quibblo_box">
<div class="quibblo_title">
<h1 class="quibblo_icon i_quibblo">
<a href="https://www.quibblo.com/quiz/c3Yj-NEU/Memory-Management-in-java-Quiz" target="_blank" title="Should i do this?">Memory Management in java Quiz</a></h1>
</div>
<div class="quibblo_body">
<iframe height="300px" id="quibblo_quiz_c3Yj_NEU_iframe" src="https://www.quibblo.com/embed/c3Yj-NEU" width="100%"></iframe></div>
</div>
</div>
</div>
<div>
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- inArticle-Programinjava -->
<br />
<ins class="adsbygoogle" data-ad-client="ca-pub-6888359403102748" data-ad-format="auto" data-ad-slot="5539353442" style="display: block;"></ins><script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div>
Adminhttp://www.blogger.com/profile/03670282434996183807noreply@blogger.com0