tag:blogger.com,1999:blog-79416668680220235502024-03-17T20:03:07.218-07:00Not Just Another Blogshazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.comBlogger118125tag:blogger.com,1999:blog-7941666868022023550.post-73695084690321609592021-06-15T05:50:00.001-07:002021-06-15T05:50:34.520-07:00Java 16 Features<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglM-eNBxWRw4Hf9khITfYOXLjrMRrjAJvUV3BLqn6wn9jcQD28YfgMzMGOXeHaSbGUkSRsQ4fI4_e7UY_GGJ7ZGZrP_mqfYPJCDnqYlJnG2cXc9CCpeKMUjPvs-gPvpPSwvlduXe3l1sA/s2000/1__ffpkcwd_kwqmc7oevcrbq.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="2000" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglM-eNBxWRw4Hf9khITfYOXLjrMRrjAJvUV3BLqn6wn9jcQD28YfgMzMGOXeHaSbGUkSRsQ4fI4_e7UY_GGJ7ZGZrP_mqfYPJCDnqYlJnG2cXc9CCpeKMUjPvs-gPvpPSwvlduXe3l1sA/w400-h240/1__ffpkcwd_kwqmc7oevcrbq.jpeg" width="400" /></a></div><br /><p>Java is nearing its next Long Term Support (LTS) Release which is Java 17. Thus far it has released Java 16 General Availability (GA) Release which can be used in Production. There are some new features available in Java 16 which we are going to look at right now.</p><p><b>Record </b></p><p>Record is simplified way to declare a class which is used only to represent data. This type of classes are defined as Data Transfer Objects (DTO) or Entities in ORM frameworks to represent Tables. The current way to declare this type of classes is to create a public class with properties and all argument constructor and define all getters/setters along with equals, hashCode and toString. This requires a lot of additional code which can be reduced using code generation libraries and plugins like Lombok yet it is still an hassle. </p><p>Java 16 introduces record type to ease creation of such classes. </p><pre style="background-attachment: initial; background-clip: initial; background-color: #f0f0f0; background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb3aXb-mfAlwnvabrUbnuC6a5VaWCAZRpToTPL-fyY2NXaLqdShW8IWKkASKTRfIiwe2foT1ywFsvf37BowAdpQX30HBQLGTHszKQO_AzjZ3RKKFGt_6fkkgoyOHsXNrgfZB6t4gFmWLP8/s320/codebg.gif); background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; background: rgb(240, 240, 240); border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; overflow-wrap: normal; word-wrap: normal;"> import java.util.*;
public class RecordTest {
public static void main(String... args) {
Student s1 = new Student(1, "Shazin", 1);
Student s2 = new Student(2, "Shahim", 2);
System.out.println("Student 1 : "+s1);
System.out.println("Student 2 : "+s2);
System.out.println("s1 == s2 : "+(s1 == s2));
System.out.println("s1.equals(s1) : "+(s1.equals(s1)));
System.out.println("s1.equals(s2) : "+(s1.equals(s2)));
record GraduateStudent(Student student, List<String> qualifications) {};
GraduateStudent gs1 = new GraduateStudent(s1, Arrays.asList("A/S", "BSc"));
System.out.println("GraduateStudent 1 : " + gs1);
}
private static record Student(Integer id, String name, Integer grade) {
public Student { // All args constructor
if (grade < 0 || grade > 13) {
throw new IllegalArgumentException("Grade must be between 1 and 13");
}
}
}
} </code></pre>
The above example shows the use a Record in Java 16. The record Student is defined below as a static variable with attributes id, name and grade. The getters/setters with equals, hashCode and toString will be generated by default for this.<p></p><div>Once instantiated the a record object acts as a regular object. The only difference is that generated accessors/modifiers don't follow Java Beans convention (getter for name is name() not getName()) thus not a drop in replacement for Java Beans. Due to this reason record types are not yet supported by JSON Serializers and ORM frameworks but its a work in progress.</div><div><br /></div><div><b>New methods in Stream interface</b></div><div><b><br /></b></div><div>There are several new methods introduced in Stream interface which can be used to increase performance and reduce boilerplate code. One is the Stream.toList() method.</div><div><br /></div><div>
<pre style="background-attachment: initial; background-clip: initial; background-color: #f0f0f0; background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb3aXb-mfAlwnvabrUbnuC6a5VaWCAZRpToTPL-fyY2NXaLqdShW8IWKkASKTRfIiwe2foT1ywFsvf37BowAdpQX30HBQLGTHszKQO_AzjZ3RKKFGt_6fkkgoyOHsXNrgfZB6t4gFmWLP8/s320/codebg.gif); background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; background: rgb(240, 240, 240); border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; overflow-wrap: normal; word-wrap: normal;"> List<Integer> evenNos = nos.stream().filter(i -> i % 2 == 0).toList(); //.collect(Collectors.toList())
System.out.println("Even numbers : "+evenNos);
</code></pre>
This method reduces the need to call collect method when there is need to aggregate a Stream values to a List.
<br /></div><div><br /></div><div>Also there is a new method introduced which is Stream.mapMulti() which is a more imperative implementation of Stream.flatMap(). The practical use of this is a bit complex but this <a href="https://nipafx.dev/java-16-stream-mapmulti/" target="_blank">article</a> by Nicolai Parlog explains it well. I suggest you read it.</div><div><br /></div><div><b>Pattern Matching</b></div><div><b><br /></b></div><div>Pattern Matching is a very useful addition to Java 16 which eliminates the need of casting after the use of instanceof operator.</div><div><br />
<pre style="background-attachment: initial; background-clip: initial; background-color: #f0f0f0; background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb3aXb-mfAlwnvabrUbnuC6a5VaWCAZRpToTPL-fyY2NXaLqdShW8IWKkASKTRfIiwe2foT1ywFsvf37BowAdpQX30HBQLGTHszKQO_AzjZ3RKKFGt_6fkkgoyOHsXNrgfZB6t4gFmWLP8/s320/codebg.gif); background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; background: #f0f0f0; border: 1px dashed rgb(204, 204, 204); color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; overflow-wrap: normal; word-wrap: normal;"> import java.util.*;
public class PatternMatchingTest {
public static void main(String... args) {
String name = "Shazin";
List<Integer> nos = Arrays.asList(1, 2, 3, 4, 5);
Map map = Collections.singletonMap("Key", "Value");
Long bigInt = 1l;
System.out.println("Name Length : " + length(name));
System.out.println("Nos Length : " + length(nos));
System.out.println("Map Length : " + length(map));
System.out.println("bigInt Length : " + length(bigInt));
}
private static int length(Object o) {
if (o instanceof String s) { // s variable of type String
return s.length();
} else if (o instanceof Collection c) { // c variable of type Collection
return c.size();
} else if (o instanceof Map m) { // m variable of type Map
return m.size();
} else { // o variable of type Object
return 0;
}
}
}
</code></pre>
This feature allows to use a local scoped variable name right after the use of instanceof operator which matches the type used and can be used without casting. This eliminates a lot of boilerplate code.</div><div><br /></div><div><br /></div>shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-87521039826791693842021-04-18T01:15:00.001-07:002021-04-18T01:18:33.106-07:00Writing Cloud Native Spring Applications with Docker, GraalVM and Spring Native <p>Cloud Computing has taken the IT world by storm because of its versatility, low initial capital required to get started and low operational costs. Many new programming languages/platforms such as Go, Node JS, etc. have been tailormade to work on Cloud nodes </p><p>But many Enterprise level Software applications still run on Java and have failed to harness full capabilities of the Cloud due to its high memory footprint and high latency due to its Just in Time (JIT) compilation. These contribute to slower start up times and cold starts.</p><p>In order to tackle problems Oracle Labs Team introduced a VM called <a href="https://www.graalvm.org/">GraalVM</a> which enabled language agnostic method to produce Cloud Native applications using Java (not only Java) along with capabilities to integrate Spring as well.</p><p>In order to harness the capabilities of GraalVM and easy development Spring introduced a developer tool named Spring Native which enables to convert most of the Spring Applications to Docker Container Images with GraalVM image as base which converts the Spring Application to a Native application runs it.</p><p>This enables faster startup time, low latency and low memory usage. GraalVM achieves this by using its Ahead of Time (AOT) compilation and building capabilities. </p><p>You can use this developer tool directly from <a href="http://start.spring.io">Spring Initializr</a> as below;</p><p></p><div class="separator" style="clear: both; text-align: center;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2p4sFIOuLBVY2auNjXWaA1JNxtKp-XV5MlLnARQ-MwgKDmOhIXy70RCT_fvKJXfa0xKnvkpkE8gsFlLb7aYO_LwdhFmjQ-EhU5CPDFRoMtvo7yNARECWDBXv6EohprAaHG-4zd9vv9w4/" style="margin-left: 1em; margin-right: 1em;"><img alt="" data-original-height="942" data-original-width="1665" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2p4sFIOuLBVY2auNjXWaA1JNxtKp-XV5MlLnARQ-MwgKDmOhIXy70RCT_fvKJXfa0xKnvkpkE8gsFlLb7aYO_LwdhFmjQ-EhU5CPDFRoMtvo7yNARECWDBXv6EohprAaHG-4zd9vv9w4/w400-h226/image.png" width="400" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;">Using this tool will add a new plugin and a Spring Native dependency to the maven pom.xml of the Spring Application. This plugin is available for Gradle as well if you fancy that.</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">If you have a Simple Spring Application like below;<br /></div><div class="separator" style="clear: both; text-align: left;"><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #bbb529;">@SpringBootApplication<br /></span><span style="color: #bbb529;">@RestController<br /></span><span style="color: #bbb529;">@RequestMapping</span>(<span style="color: #6a8759;">"/native"</span>)<br /><span style="color: #cc7832;">public class </span>SpringNativeDemoApplication {<br /><br /> <span style="color: #cc7832;">public static void </span><span style="color: #ffc66d;">main</span>(String[] args) {<br /> SpringApplication.<span style="font-style: italic;">run</span>(SpringNativeDemoApplication.<span style="color: #cc7832;">class, </span>args)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /><br /> <span style="color: #bbb529;">@PostConstruct<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">public void </span><span style="color: #ffc66d;">init</span>() {<br /> System.<span style="color: #9876aa; font-style: italic;">out</span>.println(<span style="color: #6a8759;">"Initialized..."</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /><br /> <span style="color: #bbb529;">@GetMapping</span>(<span style="color: #6a8759;">"/greet"</span>)<br /> <span style="color: #cc7832;">public </span>String <span style="color: #ffc66d;">greet</span>() {<br /> <span style="color: #cc7832;">return </span><span style="color: #6a8759;">"Hello"</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>}<br /><br /><br />}<br /></pre></div><div class="separator" style="clear: both; text-align: left;">Which has an endpoint /native/greet you will see the dependency in the pom.xml following;</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #e8bf6a;"><dependency><br /></span><span style="color: #e8bf6a;"> <groupId></span>org.springframework.experimental<span style="color: #e8bf6a;"></groupId><br /></span><span style="color: #e8bf6a;"> <artifactId></span>spring-native<span style="color: #e8bf6a;"></artifactId><br /></span><span style="color: #e8bf6a;"> <version></span>${spring-native.version}<span style="color: #e8bf6a;"></version><br /></span><span style="color: #e8bf6a;"></dependency></span></pre></div><br /></div><div class="separator" style="clear: both; text-align: left;">And plugin under plugins;</div><div class="separator" style="clear: both; text-align: left;"><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #e8bf6a;"><plugin><br /></span><span style="color: #e8bf6a;"> <groupId></span>org.springframework.boot<span style="color: #e8bf6a;"></groupId><br /></span><span style="color: #e8bf6a;"> <artifactId></span>spring-boot-maven-plugin<span style="color: #e8bf6a;"></artifactId><br /></span><span style="color: #e8bf6a;"> <configuration><br /></span><span style="color: #e8bf6a;"> <image><br /></span><span style="color: #e8bf6a;"> <builder></span>paketobuildpacks/builder:tiny<span style="color: #e8bf6a;"></builder><br /></span><span style="color: #e8bf6a;"> <env><br /></span><span style="color: #e8bf6a;"> <BP_NATIVE_IMAGE></span>true<span style="color: #e8bf6a;"></BP_NATIVE_IMAGE><br /></span><span style="color: #e8bf6a;"> </env><br /></span><span style="color: #e8bf6a;"> </image><br /></span><span style="color: #e8bf6a;"> </configuration><br /></span><span style="color: #e8bf6a;"></plugin></span></pre></div>You can build the Docker container using the following command;<p></p><div><blockquote>./mvnw spring-boot:build-image</blockquote><p>This will generate the native image with ahead of time compilation. The generated executable will have all the necessary classes and statically linked native code from the SDK. Substrate VM which is a docker image provided by GraalVM is used here as the base of the image.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKPUSEMnFyNNRuUhErsE65iSVLZ5zudNAinJRX4ZVReGigmOfeBgqr0RpeuF-OGN-5HM-v3QIfaZKLmnVomnKTn4jKtcXverGWJDwKSCymfk7D7ZAacycbSvAzhTldJ671rVl8Qbl1SCY/s1920/Screenshot+from+2021-04-17+13-51-24.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1080" data-original-width="1920" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiKPUSEMnFyNNRuUhErsE65iSVLZ5zudNAinJRX4ZVReGigmOfeBgqr0RpeuF-OGN-5HM-v3QIfaZKLmnVomnKTn4jKtcXverGWJDwKSCymfk7D7ZAacycbSvAzhTldJ671rVl8Qbl1SCY/w400-h225/Screenshot+from+2021-04-17+13-51-24.png" width="400" /> </a></div><div class="separator" style="clear: both; text-align: center;"> </div><div class="separator" style="clear: both; text-align: left;"> After a long build process which is one of the downsides of this approach for the time being. Following command be used to list down the newly built image in Docker.</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><blockquote>./docker images | grep spring-native-demo</blockquote></div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">Then the image can be run using the following command to be listening at port 8088.</div><div class="separator" style="clear: both; text-align: left;"></div><blockquote><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">./docker run -p 8088:8080 spring-native-demo:0.0.1-SNAPSHOT</div></blockquote><p>This will start up the Spring Application very quickly due to the Ahead of Time compilation capability of GraalVM. </p><p>You can hit the endpoint using following command or by visiting the url in a browser;</p><p></p><blockquote>curl localhost:8088/native/greet --silent</blockquote><p>As of now not all Spring Eco System modules are supported in Spring Native. But it is a work in progress. <br /></p>There are numerous <a href="https://github.com/spring-projects-experimental/spring-native/tree/main/samples">samples</a> provided by Spring Team on how to use Spring Native in with other Spring Modules. </div><div><br /></div><div>More details can be found at <a href="https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/">Spring Native documentation</a>. You can find the Source code at <a href="https://github.com/shazin/spring-native-demo">Github</a>.<br /><p></p><div class="separator" style="clear: both; text-align: left;"><br /></div><br /> <p></p><p> </p></div>shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-13734658549505592912020-11-20T08:47:00.001-08:002020-11-20T20:48:18.579-08:00Spring Data Elasticsearch GeoPoint Field in Index<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzfrBDboOCzVeg4cGdZdkpTldar-2yb0SuNJ6jcwm4aSA1IZDvRQRIiCZi6E-NJkn9jTcVxyWQf2hyphenhyphenqt4N7sgloSk980EM-uiJeyaLE95AERUCK2HuxwdL3GtH624gxtGcAidqVD3owa4/s428/logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="216" data-original-width="428" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzfrBDboOCzVeg4cGdZdkpTldar-2yb0SuNJ6jcwm4aSA1IZDvRQRIiCZi6E-NJkn9jTcVxyWQf2hyphenhyphenqt4N7sgloSk980EM-uiJeyaLE95AERUCK2HuxwdL3GtH624gxtGcAidqVD3owa4/s320/logo.png" width="320" /></a></div><p>Recently I was working on a Project which involved Spring Data Elasticsearch and needed to use geo_point type field in the Elasticsearch index for speedier location based searches. Spring Data Elasticsearch provides a data type by the name <b>GeoPoint</b> to achieve this and an annotation named <b>@GeoPointField</b> to mark such fields.</p><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #bbb529;">@Document</span>(indexName = <span style="color: #6a8759;">"properties"</span><span style="color: #cc7832;">, </span>replicas = <span style="color: #6897bb;">0</span><span style="color: #cc7832;">, </span>refreshInterval = <span style="color: #6a8759;">"-1"</span>)<span style="color: #bbb529;"><br /></span><span style="color: #cc7832;">public class </span>Property <span style="color: #cc7832;">implements </span>Serializable {<br /><br /> <span style="color: #bbb529;">...</span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;"> </span><span style="color: #bbb529;">@GeoPointField<br /></span><span style="color: #bbb529;"> </span><span style="color: #cc7832;">private </span>GeoPoint <span style="color: #9876aa;">location</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> ...</span><span style="color: #cc7832;"></span><span style="color: #cc7832;"></span></pre><p> </p><p>But sadly for me creating a repository out for the above <b>@Document</b> and saving objects was keep on converting the <b>GeoPoint</b> field to a plain map of latitude and longitude but not as a geo_point field. </p><pre><code> "location": {
"properties": {
"latitude": {
"type": "string"
},
"longitude": {
"type": "string"
}
}
}</code> </pre><p>This results in following error while querying using Geo Distance Query. </p><p></p><p><b>QueryParsingException failed to find geo_point field</b></p><p>After some research I finally found out that in such cases we would need to manually create the index before calling <b>Repository.save.</b></p><p>In a service or any other initializer class you need to do the following;</p><p></p><pre style="background-color: #2b2b2b; color: #a9b7c6; font-family: "JetBrains Mono", monospace; font-size: 9.8pt;"><span style="color: #cc7832;">@Autowired</span><span style="color: #cc7832;"><br /></span><span style="color: #cc7832;">private </span>ElasticsearchRestTemplate <span style="color: #9876aa;">elasticsearchRestTemplate</span><span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"><br /></span><span style="color: #bbb529;">@PostConstruct<br /></span><span style="color: #cc7832;">public void </span><span style="color: #ffc66d;">init</span>() {<br /> IndexOperations indexOperations = <span style="color: #9876aa;">elasticsearchRestTemplate</span>.indexOps(Property.<span style="color: #cc7832;">class</span>)<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>indexOperations.putMapping(indexOperations.createMapping())<span style="color: #cc7832;">;<br /></span><span style="color: #cc7832;"> </span>indexOperations.refresh()<span style="color: #cc7832;">;<br /></span>}</pre><p><b> </b>This fixes the issue and creates the location field with type geo_point as expected.<br /></p>shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-40234102318323065562020-03-22T01:21:00.001-07:002020-03-22T06:50:17.967-07:00Corona Meter<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
Corona Meter</h1>
<img alt="" height="225" src="https://techtalks.lk/assets/images/posts/1584864315" width="400" xss="removed" /><br />
<br />
Corona (Covid19) has taken over the world and it also has made its way to my home country Sri Lanka. We are in a curfew till Tuesday (24/03/2020) and it is expected to extended. Our security forces, medical staff are doing there best but as of writing this the no of cases reported hasn't stopped completely and also it hasn't blown out of control either. So we can say we are safe for now as long as we maintain social distancing and good personal hygiene.<br />
<br />
<img alt="" height="241" src="https://techtalks.lk/assets/images/posts/1584864340" width="400" xss="removed" /><br />
<br />
In a time like this, it is particularly important to keep track of the Corona virus outbreak situation. So to ease thing I made a small DIY project which uses an API provided by the official Sri Lankan Health Promotion Bureau to show vital statistics. This is a need of the hour as it makes easy for myself and my wife to keep track of things easily. This is a self contained IOT project which only requires a power source or battery which can input 5v. It will connect to WiFi router and pull data. It can be easily modified to pull and display any country's data with minor modifications so it will be useful for many.<br />
<br />
More information on how this was developed can be found at <a href="https://github.com/shazin/corona-meter">https://github.com/shazin/corona-meter</a><br />
<br />
<iframe allowfullscreen="" frameborder="0" height="240" src="https://www.youtube.com/embed/ztibeK-gi4Y" width="520"></iframe></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-45419631746757520872018-10-30T21:32:00.004-07:002018-10-30T21:33:15.340-07:00Spring Security Authentication<div dir="ltr" style="text-align: left;" trbidi="on">
<h2>
Spring Security Authentication</h2>
<em>Security is one of the most vital concerns for any organization. In this article, you will learn about authentication and how to integrate them easily with the Spring MVC application.</em><br />
<h2>
Authentication</h2>
One of the fundamental ways to secure a resource is to make sure that the caller is who they claim to be. This process of checking credentials and making sure that they are genuine is called authentication.<br />
This article will delve into the technical capabilities of Spring Security, specifically authentication. To find the complete code for this article, go to <a href="https://github.com/PacktPublishing/Hands-On-Spring-Security-5-for-Reactive-Applications/tree/master/Chapter02">https://github.com/PacktPublishing/Hands-On-Spring-Security-5-for-Reactive-Applications/tree/master/Chapter02</a>.<br />
The following diagram shows the fundamental process Spring Security uses to address this core security requirement. The figure is generic and can be used to explain all the various authentication methods that the framework supports:<br />
<img alt="" height="268" src="https://www.techtalks.lk/assets/images/posts/1540960050" width="400" xss="removed" /><br />
<br />
<div style="text-align: center;">
Authentication architecture</div>
<br />
Spring Security has a series of servlet filters (a filter chain). When a request reaches the server, it is intercepted by this series of filters (<strong>Step 1</strong> in the preceding diagram).In the reactive world (with the new Spring WebFlux web application framework), filters are written quite differently fromtraditional filters (such as those used in the Spring MVC web application framework). Having said that, the fundamental mechanism remains the same for both.<br />
The Servlet filter code execution in the filter chain keeps skipping until the right filter is reached. Once it reaches the right authentication filter based on the authentication mechanism used, it extracts the supplied credentials (most commonly a username and password) from the caller.<br />
Using the supplied values (here, you have a username and password), the filter(UsernamePasswordAuthenticationFilter) creates an Authentication object (in the preceding diagram, UsernamePasswordAuthenticationToken is created withthe username and password supplied in Step 2). The Authentication object created in Step 2 is then used to call the authenticate method in the AuthenticationManager interface:<br />
<pre><code class="language-java">public interface AuthenticationManager {
Authentication authenticate(Authentication authentication)
throwsAuthenticationException;
}</code></pre>
The actual implementation is provided by <strong>ProviderManager</strong>, which has a list of configured AuthenticationProvider.<br />
<pre><code class="language-java">public interface AuthenticationProvider {
Authentication authenticate(Authentication authentication)
throwsAuthenticationException;
boolean supports(Class<!--?--> authentication);
}</code></pre>
The request passes through various providers and, in due course, tries to authenticate the request. There are a number of AuthenticationProvider interfaces as part of Spring Security.<br />
In the diagram above, AuthenticationProvider requires user details (some providers require this, but some don't), which are provided in UserDetailsService:<br />
<pre><code class="language-java">public interface UserDetailsService {
UserDetailsloadUserByUsername(String username) throws UsernameNotFoundException;
}</code></pre>
UserDetailsService retrieves UserDetails (and implements the User interface) using the supplied username.<br />
If all goes well, Spring Security creates a fully populated Authentication object (authenticate: true, granted authority list, and username), which will contain various necessary details. The Authentication object is stored in the SecurityContext object by the filter for future use.<br />
The authenticate method in AuthenticationManager can return the following:<br />
<ul>
<li>
An Authentication object with authenticated=true, if Spring Security can validate the supplied usercredentials<br />
</li>
<li>
An AuthenticationException, if Spring Security finds that the supplied user credentials are invalid<br />
</li>
<li>
null, if Spring Security cannot decide whether it is true or false (confused state)<br />
</li>
</ul>
<h2>
Setting up AuthenticationManager</h2>
There are a number of built-in AuthenticationManager in Spring Security that can be easily used in your application. Spring Security also has a number of helper classes, using which you can set up AuthenticationManager. One helper class is AuthenticationManagerBuilder.<br />
Using this class, it’s quite easy to set up UserDetailsService against a database, in memory, in LDAP, and so on. If the need arises, you could also have your own custom UserDetailsService (maybe a custom single sign-on solution is already there in your organization).<br />
You can make an AuthenticationManager global, so it will be accessible by your entire application. It will be available for method security and other WebSecurityConfigurerAdapter instances.<br />
WebSecurityConfigurerAdapter is a class that is extended by your Spring configuration file, making it quite easy to bring Spring Security into your Spring application. This is how you set up a global AuthenticationManager using the @Autowired annotation:<br />
<pre><code class="language-java">@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void confGlobalAuthManager(AuthenticationManagerBuilderauth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("admin@password")
.roles("ROLE_ADMIN");
}
}</code></pre>
You can also create local AuthenticationManager, which is only available for this particular WebSecurityConfigurerAdapter, by overriding the configure method, as shown in the following code:<br />
<pre><code class="language-java">@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilderauth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("admin@password")
.roles("ROLE_ADMIN");
}
}</code></pre>
Another option is to expose the AuthenticationManager bean by overriding the authenticationManagerBean method:<br />
<pre><code class="language-java">@Override
publicAuthenticationManagerauthenticationManagerBean() throws Exception {
returnsuper.authenticationManagerBean();
}</code></pre>
You can also expose various AuthenticationManager, AuthenticationProvider, or UserDetailsService as beans, which will override the default ones.<br />
The preceding code example has used AuthenticationManagerBuilder to configure in-memory authentication.<br />
<h2>
AuthenticationProvider</h2>
AuthenticationProvider provides a mechanism for getting the user details with which authentication can be performed. Spring Security provides a number of AuthenticationProvider implementations, as shown in the following diagram:<br />
<img alt="" height="320" src="https://www.techtalks.lk/assets/images/posts/1540960156" width="640" xss="removed" /><br />
<br />
<div style="text-align: center;">
Spring Security built-in AuthenticationProvider</div>
<h2>
Custom AuthenticationProvider</h2>
You can also write a custom AuthenticationProvider by implementing the AuthenticationProvider interface. You have to implement two methods, namely authenticate (Authentication) and supports(Class<!--?-->aClass):<br />
<pre><code class="language-java">@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException
{
String username = authentication.getName();
String password = authentication.getCredentials().toString();
if ("user".equals(username) && "password".equals(password)) {
return new UsernamePasswordAuthenticationToken(username, password, Collections.emptyList());
} else {
throw new BadCredentialsException("Authentication failed");
}
}
@Override
publicboolean supports(Class<!--?-->aClass) {
returna Class.equals(UsernamePasswordAuthenticationToken.class);
}
}</code></pre>
<br />
Your authenticate method is quite simple. Compare the username and password with a static value. You can write any logic here and authenticate the user. If there is an error, it throws an exception, AuthenticationException.<br />
On the GitHub page, navigate to the jetty-in-memory-basic-custom-authentication project to see the full source code of this class.<br />
<h2>
Multiple AuthenticationProvider</h2>
Spring Security allows you to declare multiple AuthenticationProvider in your application. They are executedaccording to the order in which they are declared in the configuration.<br />
The jetty-in-memory-basic-custom-authentication project is modified further, and you have used the newly created CustomAuthenticationProvider as an AuthenticationProvider (Order 1) and the existing inMemoryAuthentication as your second AuthenticationProvider (Order 2):<br />
<pre><code class="language-java">@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomAuthenticationProvider.class)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/**")
.authenticated(); // Use Basic authentication
}
@Override
protected void configure(AuthenticationManagerBuilderauth) throws Exception {
// Custom authentication provider - Order 1
auth.authenticationProvider(customAuthenticationProvider);
// Built-in authentication provider - Order 2
auth.inMemoryAuthentication()
.withUser("admin")
.password("{noop}admin@password")
//{noop} makes sure that the password encoder doesn't do anything
.roles("ADMIN") // Role of the user
.and()
.withUser("user")
.password("{noop}user@password")
.credentialsExpired(true)
.accountExpired(true)
.accountLocked(true)
.roles("USER");
}
}</code></pre>
Whenever the authenticate method executes without error, the controls return, and, thereafter, the configured AuthenticationProvider doesn't get executed.<br />
<br />
<em>If you found this article interesting, you can explore <a href="https://www.amazon.com/Hands-Spring-Security-Reactive-Applications/dp/178899597X">Hands-On Spring Security 5 for Reactive Applications</a> to secure your Java applications by integrating the Spring Security framework in your code. <a href="https://www.packtpub.com/application-development/hands-spring-security-5-reactive-applications">Hands-On Spring Security 5 for Reactive Applications</a></em><br />
<em>will guide you in integrating add-ons that will add value to any Spring Security module.</em></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-66710756490822553092018-02-18T00:04:00.000-08:002018-02-18T00:12:02.763-08:00Using ThingsBoard, MQTT, Arduino to Monitor Plant Soil Moisture, Temperature and Humidity - Part 1<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Problem</b><br />
<br />
Recently I got a rose plant from one of my friends who got married. I have been having plants of all sorts at my home but as any busy guy I forget to water the plants. This is a problem I thought I need to address using technology.<br />
<br />
<b>Solution</b><br />
<b><br /></b>
After doing some research I got to know about a Platform called ThingsBoard which allows Devices to connect to a central server using HTTP, MQTT, CoAP protocols and send telemetry to visualize everything in a Dashboard. I quickly downloaded it, used my Ethernet Shield, DHT11 sensor and YL-69 Soil Moisture sensor which I had lying around to check the possibility of integrating everything together.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPQSnR3bRF0pt6WSco77wAkEWMgFAtyhDjzQIC-PLofbMWY9aOCBxdrTmc10MpiUBUHyECbjkYMsqEis_XgH-bmbinnod4L9jthbxxFwABX3AuvXww7syIsUsIoQzp-CUMlWZT2BUDTaA/s1600/20180217_222415.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1200" data-original-width="1600" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPQSnR3bRF0pt6WSco77wAkEWMgFAtyhDjzQIC-PLofbMWY9aOCBxdrTmc10MpiUBUHyECbjkYMsqEis_XgH-bmbinnod4L9jthbxxFwABX3AuvXww7syIsUsIoQzp-CUMlWZT2BUDTaA/s320/20180217_222415.jpg" width="320" /></a></div>
<br />
<b>Implementation</b><br />
<b><br /></b>
I just connected YL-69 Soil Moisture sensor to Arduno Analog Pin A1 to read the sensor data and Digital Pin 7 to power it if and when required. I connected Digital Pin 6 to read the sensor data from DHT11 sensor. Finally I followed the ThingsBoard Installation guide [1] and Getting Started guide [2] to setup ThingsBoard server in my laptop.<br />
<br />
Finally wrote the following Arduino Sketch to use Ethernet Shield to connect to the network and ThingsBoard via MQTT protocol to send data.<br />
<br />
Arduino sketch can be found below;<br />
<br />
<script src="https://gist.github.com/shazin/f30f41977d801873c5b331287c2240ea.js"></script>
<br />
Data was sent in following format.
<br />
<i>{"temperature":29.00, "humidity":30.00, "moisture":10, "active": false}</i><br />
<i><br /></i>
And was visualized in Dashboard Widgets like below;<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzLVKEZEVmGW4c_zVsKI_vUOCkRCyajm8nupTg88Kn3UbUtjffc528Z4_HY72CQnXBrYWIewC8SIyrmrEOvYdAPREBq9kddhK-RJUS0JfPAvd7iU06TycTPa5mPSA1cEGgg3i1OQjUoxk/s1600/Dashboard.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="475" data-original-width="1600" height="118" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgzLVKEZEVmGW4c_zVsKI_vUOCkRCyajm8nupTg88Kn3UbUtjffc528Z4_HY72CQnXBrYWIewC8SIyrmrEOvYdAPREBq9kddhK-RJUS0JfPAvd7iU06TycTPa5mPSA1cEGgg3i1OQjUoxk/s400/Dashboard.png" width="400" /></a></div>
<br />
<br />
You can see a Demo below where the Blue Color Soil Moisture sensor reading go up and down based on inserting and removal of the YL-69 sensor probe and Temperature and Humidity going up when I exhale hot air into the DHT11 sensor.<br />
<br />
<iframe allow="autoplay; encrypted-media" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/KHJsaRTtyIU" width="560"></iframe>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<br />
<b>Future Work</b><br />
<b><br /></b>
Future work would be to add ESP8266 wireless connectivity to the entire thing and using ThingsBoard rule to fire and send an email when soil moisture level goes down below a threshold.<br />
<br />
<b>References</b><br />
<b><br /></b>
<br />
<ol style="text-align: left;">
<li><a href="https://thingsboard.io/docs/user-guide/install/windows/">https://thingsboard.io/docs/user-guide/install/windows/</a></li>
<li><a href="https://thingsboard.io/docs/getting-started-guides/helloworld/">https://thingsboard.io/docs/getting-started-guides/helloworld/</a></li>
<li><a href="https://create.arduino.cc/projecthub/nekhbet/using-the-yl-39-yl-69-soil-humidity-sensor-with-arduino-968268">https://create.arduino.cc/projecthub/nekhbet/using-the-yl-39-yl-69-soil-humidity-sensor-with-arduino-968268</a></li>
<li><a href="https://playground.arduino.cc/Main/DHT11Lib">https://playground.arduino.cc/Main/DHT11Lib</a></li>
<li><a href="https://www.techtalks.lk/blog/2018/2/thingsboard-mqtt-arduino-part-1">https://www.techtalks.lk/blog/2018/2/thingsboard-mqtt-arduino-part-1</a></li>
</ol>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-25907580443956148102018-01-30T00:57:00.002-08:002018-01-30T03:33:34.602-08:00Git Commit Information in Spring MVC<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
At time there is a need for developers the need to know which Git Commit from which Git Branch was used to build a release so that they can keep track of the versions.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
Spring Boot Actuator is tailor made for this if you are working on Spring Boot but if you are like me and not using Spring Boot for a specific project but only using Spring MVC then this guide will be helpful for you.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
First of all you need to add the spring-boot-actuator</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb3aXb-mfAlwnvabrUbnuC6a5VaWCAZRpToTPL-fyY2NXaLqdShW8IWKkASKTRfIiwe2foT1ywFsvf37BowAdpQX30HBQLGTHszKQO_AzjZ3RKKFGt_6fkkgoyOHsXNrgfZB6t4gFmWLP8/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> <dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-actuator</artifactid>
<version>Version</version>
</dependency>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
And then instead of relying on Spring Boot Autoconfiguration, in your Java Annotation configuration you can do the following;</div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="language-java" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;">@Configuration
@Import({
EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
EndpointAutoConfiguration.class
})</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
And in order to generate the git.properties file you can use the Maven git-commit-id plugin and use it as below;</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb3aXb-mfAlwnvabrUbnuC6a5VaWCAZRpToTPL-fyY2NXaLqdShW8IWKkASKTRfIiwe2foT1ywFsvf37BowAdpQX30HBQLGTHszKQO_AzjZ3RKKFGt_6fkkgoyOHsXNrgfZB6t4gFmWLP8/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"> <build>
<pluginmanagement>
<plugins>
<plugin>
<groupid>pl.project13.maven</groupid>
<artifactid>git-commit-id-plugin</artifactid>
<version>2.2.4</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>validate-the-git-infos</id>
<goals>
<goal>validateRevision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotgitdirectory>${project.basedir}/../.git</dotgitdirectory>
<dateformat>yyyy-MM-dd'T'HH:mm:ssZ</dateformat>
<dateformattimezone>${user.timezone}</dateformattimezone>
<generategitpropertiesfile>true</generategitpropertiesfile>
<generategitpropertiesfilename>${project.build.outputDirectory}/git.properties</generategitpropertiesfilename>
<format>properties</format>
<skippoms>true</skippoms>
<injectallreactorprojects>false</injectallreactorprojects>
<failonnogitdirectory>true</failonnogitdirectory>
<failonunabletoextractrepoinfo>true</failonunabletoextractrepoinfo>
<commitidgenerationmode>flat</commitidgenerationmode>
<gitdescribe>
<skip>false</skip>
<always>true</always>
<dirty>-dirty</dirty>
<match>*</match>
<tags>false</tags>
<forcelongformat>false</forcelongformat>
</gitdescribe>
<validationproperties>
<validationproperty>
<name>validating project version</name>
<value>${project.version}</value>
<shouldmatchto>&lt;![CDATA[^.*(?&lt;!-SNAPSHOT)$]]&gt;</shouldmatchto>
</validationproperty>
</validationproperties>
</configuration>
</plugin>
</plugins>
</pluginmanagement>
</build>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
And when packaging you can use the following command</div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="language-bash" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;">$ mvn clean package git-commit-id:revision </code></pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
Now when you access <span style="box-sizing: border-box; font-weight: 700;">http://<host>:<port>/<app>/info</app></port></host></span> endpoint you will see the git commit id, branch name, commit time etc. and you can customize the pom.xml git-commit-id plugin configuration to have more information.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
Source: <a href="https://www.techtalks.lk/blog/git-commit-information-in-spring-mvc">https://www.techtalks.lk/blog/git-commit-information-in-spring-mvc</a></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-8233412719423778232017-12-26T19:24:00.002-08:002017-12-26T19:24:27.643-08:00Java 9 Migration Tips<div dir="ltr" style="text-align: left;" trbidi="on">
<img alt="" data-cke-saved-src="https://www.techtalks.lk/assets/images/posts/1514287549" src="https://www.techtalks.lk/assets/images/posts/1514287549" /><br />
Java 9 was released this year and it is a much awaited release of Java with a lot of interesting and exciting features. But Upgrading is a concern for any CTO or Infrastructure Team as it may break the existing functionality or create unforeseen issues.<br />
<br />
But in a recent presentation, Bernard Traversat who is the Vice President of Development for the Java SE Platform at Oracle provided some tips on successful migration to Java 9. Bernard manages the development team responsible for the Java Language, JVM, Core libraries, Java UI toolkits, and Java deployment stack for Oracle JDK product. So who else is fit enough provide advise on Java 9 Migration than him.<br />
<h2>
Why Upgrading is required;</h2>
<ul>
<li>Upgrades include Security fixes </li>
<li>Upgrades include Regressions fixes </li>
<li>New features with releases</li>
<li>More performance </li>
<li>More robustness </li>
<li>Because the top or bottom tier was upgraded (OS’s, Hardware) </li>
<li>Reduce cost of operation via uniformity</li>
</ul>
<h2>
Why Upgrading is delayed;</h2>
<ul>
<li>Fear of something is going to go wrong.</li>
<li>It will cost money if something goes wrong. According InformationWeek IT Downtime Costs $26.5 Billion in Revenue.</li>
<li>It requires extra work which costs time and money.</li>
</ul>
Out of Programming Languages Java, Golang, Python, C# .NET, As per Bernard Only Java is Backward compatible and upgradable.<br />
<br />There are binary, source and runtime compability explicit managed in the Java platform.<br />
Most of the APIs (java.*, java.*) undergo millions of tests to ensure compatibility and have specifications managed under the JCP (Java Community Process).<br />In order to update a specification of an API, rigorous JCP process is required.<br />
<h1>
Migration</h1>
<h2>
The following things remain the same from Java x to Java 9</h2>
<ul>
<li>The class path </li>
<li>Class loading </li>
<li>Not forced to migrate to modules</li>
<li>sun.misc.Unsafe works</li>
<li>Most existing code should work</li>
<li>The IDEs, Maven, etc. all have support for JDK 9 already</li>
</ul>
<h2>
Things to look out for when migrating from Java x to Java 9</h2>
<ul>
<li>You will have to upgrade the libraries and tools that you use </li>
<li>Build or deployment needs to be adjusted If you use any of the components that are shared between Java SE and Java EE </li>
<li>A small number of supported APIs have been removed </li>
<li>A number of non-API features and tools have been removed </li>
<li>Libraries that you use may show some warnings</li>
</ul>
<h2>
Incompatibility issues you will face during or after migration;</h2>
No 1 issue will be a Third Party library you use in your older Java code is not compatible with Java 9 anymore. This is addressed as many open source projects and vendors are already changing their code base to Java 9 but in case one hasn't done already until they do your migration will have issues.<br />
<h2>
Apart from that following can also affect you and you must be aware;</h2>
1. JEP-260 Encapsulate most internal APIs - This means Internal APIs will no longer be accessible to developers. If your code uses an internal API then it needs to change.<br />
<br />
2. JEP-223 New Version String Scheme - JDK 1.9.0_25 will turn into JDK 9.1.3. If your code contains any version string checking then it has to change.<br />
<br />
3. JEP-220 Modular Runtime Images - No more rt.jar and JDK directory structure will change. If your code has any JDK directory checking then it needs to change.<br />
<br />
4. Most sun.misc.* and sun.reflect.* are removed - If your code uses any of these packages then it needs to change. Ex:- you can no longer use sun.misc.Base64 class.<br />
<br />
5. JEP-214 Remove Java 8 Deprecated GC Combinations - Following GC Combinations will now produce errors. You need to change your Java start up scripts if you use any.<br />
<br />
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="language-java" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;">DefNew + CMS : -XX:-UseParNewGC -XX:+UseConcMarkSweepGC
ParNew + SerialOld : -XX:+UseParNewGC
ParNew + iCMS : -Xincgc
ParNew + iCMS : -XX:+CMSIncrementalMode -XX:+UseConcMarkSweepGC
DefNew + iCMS : -XX:+CMSIncrementalMode -XX:+UseConcMarkSweepGC -XX:-UseParNewGC
CMS foreground : -XX:+UseCMSCompactAtFullCollection
CMS foreground : -XX:+CMSFullGCsBeforeCompaction
CMS foreground : -XX:+UseCMSCollectionPassing</code></pre>
<br />
6. Launching JVM with Unrecognized VM Options will result in failure to start<br />
<br />
7. java.awt.peer and java.awt.dnd.peer packages will be hidden - If your code uses these it needs to be changed to use supporting APIs<br />
<br />
8. Calling Thread.stop(Throwable) method will throw an Exception - This was deprecated for a long time now.<br />
<br />
9. JEP-271 Unified GC Logging - Re-implemented GC Logging with the new JVM Logging framework JEP-158, If your Java start up scripts use any of the following they need to change.<br />
<br />
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;"><code class="language-java" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;">-XX:+PrintGC will be changed to -Xlog:gc
-XX:+PrintGCDetails will be changed to -Xlog:gc*
-xloggc: will be changed to -Xlog:gc:</code></pre>
<br />
10. JEP-248 G1 is the new Garbage Collector - Serial GC is and will remain the default garbage collector for Windows 32 bit.<br />
<h2>
Conclusion</h2>
Migration to Java 9 is not as scary as it looks and will be easy with proper planning. Inorder to harness the full features it is advised to migrate<br />in small increments so that issues arising can also be mitigated easily.<br />
<h2>
References</h2>
<a href="https://www.techtalks.lk/blog/java-9-migration-tips">https://www.techtalks.lk/blog/java-9-migration-tips</a><br />
<br /></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-67848863565005336822017-12-08T21:20:00.002-08:002017-12-09T07:55:45.547-08:00Writing Reactive Repositories for Spring Data with Mongodb<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<h2>
1. <strong>Overview</strong></h2>
Reactive Programming has been alive for sometime now. Programming frameworks like Akka, Reactive Streams, Reactor, RxJava etc are good examples. In simple terms reactive programming is about writing non blocking software that are asynchronous and event driven.<br />
Reactive Programming requires a small number of threads to scale vertically (Scale up inside a single JVM) instead of horizontally (Scale out to different nodes by means of clustering).<br />
With Spring 5.0 there is out of the box support for Reactive Programming and now Spring Data project also has Reactive support. Now we will be looking at those latest features here in detail:<br />
<h2>
2. <strong>Setup</strong></h2>
In order to use Spring Data Reactive Repositories we need to include spring-boot-starter-data-mongodb-reactive, de.flapdoodle.embed.mongo (for testing), rxjava and rxjava-reactive-streams. Plus reactive mongo-db driver is needed to make full use of the reactive capabilities. The maven dependencies will look like below:<br />
<script src="https://gist.github.com/shazin/2e222fc87b1726adae6163a67d09e3da.js"></script>
<br />
<div class="sectionbody">
<div class="exampleblock">
<div class="content">
<div class="listingblock">
Complete pom.xml file can be found at the Github repository listed at the conclusion section.<br />
<h2 class="content">
3. <strong>Enabling Spring Data Reactive Repositories for Mongodb</strong></h2>
<div>
As the title suggests we will have a look the Spring Data Reactive Repositories with Mongodb. The new <em>@EnableReactiveMongoRepositories<strong> </strong></em>is introduced to enable Reactive Repository support for Mongodb. The following configuration enables Spring Data Reactive Repositories for Mongodb:</div>
<div>
<br /></div>
</div>
<div>
<script src="https://gist.github.com/shazin/2d59f71beb0c413708c49d1440da0c36.js"></script>
</div>
</div>
<h2 id="reactive-repositories">
4. <strong>Reactive Repositories</strong></h2>
<div class="sectionbody">
<div class="paragraph">
Spring Data project uses the repositories programming model which is the most high-level abstraction to deal with data. They’re comprised of a set of CRUD methods defined in a Spring Data provided interface and domain-specific query methods.<br />
Mainly by using an interface named CrudRepository which exposes methods like findOne, delete, save. With Reactive Programming support Spring Data project has now introduced two more interfaces named <em>ReactiveCrudRepository</em> and <em>RxJava2CrudRepository<strong> </strong></em>(for RxJava project support)</div>
A typical Spring Data Reactive Repository would look like below:<br />
<br />
<script src="https://gist.github.com/shazin/e680fd6985b659d9f2871154aefd243e.js"></script>
</div>
And a RxJava2 version of the same Repository would look like below:<br />
<br />
<script src="https://gist.github.com/shazin/831718ff08e5a58cb3d331ea8c6d2d77.js"></script>
<br />
<div class="content">
<div class="listingblock">
<div class="content">
Note that Spring 5.0 Reactor Project specific <em>Flux</em> is returned in <em>ReactiveTaxiRepository </em>and RxJava project specific <em>Flowable</em> is returned in <em>RxJava2TaxiRepository</em>.</div>
These repositories are really identical to standard Spring Data Repositories except for the fact that now they can return and/or accept as parameters, reactive elements such as <em>Flux</em>, <em>Mono</em> and <em>Flowable</em>. By default, reactive repositories use <a href="https://projectreactor.io/">Project Reactor</a> types but other reactive libraries can also be used such as RxJava2 as shown above.</div>
</div>
</div>
</div>
<h2 class="content">
5. <strong>Using Spring Data Reactive Repositories for Mongodb</strong></h2>
When using the new Spring Data Reactive Repositories, We can use the full features of Reactive Programming provided by the entities <em>Flux</em>, <em>Mono</em> or <em>Flowable</em> (RxJava2).<br />
And now we look at Reactor version would like below:<br />
<br />
<script src="https://gist.github.com/shazin/7da03b9a767c3a6aa0e0663d62dcc465.js"></script>
<br />
<div class="content">
<div class="listingblock">
And now we look at the RxJava2 version:<br />
<br />
<script src="https://gist.github.com/shazin/f31b4228cfa4c9157c3bb7ff43ed1aee.js"></script>
The above codes will find Taxis by Number CAL-4259 and collect that Flux stream or Flowable stream into a List and will block until the collection is finished.</div>
</div>
</div>
<h2 class="content">
6. <strong>Streaming Data with Tailable Cursor</strong></h2>
Spring Data Reactive Repositories provide a way to Stream Data as it arrives into Mongodb with a <em>@Tailable</em> annotation, sort of like an Event Source system. Sticking to our example of Taxis, we can Subscribe to a Tailable Stream and while being subscribed, insert <em>Taxi</em> entities into Mongodb.<br />
This will enable to see newly added <em>Taxi</em> entities coming into system in real time in a streaming manner until the subscription is disposed of. Simulating Taxis entering into a City in real time:<br />
<br />
<script src="https://gist.github.com/shazin/82059f6661edefdc12aef569e9cf709d.js"></script>
<br />
<h2>
<strong>7. Advantages</strong></h2>
Compared to a Standard Spring Data Repository, A Reactive Repository provides all the features of Reactive Programming to Data Retrieval. Just by using Reactive Repositories, we can easily filter, process, aggregate data returned declaratively and use asynchronous capabilities provided out of the box in Reactive Programming.<br />
<h2>
8. <strong>Conclusion</strong></h2>
Reactive Programming provides a lot of features such Functional, Declarative style of Coding which is being rapidly adopted by developers and enables to write scale-able, easy to understand code.<br />
Now with Spring Data Reactive Repositories these features can be easily incorporated into the existing features of Spring Data project. The complete Source code for the project can be found at <a href="https://github.com/shazin/spring-data-reactive-mongodb-tutorial">GitHub</a> .
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-29244276123994260972017-10-03T08:43:00.003-07:002017-10-03T08:43:58.736-07:00Java + Spring Boot implementation of Blockchain<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
Blockchain is the buzz word these days as it is technology used by Cryptocurrencies which is taking over the world like crazy. There are so many articles written on the Theory of Blockchain like <a href="https://www.techtalks.lk/blog/blockchain-in-11-mins">this one</a> but there are very little implementations. But recently I stumbled upon an article by <a href="https://hackernoon.com/learn-blockchains-by-building-one-117428612f46">Daniel van Flymen</a> who happens to have written a Python based implementation of the Blockchain.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
According to him and I am pretty sure many Software Engineers would agree that the best way to learn something is to actually implement it so that you learn more than you get to learn while reading theory documentation.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
So this inspired me to write Java + Spring Boot based Implementation of Daniel's work so that I can learn the Blockchain workings. For everyone who is interested following is a basic version of how a Blockchain will look like in the wild.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
<script src="https://gist.github.com/shazin/e39b178c9fc11295557dfc61e416f705.js"></script>
So Daniel talks a lot about the details of the Blockchain which you can read but in short following are the Steps required to write a Blockchain implementation.<br />
<br />
<br />
<ol style="text-align: left;">
<li>Building a Blockchain</li>
<li>Building an API to access the Blockchain</li>
<li>Interacting with the Blockchain </li>
<li>Consensus</li>
</ol>
<div>
You can see all above Steps implemented in Java + Spring Boot at <a href="https://github.com/shazin/block-chain">https://github.com/shazin/block-chain</a>. </div>
<div>
<br /></div>
<div>
<br /></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-55663280615923065072017-09-18T04:36:00.001-07:002017-09-18T06:49:30.800-07:00Server Sent Event Processing in Spring MVC 4.2 <div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
In a previous <a href="http://shazsterblog.blogspot.com/2016/02/asynchronous-streaming-request.html">post</a> I spoke about the features Spring MVC 4.2 has from Streaming Request Processing. I have discussed how we can <b>StreamingResponseBody </b>to send large data asynchronously in a streaming manner. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
In this post I am planning to talk about <b>SseEmitter</b> which is another way to send semi structured data clients in an asynchronous streaming manner.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
If you want to learn more about the definition and structure of a Server Sent Event you can read the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events">Mozilla Documentation</a>. But in short a Server Sent Event is a push event from a server which will be taking place inside of a single TCP/IP Socket connection from Client to Server. This event being push means it eliminates the need for constant polling of the Server for data thus reduces unwanted load on Server and the Client can receive the data in real time.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
In Spring MVC following controller code can be written to send server sent events easily and in the client end Javascript can be used to read these events and present in the web page. For demo purpose I am showing a live Cricket match score and commentary coming down to a Web page client in real time. The server code will look like below; </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
<script src="https://gist.github.com/anonymous/4af29c14d84f425bf51eb338791d8f74.js"></script>
<br />
<div style="text-align: justify;">
And Javascript client client code will look like below;<br />
<br />
<br />
<script src="https://gist.github.com/anonymous/3cbc43d44abfcf1b51c3d408327dbce7.js"></script>
</div>
And as you can see from the image below there is only one request being initiated to /score end point but multiple commentary data being received through that long lived connection.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUrEAh_RX6slTNYZA2MzKDmeRmdAzeLTWN6ykUo7mHqBwumekv3TS1REj4Q4hW4frSUHdXJCngflW2kWlWf_YC5ENJDtxT_O7cdlj7AGs49HgTR2HhRNpUtLPiQibMnz48345Oij9EZaU/s1600/SseEvent.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="618" data-original-width="1600" height="153" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUrEAh_RX6slTNYZA2MzKDmeRmdAzeLTWN6ykUo7mHqBwumekv3TS1REj4Q4hW4frSUHdXJCngflW2kWlWf_YC5ENJDtxT_O7cdlj7AGs49HgTR2HhRNpUtLPiQibMnz48345Oij9EZaU/s400/SseEvent.png" width="400" /></a></div>
<br /></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-19230440971416719682017-09-09T22:29:00.001-07:002017-09-09T22:29:25.130-07:00Spring Web Flux - The Non Blocking Asynchronous Functional Reactive Web Framework - 2<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">
Spring Web Flux Framework can be used with its latest Release Candidate Release 5.0.0.RC3 if you want to try it out. You just need to add the Spring Milestone Repository in your gradle or maven file.<br />
<br />
<br /></div>
<script src="https://gist.github.com/shazin/5a9444dc13a978edbd87d831c4be1590.js"></script>
I have written the following service which emulates a delay of maximum 1000 milliseconds in a service to test how the conventional Spring Web MVC vs Spring Web Flux work.<br />
<br />
<br /></div>
<script src="https://gist.github.com/shazin/9594355673f74fbc4c9e18302a7ae09c.js"></script>
And the following controller implementations one with a Blocking conventional Spring Web MVC Controller method which returns a list to get people and a Non Blocking Spring Web Flux Controller method which returns a <b>Flux<list erson=""><list eople="">> </list></list></b>which is a defferred result and processed differently.<br />
<br />
<br /></div>
<script src="https://gist.github.com/shazin/5db4242b81640187fb8dccc48d3d0354.js"></script>
And Tested sending 1000 concurrent requests to both methods using a Gatling test. The Application was running inside of a Tomcat Container.<br />
<br />
Spring Web MVC results were<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTStt_r14VY1t34oDicL2B8UtwKqqx0p1pK3mQM5ww9IjiRd6whHFLSBhLSniguW1TbPYpTQkp6F9E_MsB6lRU_jzEM_zWwHq8GUQ1DjOHPKXPLNIHYeSZFjxWipLQCI1ymPvLoaM8VM0/s1600/SpringMvc.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="623" data-original-width="924" height="215" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTStt_r14VY1t34oDicL2B8UtwKqqx0p1pK3mQM5ww9IjiRd6whHFLSBhLSniguW1TbPYpTQkp6F9E_MsB6lRU_jzEM_zWwHq8GUQ1DjOHPKXPLNIHYeSZFjxWipLQCI1ymPvLoaM8VM0/s320/SpringMvc.png" width="320" /></a></div>
<br />
And Spring Web Flux results were<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiShjb86cztIC4SUMvd2pSBrl7KT1WcN3A0r8vRGuFzZzvrec9PpgD0LDqu0QhVmpVitsYWA4PZNWmmICYiPI-XDLy149Hdalf9HVQ61RweAW96C44gdUssZP7GbTsfOZAbTr9FQTMC5DM/s1600/SpringFlux.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="597" data-original-width="916" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiShjb86cztIC4SUMvd2pSBrl7KT1WcN3A0r8vRGuFzZzvrec9PpgD0LDqu0QhVmpVitsYWA4PZNWmmICYiPI-XDLy149Hdalf9HVQ61RweAW96C44gdUssZP7GbTsfOZAbTr9FQTMC5DM/s320/SpringFlux.png" width="320" /></a></div>
<br />
If you compare the 99th Percentile, Max and Mean response times of the both results you can see Spring Web Flux is considerably faster without any code changes to improve performance. This is a promising sign. The numbers can be improved even more if we use the Netty Reactive Servers instead of conventional Tomcat servers in my understanding.<br />
<br />
<br />
<br /></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-37050832091796831712017-09-09T01:14:00.001-07:002017-09-09T07:51:16.805-07:00Spring Web Flux - The Non Blocking Asynchronous Functional Reactive Web Framework - 1<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
Spring has a very diverse eco system of projects from Batch Jobs to Web Security. The best part of Spring project is that they are always ahead of time and do innovative work which sometimes are adopted by the Java Platform itself. For Example the <b>@Autowired</b> Annotation introduced by Spring was later standardized in JSR 330 as <b>@Inject</b>. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Spring 5 is something they are working on right now (Have released 5.0.0 Release Candidate 3 Version as of writing this post) and has a lot of innovative features. Following are a list of those features;</div>
<ul style="text-align: left;">
<li style="text-align: justify;">Reactive programming: introducing our Spring WebFlux framework built on Reactor 3.1, with support for RxJava 1.3 & 2.1 and running on Tomcat, Jetty, Netty or Undertow.</li>
<li style="text-align: justify;">Functional style with Java 8 & Kotlin: several API refinements and Kotlin extensions across the framework, in particular for bean registration and functional web endpoints.</li>
<li style="text-align: justify;">Integration with Java EE 8 APIs: support for Servlet 4.0, Bean Validation 2.0, JPA 2.2, as well as the JSON Binding API (as an alternative to Jackson/Gson in Spring MVC).</li>
<li style="text-align: justify;">Ready for JDK 9: fully aligned with JDK 9 at runtime, on the classpath as well as the module path (on the latter: as filename-based “automatic modules” for the time being).</li>
</ul>
<div style="text-align: justify;">
One of most interesting is the introduction of Spring Web Flux - A Non Blocking Functional Reactive Web Framework. Reactive Programming has been alive for sometime in form of RxJava project and many more programming frameworks like Akka, NodeJS etc.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
In simple terms reactive programming is about writing non blocking software that are asynchronous and event driven which require a small number of threads to scale vertically (Scale up inside a single JVM) instead of horizontally (Scale out to different nodes by means of clustering). </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
If we take a look at the Spring Web MVC framework which was a Synchronous Blocking Framework based on Servlet (Prior to Servlet 3.x). Every Request Reaching a Spring Web MVC Controller would use the Request Thread that came a long with the Client Request to cater that request and will be blocking, Which meant that the scale-ability was bound to the maximum number of Request Threads a Web Container had. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
This was partially solved by the introduction of Servlet 3.x with asynchronous servlets where now a Request Thread would delegate the task of catering the request to a Separate Thread. Spring Web MVC framework allowed to return a <b>DefferedResult </b>from a Controller method which meant that the Request thread will not be blocking until the processing of the request by the Controller method.</div>
<div style="text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSDP07F81N6So6Ggis-ljYFW_IE30e_WIvWsUYKD1IgCGQZ22mwale8BvT4w4-0atbUiGRTdXt1goBMX6J0l4MamGgtfCU_HpWoo9JdagUjipYHRq5UjqCgQgJudLk-ualM99BruWYwDk/s1600/webflux-overview.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="645" data-original-width="1264" height="163" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSDP07F81N6So6Ggis-ljYFW_IE30e_WIvWsUYKD1IgCGQZ22mwale8BvT4w4-0atbUiGRTdXt1goBMX6J0l4MamGgtfCU_HpWoo9JdagUjipYHRq5UjqCgQgJudLk-ualM99BruWYwDk/s320/webflux-overview.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
Spring Web Flux uses a completely new approach of using Reactive Streams instead of the Servlet API. A key aspect of reactive is that it won't overwhelm the consumers when producers produce at a rate which is faster than the consumers can consume. This concept is called back pressure.</div>
<div class="separator" style="clear: both; text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjabLS2v-Z2ax8dRtT-EsF_FWUjxK_iBIA1mI0q0z-ePFp1kpFycjxK432qx6UrmiP4dRFxaeeYN9I33em0843KYd6dnwtiJn22GhuF5t2CmnoKX1cGRk_GQDt91hpDlgcyi_73eNXnYpE/s1600/iStock_000046115530_Small-1260x840.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="840" data-original-width="1260" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjabLS2v-Z2ax8dRtT-EsF_FWUjxK_iBIA1mI0q0z-ePFp1kpFycjxK432qx6UrmiP4dRFxaeeYN9I33em0843KYd6dnwtiJn22GhuF5t2CmnoKX1cGRk_GQDt91hpDlgcyi_73eNXnYpE/s320/iStock_000046115530_Small-1260x840.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<i>Reactive Programming will not let this happen (Image Courtesy : iStock)</i></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
<b>References</b></div>
<div class="separator" style="clear: both; text-align: justify;">
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: justify;">
</div>
<ol>
<li><a href="https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html">https://docs.spring.io/spring-framework/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html</a></li>
<li><a href="https://spring.io/blog/2017/05/08/spring-framework-5-0-goes-rc1">https://spring.io/blog/2017/05/08/spring-framework-5-0-goes-rc1</a></li>
</ol>
<br /></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-51851134168637995532017-08-28T03:42:00.001-07:002017-08-30T04:59:04.331-07:00Java 9 : The Future 3<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Why is a module path better than a class path? </b><br />
<br />
<b>Class path </b><br />
<br />
<ul style="text-align: left;">
<li>Class path can get in trouble when multiple classpath elements contain the same package </li>
<li>Class path is searched (linearly) every time a new class is requested </li>
</ul>
<br />
<b>Module Path </b><br />
<br />
<ul style="text-align: left;">
<li>Modules form a partition of packages </li>
<ul>
<li>no package can be in more than one runtime module</li>
</ul>
<li>Modules perform a directed search </li>
<ul>
<li>Once the runtime finds a module,it remembers its packages </li>
<li>Never has to search for those packages again </li>
<li>Class loading becomes O(1), not O(n)</li>
</ul>
</ul>
<br />
<div>
So now instead Dependency List as with Classpath, we have a Module Dependency Graph which is Directed Graph on Dependencies. </div>
<div>
<br /></div>
<div>
Following is the Dependency Graph for java.se module which is the Entire JVM Utilities available out of the box.</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtByudn3jOpxuKBpNdRMey8dGse8-DfCUXlrmSWmzgFTn93cCP9oLcmigiTQVgqsrK4IY-s-5IOVHUU9voc8j3jlFVnRW9LUT7OUTVfqmlxSX3mmsJ_co9v9K1_PgE7RwmFXuiXcUXlL8/s1600/DependencyGraph.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="721" data-original-width="1279" height="225" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtByudn3jOpxuKBpNdRMey8dGse8-DfCUXlrmSWmzgFTn93cCP9oLcmigiTQVgqsrK4IY-s-5IOVHUU9voc8j3jlFVnRW9LUT7OUTVfqmlxSX3mmsJ_co9v9K1_PgE7RwmFXuiXcUXlL8/s400/DependencyGraph.png" width="400" /></a></div>
<div>
<br /></div>
<div>
Breaking a single rt.jar into modules which can be defined in the graph shown above was the reason why Java 9 release took longer than other Java releases. It must have been very difficult task for the Engineering team and hats off to them.</div>
<div>
<br /></div>
<div>
<b>So what about Backward Compatibility??</b></div>
<div>
<b><br /></b></div>
<div>
<div>
All Old Codes which are not modularized (available in Classpath) will be put into a one big module called the "Unnamed Module". That module will by default</div>
<div style="text-align: left;">
<ul style="text-align: left;">
<li>Requires every other module</li>
<li>Exports / opens every package available in that module </li>
<li>Maximum compatibility with classpath behavior </li>
</ul>
</div>
<div>
This sort of behavior allows Java code to incrementally modularized when and if required. </div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div>
<br /></div>
<div>
<br /></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-61674837653690475582017-08-08T00:13:00.001-07:002017-08-08T00:13:36.654-07:00Java 9 : The Future 2<div dir="ltr" style="text-align: left;" trbidi="on">
This is a continuation of <a href="http://shazsterblog.blogspot.com/2017/08/java-9-future-1.html">Java 9 : Future 1</a> Post<br />
<br />
<div style="text-align: justify;">
Modules enforce Strong Encapsulation as we discussed in the previous post. Now a Package is not public by default to all other modules unless it is explicitly exported in the module-info.java Module Descriptor.</div>
<br />
Can Class D from Module A access Class F from Module B?<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifpotF64O2rxmBJ7wzeD6Zt99KS2gR4KyERseEPh3TwkM4SxeRimz9zQMDN9t9jzaramBut8MVMgdy2smqkTAXHsuipGnG0XAePEr_2nbBATFVNr8Ar0e7kQhSlD-vF4wMes7E5-eLzrI/s1600/Module.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="312" data-original-width="738" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifpotF64O2rxmBJ7wzeD6Zt99KS2gR4KyERseEPh3TwkM4SxeRimz9zQMDN9t9jzaramBut8MVMgdy2smqkTAXHsuipGnG0XAePEr_2nbBATFVNr8Ar0e7kQhSlD-vF4wMes7E5-eLzrI/s400/Module.png" width="400" /></a></div>
<br />
With the Module Descriptors mentioned below, yes it is possible as Module A has mentioned as it requires Module B and Module B has exports package Z.<br />
<br />
<h3 style="text-align: left;">
Strong Encapsulation</h3>
<div>
<ul style="text-align: left;">
<li>Module must require other Modules to be used in them.</li>
<li>Module exports packages to specific Modules or to all.</li>
<li>Module opens packages explicitly for reflection.</li>
<li>Accessibility is enforced by compiler, JVM and Reflection.</li>
</ul>
<h3 style="text-align: left;">
But why Modules again?</h3>
</div>
<div>
Remember packages starting with sun.* or com.* that were available in rt.jar (JVM Runtime) which were only specific for certain JVM Vendor Implementations and not all. Java programmers have been constantly warned not to use these packages as they will break Platform Independence. But up until Java 9 Modules there was no way to completely stop developers from using those packages.</div>
<div>
<br /></div>
<h4 style="text-align: left;">
Enter java.base Module</h4>
<div>
<span style="text-align: center;">Following is the Module Descriptor for java.base module. With this as you can see only certain packages are exported and com.*/sun.* packages are only available within the module.</span></div>
<div>
<span style="text-align: center;"><br /></span></div>
<div>
<span style="text-align: center;">// module-info.java</span></div>
<div>
<span style="text-align: center;">module java.base { </span></div>
<div>
<span style="text-align: center;"> exports java.lang; </span></div>
<div>
<span style="text-align: center;"> exports java.io; </span></div>
<div>
<span style="text-align: center;"> exports java.net; </span></div>
<div>
<span style="text-align: center;"> exports java.util; </span></div>
<div>
<span style="text-align: center;">}</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPSkyRhxJoTPj3dWjGsWZU8mmosUUjAYe9h2XvfEnUj7lnK4ihPUk2FuL3_HC6QDysQN4Zsokucmp_79Rl6KBTKvTrw6oKg5n5um8B99U5tvL4xImrhf6V58RU5nXCcJoPZO9pKeS-A_4/s1600/java.base.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="323" data-original-width="283" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPSkyRhxJoTPj3dWjGsWZU8mmosUUjAYe9h2XvfEnUj7lnK4ihPUk2FuL3_HC6QDysQN4Zsokucmp_79Rl6KBTKvTrw6oKg5n5um8B99U5tvL4xImrhf6V58RU5nXCcJoPZO9pKeS-A_4/s200/java.base.png" width="175" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Every Module in Java 9 implicitly will have require to java.base. Just like every class in Java will automatically import java.lang.*. </div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<h4 style="clear: both; text-align: left;">
So what does this means?</h4>
<div>
This means that now there can be a Java Application which is 100% modular with all Jar files being modules. Which means we can't use classpath in those scenarios. </div>
<h4 style="text-align: left;">
Enter Module Path</h4>
<div>
<div>
<ul style="text-align: left;">
<li>Path containing directories which contain modules</li>
<li>Modules can be exploded directories, or modular JARs (other forms too) </li>
<li>The runtime searches the module path when looking for a module.</li>
</ul>
</div>
</div>
<div>
Plus a Java Application can be an hybrid of Modular as well as Backward Compatible Application in those cases there will be both a Module Path and Class Path.</div>
<div>
<br /></div>
<div>
To be continued..</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-87637095799699984472017-08-02T01:06:00.001-07:002017-08-02T01:09:01.573-07:00Java 9 : The Future 1<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
There is much so talk and hype about Java 9 and its features. There are so many posts about Java 9 and specially on Java 9 Jigshaw or Modules. As a Java Developer for almost a decade now, I was really cautious and concerned about the new features of Java 9 and wanted to learn about it in depth. What better way than learning from the creators of Java 9 itself. Recently I got the chance to view an InfoQ video on a Presentation done by<span style="font-family: "times" , "times new roman" , serif;"> <span style="background-color: white;"><span style="color: #333333;">Karen Kinnear who is the Technical Lead for the Hotspot Java Virtual Machine Runtime team at Oracle. This lead me to realize Java 9 is far beyond Modules and the in depth nature of the Modules and the necessity for such time and resource consuming change in the Java Language Framework and Java Virtual Machine.</span></span></span></div>
<div style="text-align: justify;">
<span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;"><span style="color: #333333;"><br /></span></span></span></div>
<div style="text-align: justify;">
<span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;"><span style="color: #333333;">To start off if we look at all the features available in Java 9, we can list the followings;</span></span></span><br />
<span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;"><span style="color: #333333;"><br /></span></span></span>
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">102: Process API Updates</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">110: HTTP 2 Client</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">143: Improve Contended Locking</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">158: Unified JVM Logging</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">165: Compiler Control</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">193: Variable Handles</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">197: Segmented Code Cache</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">199: Smart Java Compilation, Phase Two</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">200: The Modular JDK</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">201: Modular Source Code</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">211: Elide Deprecation Warnings on Import Statements</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">212: Resolve Lint and Doclint Warnings</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">213: Milling Project Coin</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">214: Remove GC Combinations Deprecated in JDK 8</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">215: Tiered Attribution for javac</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">216: Process Import Statements Correctly</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">217: Annotations Pipeline 2.0</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">219: Datagram Transport Layer Security (DTLS)</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">220: Modular Run-Time Images</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">221: Simplified Doclet API</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">222: jshell: The Java Shell (Read-Eval-Print Loop)</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">223: New Version-String Scheme</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">224: HTML5 Javadoc</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">225: Javadoc Search</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">226: UTF-8 Property Files</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">227: Unicode 7.0</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">228: Add More Diagnostic Commands</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">229: Create PKCS12 Keystores by Default</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">231: Remove Launch-Time JRE Version Selection</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">232: Improve Secure Application Performance</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">233: Generate Run-Time Compiler Tests Automatically</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">235: Test Class-File Attributes Generated by javac</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">236: Parser API for Nashorn</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">237: Linux/AArch64 Port</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">238: Multi-Release JAR Files</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">240: Remove the JVM TI hprof Agent</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">241: Remove the jhat Tool</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">243: Java-Level JVM Compiler Interface</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">244: TLS Application-Layer Protocol Negotiation Extension</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">245: Validate JVM Command-Line Flag Arguments</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">246: Leverage CPU Instructions for GHASH and RSA</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">247: Compile for Older Platform Versions</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">248: Make G1 the Default Garbage Collector</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">249: OCSP Stapling for TLS</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">250: Store Interned Strings in CDS Archives</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">251: Multi-Resolution Images</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">252: Use CLDR Locale Data by Default</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">253: Prepare JavaFX UI Controls & CSS APIs for Modularization</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">254: Compact Strings</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">255: Merge Selected Xerces 2.11.0 Updates into JAXP</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">256: BeanInfo Annotations</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">257: Update JavaFX/Media to Newer Version of GStreamer</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">258: HarfBuzz Font-Layout Engine</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">259: Stack-Walking API</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">260: Encapsulate Most Internal APIs</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">261: Module System</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">262: TIFF Image I/O</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">263: HiDPI Graphics on Windows and Linux</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">264: Platform Logging API and Service</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">265: Marlin Graphics Renderer</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">266: More Concurrency Updates</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">267: Unicode 8.0</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">268: XML Catalogs</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">269: Convenience Factory Methods for Collections</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">270: Reserved Stack Areas for Critical Sections</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">271: Unified GC Logging</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">272: Platform-Specific Desktop Features</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">273: DRBG-Based SecureRandom Implementations</span><br />
<span style="color: #333333; font-family: "times" , "times new roman" , serif;">274: Enhanced Method Handles</span><br />
<span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;"><span style="color: #333333;"><br /></span></span></span></div>
<div style="text-align: justify;">
<span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;"><span style="color: #333333;"></span></span></span></div>
<div style="text-align: justify;">
Modular JDK feature is just a small part of it but is a significant one, in this part I am planning to talk about it in depth with some coding examples;</div>
<div style="text-align: justify;">
<br /></div>
<h3 style="text-align: justify;">
<b>The Why of Modular JDK?</b></h3>
<div style="text-align: justify;">
The main reason for a Modular JDK was to avoid Classpath Hell. People who have experience Deploying Java Applications in Production or Pre Production Environments would agree that creating and maintaining the Classpath for a Java Application with the focus to enable future maintenance is a Nightmare and can become a Hell very soon if not done properly. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
OSGi Framework tried to address this by creating an Engine in which Classloader Isolated Bundles can run with Strict Contracts to Export and Import Dependency. This was a good approach to solve Classpath hell but the problem was an OSGi Bundle is a Jar on Steroids (Had to have a META-INF/MANIFEST.MF Bundle Descriptor). So Normal Jar Files had to be converted to a OSGi Bundle before those can be used inside an OSGi Engine. Making OSGi Bundles of already existing millions of Jars and maintaining them through latest versions in its entirety could be classified as Hell. So OSGi didn't catch up to its fullest potential. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Plus JVM Classpath scanning was Sequential where when a Class is requested by an Application running in JVM it will scan the Classpath Jars one after the other until it finds the requested Class. This is an O(n) problem which needed to be addressed to improve performance.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The Official Java Team had to come up with something off the shelf. This is the reason <b>Why </b>Modular JDK was created with all the difficulties.</div>
<div style="text-align: justify;">
<br /></div>
<h3 style="text-align: justify;">
<b>The How of the Modular JDK?</b></h3>
<div>
Up until Java 8, there were only 2 concepts of code containers; </div>
<div>
<br /></div>
<div>
1. Package (Public to all)</div>
<div>
2. Class inside Package (default or package private, Public to all)</div>
<div>
<br /></div>
<div>
From Java 9 there are 3 concepts of code containers;</div>
<div>
<br /></div>
<div>
1. Module (A module is group of packages with a Module Descriptor)</div>
<div>
2. Package inside Module (Public to All modules which can access the module contain the package)</div>
<div>
3. Class inside Package (Public within module, Public to certain modules, Public to all modules)</div>
<h4 style="text-align: left;">
<br />How to create a Module in Java 9?</h4>
<div>
A module is a same old Java Jar file with a Module Descriptor. A Module Descriptor is a Java Source file with the name module-info.java residing in the root of the source directory. </div>
<div>
<br /></div>
<h4 style="text-align: left;">
The Structure of the Module Descriptor</h4>
<div>
<br /></div>
<div>
<script src="https://gist.github.com/shazin/755ccce7eb1f57dcc98533bfa760abf3.js"></script>
</div>
<h3 style="text-align: left;">
References</h3>
<div>
<br /></div>
<div>
1. <a href="https://www.infoq.com/presentations/java-9-modules">https://www.infoq.com/presentations/java-9-modules</a></div>
<div>
2. <a href="http://openjdk.java.net/projects/jdk9/">http://openjdk.java.net/projects/jdk9/</a></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-61000006456616659252017-07-31T23:15:00.002-07:002017-07-31T23:15:44.892-07:00Launching https://www.techtalks.lk<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
I have been quiet for sometime and didn't write much because I was busy building <a href="https://www.techtalks.lk/">https://www.techtalks.lk</a>. This is a platform for Sri Lanka IT Industry Experts and New Comers, Prospective New Comers to come together and share knowledge, expertise and best practices to improve the quality and standard of the IT Industry.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
As someone who is part of the Sri Lankan IT Industry for almost a decade now, I experienced the huge gap between what New Comers, University Students learn and what really happens in the IT Industry. Many struggle to catch up the high demands of the Sri Lankan IT Industry it terms of Technical Knowledge, Best Practices, Communication, Interview Facing etc. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Thus born Techtalks.lk which facilitates Real Technical Experts of the Sri Lanka IT Industry and New Comers, Prospective New Comers to the Sri Lankan IT Industry to come together to one platform to get things straightened out. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The launching is just the beginning and there is a long way to go but as the famous quote states, </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
"A journey of a Thousand Miles begins with a Single Step" - Lao Tzu</div>
<div style="text-align: justify;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqFwZvUWBpEf75b5fOnsw78XaGBzfBW_slHorXvxjNy2wkXrWvdQCfyAf-u8FHoIHWxFpOTfa73DvoNzsPphrKpDRb8oZTqc__hhGxxbn_8axU9A_FizvOt-CJQhNMyYBNKpIc9RnXhi4/s1600/logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="300" data-original-width="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqFwZvUWBpEf75b5fOnsw78XaGBzfBW_slHorXvxjNy2wkXrWvdQCfyAf-u8FHoIHWxFpOTfa73DvoNzsPphrKpDRb8oZTqc__hhGxxbn_8axU9A_FizvOt-CJQhNMyYBNKpIc9RnXhi4/s1600/logo.png" /></a></div>
<div style="text-align: justify;">
<br /></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-7401048921117764612017-06-12T01:35:00.006-07:002017-06-12T01:35:59.048-07:00My Visit to Belgium and Europe<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="margin-bottom: 0in; text-align: justify;">
<span style="font-size: 11pt; text-align: left;">I
was invited by my Client in Belgium (Immoweb) for an On Site visit to
their Head Quarters in Brussels, Belgium. This was a dream come true
for me as I have been wanting to visit Europe since I was a Teenager.
I was invited by the CTO as he was about to leave the Company and
There were crucial tasks which needed to be handled before he left
and which needed my physical presence in the company. During that
time I have been working for Immoweb as a Remote Employee for more
than one and a half years. The timing was really nice and early
notification of this visit from my client enabled me to plan my trip
and make some travel arrangements to visit some neighboring countries
of Belgium also. I flew to Belgium via transit at Doha, Qatar on
Qatar Airways on Friday the 3</span><sup style="text-align: left;">rd</sup><span style="font-size: 11pt; text-align: left;"> of February 2017 and
Landed in Belgium early hours of Saturday the 4</span><sup style="text-align: left;">th</sup><span style="font-size: 11pt; text-align: left;"> of
February 2017. I stayed there till Monday the 13</span><sup style="text-align: left;">th</sup><span style="font-size: 11pt; text-align: left;"> of
February 2017.</span></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhR8mBYAJozn-qiPctQ0jrczq32SNaLsewrBDKGKMwLI0fI997ceGWnHs69XoLY9XrlReChHM0yeBMSkGCKDpQMGp-0YEMAiEhkEdN_Vlhajc1iwueh42tHZAOF8p8vkd6CJDlojb_0ZuY/s1600/20170204_140613.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhR8mBYAJozn-qiPctQ0jrczq32SNaLsewrBDKGKMwLI0fI997ceGWnHs69XoLY9XrlReChHM0yeBMSkGCKDpQMGp-0YEMAiEhkEdN_Vlhajc1iwueh42tHZAOF8p8vkd6CJDlojb_0ZuY/s320/20170204_140613.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqmRV3MEUmqDuA28jhFI2F5qhXy4kNfGQf0rooaBMrWO1JhtEdT2MpMtPD8lOl-5awBSLmImIAEypjrV-_xnNM-ZWWuDcVbMUYZLLwmTfkJsmxHFxsinRX4lYWiJ1lCwJyYj4_-r10uOU/s1600/20170204_133620.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="1600" data-original-width="900" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqmRV3MEUmqDuA28jhFI2F5qhXy4kNfGQf0rooaBMrWO1JhtEdT2MpMtPD8lOl-5awBSLmImIAEypjrV-_xnNM-ZWWuDcVbMUYZLLwmTfkJsmxHFxsinRX4lYWiJ1lCwJyYj4_-r10uOU/s320/20170204_133620.jpg" width="180" /></a></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;"><br /></span></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;">I
was picked from the Airport by Sudesh, A Sri Lankan Colleague who is
residing in Belgium on Saturday and he drove me to Hotel Izan Avenue,
Louise in the center of Brussels. It was a great Hotel right in the
middle of Brussels' most prestigious area. After I got myself
checked-in in my room, Sudesh bid farewell to me. I spent sometime
settling down in my room then I went out to roam around the city and
to visit a famous park in the City by the name Jubel Park also called
Parc du Cinquantenaire. I went to that park spent some time and
co-incidentally the park had a Mosque, An Air Craft Museum and An
Automobile Museum which I visited during my stay there. </span>
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMM-x3pstVS9LyKPdczd2BbjdwKXokKI-sEiQ2i5haOFOwWgmyeolvcAhSvPeOFMGQ98tKTvFaGXeBxVZKj04CyFBipJehyphenhyphenbrIPdT-fu_FUlb-tu3wUKUj8R62fD7CWvBQ3X1VEIa5twc/s1600/20170205_081946.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMM-x3pstVS9LyKPdczd2BbjdwKXokKI-sEiQ2i5haOFOwWgmyeolvcAhSvPeOFMGQ98tKTvFaGXeBxVZKj04CyFBipJehyphenhyphenbrIPdT-fu_FUlb-tu3wUKUj8R62fD7CWvBQ3X1VEIa5twc/s320/20170205_081946.jpg" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLfE-aHPHJ3jTRWVQ-vn0Ka_h8xfaCWPDaAL9XTldmCPEGuaaxpEzGuxHdLrc1cKQOb3cNcYaSslwDnuc2ygEbbFQNGWanwdgJV33miO5GGYn8ebXXT6Ao3AbAJ2fWpKiLiBTRGWRK7bQ/s1600/20170205_140435.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLfE-aHPHJ3jTRWVQ-vn0Ka_h8xfaCWPDaAL9XTldmCPEGuaaxpEzGuxHdLrc1cKQOb3cNcYaSslwDnuc2ygEbbFQNGWanwdgJV33miO5GGYn8ebXXT6Ao3AbAJ2fWpKiLiBTRGWRK7bQ/s320/20170205_140435.jpg" width="320" /></a></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br /></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;">On
Sunday the 5<sup>th</sup> of February 2017, I traveled to Cologne,
Germany which was some 221 Kilometers away. I traveled on the Bullet
Train which enabled me to travel really fast and get to Cologne
earlier than I expected. The city was one amazing experience. The
city is built around the Rhine River and has a nice cool vibe to it.
The people are super friendly and nice which enabled me to travel
roam around the city. It was a day well spent!.</span></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;">From
Monday the 6<sup>th</sup> of February 2017 to 10<sup>th</sup> of
February I was in Brussels, Belgium working at Immoweb. My Colleagues
at Immoweb were super friendly and took me to a different restaurant
everyday for lunch. They liked the presence of me around them as I am
being the First Sri Lankan most of them have met so everyday the
conversations were good. It gave me a nice time to bond with them
which has greatly improved my work as well. And after Office Hours, I
went through the Brussels City Monuments whenever I could. One night
I visited Grand Palace, which is the Central Square of Brusells, an
area filled with Good Restaurants, Markets etc. It also has
photogenic buildings and some of those are categorized as UNESCO
World Heritage Sites. On another night I went to the Atomium which is
considered as the Symbol of Brussels City. </span>
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCEgw3W0H2FmtirlAtJ5MW6OzuqIXlj_0Hi-TfLYbRPLtn7S1vzGqJN_1oWmTGD-YJTCXYKlgEIPZ8Zn_FCBv5Ap5_2dad_dbWa5LwPrBGM0sRw8YRtOk8hTj3IJ0MmTZj2AULlSk-yQk/s1600/20170206_201220.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCEgw3W0H2FmtirlAtJ5MW6OzuqIXlj_0Hi-TfLYbRPLtn7S1vzGqJN_1oWmTGD-YJTCXYKlgEIPZ8Zn_FCBv5Ap5_2dad_dbWa5LwPrBGM0sRw8YRtOk8hTj3IJ0MmTZj2AULlSk-yQk/s320/20170206_201220.jpg" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_zWe-BOCEirJQZjtDtwgsOF8wFaLeoByF7KTvraPPzhZqqlqBaMhfYsmnecXEzb5pN_rCumARBDPQjl8Jn-Tpnzye4vu7z6spu5YVA_DBo5ISrMX8b1NtV7_48mWrC1NxmkyB2HT7h0M/s1600/20170206_201228.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_zWe-BOCEirJQZjtDtwgsOF8wFaLeoByF7KTvraPPzhZqqlqBaMhfYsmnecXEzb5pN_rCumARBDPQjl8Jn-Tpnzye4vu7z6spu5YVA_DBo5ISrMX8b1NtV7_48mWrC1NxmkyB2HT7h0M/s320/20170206_201228.jpg" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyJytyAGmklXPz84pYTvowAEWuqG9QA4IEiYhNHX-Ln1lMZZsU4Sr1zoHZL8yOv4o35Ur4f0oSEtFkiC6ym4cuf3ZeCJC1yaVuRSqonJKJgtDbbln7dPhR9xeGwts_jkmbrkSrM9eL90U/s1600/20170209_184954.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyJytyAGmklXPz84pYTvowAEWuqG9QA4IEiYhNHX-Ln1lMZZsU4Sr1zoHZL8yOv4o35Ur4f0oSEtFkiC6ym4cuf3ZeCJC1yaVuRSqonJKJgtDbbln7dPhR9xeGwts_jkmbrkSrM9eL90U/s320/20170209_184954.jpg" width="320" /></a></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br /></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;">On
Saturday 11<sup>th</sup> February 2017 I got the chance to visit
Paris, France. This was one of the most memorable days of my life as
I was a huge fan and admirer of the engineering behind Eiffel Tower.
I was lucky enough not only to see it in person but go to the top
most of the tower and see the Paris City from there. It was a great
experience which I advise everyone to experience at least once. I
also visited <span style="color: black;"><span style="font-family: Times New Roman, serif;"><span lang="en">Arc
de Triomphe. </span></span></span></span>
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDkaHu4F2RjJEneh3uOsl6Lz6DFNE7BbAZuDFLOx9Hf2-EQjHmh2VT0xIpnnyhX5p-BXEBdNrp51mFAgO10kC1iNOZJHUjHLWSw9Y8O2mf4FSZPkU_WwFtfGfiz4t8-UxviKCbyS1734c/s1600/20170211_095556.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDkaHu4F2RjJEneh3uOsl6Lz6DFNE7BbAZuDFLOx9Hf2-EQjHmh2VT0xIpnnyhX5p-BXEBdNrp51mFAgO10kC1iNOZJHUjHLWSw9Y8O2mf4FSZPkU_WwFtfGfiz4t8-UxviKCbyS1734c/s320/20170211_095556.jpg" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQl1eAIqr8tWH47O4gIecILnb7myn09mGxBo2alF4lWBF_RC9RUKeKGGOYVzDTiMD8zgEiMaVwF8dwUkUJ1NEM3eUG2Bjy3M_02EDV_VNI8vZKOm708EB7IAV8bzi1Xsr62jVLS85beJs/s1600/20170211_102120.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQl1eAIqr8tWH47O4gIecILnb7myn09mGxBo2alF4lWBF_RC9RUKeKGGOYVzDTiMD8zgEiMaVwF8dwUkUJ1NEM3eUG2Bjy3M_02EDV_VNI8vZKOm708EB7IAV8bzi1Xsr62jVLS85beJs/s320/20170211_102120.jpg" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeerVVhtDLdXbl0-0JtyHH_8P7RfcVmSxzI0Pvu7t9ntba8bdHZZhnn7OGJrB46uvK3ZUtiH_IJhCucJwPEDKsgFZgLApOEKoJENOsXtrcMy3GCtMfmSA023L1wPMwtbH8aefKHE5wIUk/s1600/20170211_111649.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeerVVhtDLdXbl0-0JtyHH_8P7RfcVmSxzI0Pvu7t9ntba8bdHZZhnn7OGJrB46uvK3ZUtiH_IJhCucJwPEDKsgFZgLApOEKoJENOsXtrcMy3GCtMfmSA023L1wPMwtbH8aefKHE5wIUk/s320/20170211_111649.jpg" width="320" /></a></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;">On
Sunday 12<sup>th</sup> of February 2017, I traveled to Amsterdam,
Netherlands which was also amazing City to visit. Amsterdam is a
worth traveling if you love Civil Engineering. It shows the Effort
the Early Engineers have put in order to Build A highly Thriving City
on top of Wooden Piles on ground soil which is too soft to hold huge
buildings. The reason behind Buildings in Amsterdam being narrow and
tall instead of short of wide was also due to this, The Municipality
has taxed Each building based on the width. It was overall an amazing
city to visit and learn about.</span></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZEzy5_7ljhUrIF2RW_w9NA8hpX5Q3wV7RZWT_AwVIC4CLn9pDqDLj6eSlfIqNyLp9J8FgFL4bmmBSZMPkbXg4IToJznAgJevMjFoifSsNRUFcX6DdWLgOEivE5Vy6GjgfUtmLXtGewPM/s1600/20170212_121854.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZEzy5_7ljhUrIF2RW_w9NA8hpX5Q3wV7RZWT_AwVIC4CLn9pDqDLj6eSlfIqNyLp9J8FgFL4bmmBSZMPkbXg4IToJznAgJevMjFoifSsNRUFcX6DdWLgOEivE5Vy6GjgfUtmLXtGewPM/s320/20170212_121854.jpg" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn37bi_QFZpdJiyYzRELt41iqutXuVEn6aEdvuztT-Pxj3A_5o-g3nGfq0DuptoHowVYgteeZUwwG9T_5Fo3vvfDOoGWSYB26LKXVqaOuYe0DrTYhMiTmLtcLNOnV_yb9mgJgS2YzL7lU/s1600/20170212_145210.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="900" data-original-width="1600" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn37bi_QFZpdJiyYzRELt41iqutXuVEn6aEdvuztT-Pxj3A_5o-g3nGfq0DuptoHowVYgteeZUwwG9T_5Fo3vvfDOoGWSYB26LKXVqaOuYe0DrTYhMiTmLtcLNOnV_yb9mgJgS2YzL7lU/s320/20170212_145210.jpg" width="320" /></a></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<span style="font-size: 11pt;">That
was weekend was arguably the longest weekend in my life as I traveled
to two new countries and close to 510 Kilometers. And on Monday the
13<sup>th</sup> of February 2017 I bid farewell to Belgium and Europe
and Boarded my flight bound to Sri Lanka via Doha, Qatar on Qatar
Airways around 4.15 p.m. Belgium time with a lot memories and thirst
to come back again. I learned a lot from this visit and it has opened
my eyes to see the World in a whole new perspective. Meeting new
people, Going to new places certainly has a deep impact on your soul
and I advise everyone to visit these places and more places at least
once in their life time.</span></div>
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
<br />
<div align="JUSTIFY" style="margin-bottom: 0in;">
<br />
</div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-44045562744406495112017-01-10T23:12:00.001-08:002017-01-10T23:12:36.381-08:00Spring data JPA with dynamic where clause<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
It may be a Common Use Case where you need to dynamically submit a WHERE Clause to a Spring Data Repository to get an Entity instead of Writing findBy* methods or @Query annotated methods. You can do the following;</div>
<pre class="lang-java prettyprint prettyprinted" style="background-color: #eff0f1; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> org</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">springframework</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">data</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">jpa</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> org</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">springframework</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">data</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">jpa</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaSpecificationExecutor</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> org</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">springframework</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">data</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">NoRepositoryBean</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> java</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">io</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Serializable</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> java</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">util</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Map</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="com" style="border: 0px; color: #858c93; margin: 0px; padding: 0px;">/**
* Created by shazi on 1/11/2017.
*/</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="lit" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">@NoRepositoryBean</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">public</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">interface</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">IBaseRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> ID </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">extends</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Serializable</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">extends</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> ID</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">>,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaSpecificationExecutor</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">{</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
T findOne</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">String</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> filter</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Map</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">String</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Object</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> params</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">);</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">}</span></code></pre>
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
And Implement it as follows.</div>
<pre class="lang-java prettyprint prettyprinted" style="background-color: #eff0f1; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> org</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">springframework</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">data</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">jpa</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">support</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaEntityInformation</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> org</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">springframework</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">data</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">jpa</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">support</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">SimpleJpaRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> javax</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">persistence</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">EntityManager</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> javax</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">persistence</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Query</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> java</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">io</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Serializable</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">import</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> java</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">util</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Map</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="com" style="border: 0px; color: #858c93; margin: 0px; padding: 0px;">/**
* Created by shazi on 1/11/2017.
*/</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">public</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">class</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">BaseRepositoryImpl</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> ID </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">extends</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Serializable</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">extends</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">SimpleJpaRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> ID</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">implements</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">IBaseRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> ID</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">{</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">private</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">final</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">EntityManager</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityManager</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">private</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">final</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaEntityInformation</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityInformation</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">public</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">BaseRepositoryImpl</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">JpaEntityInformation</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityInformation</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">EntityManager</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityManager</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">{</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">super</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">entityInformation</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityManager</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">);</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="com" style="border: 0px; color: #858c93; margin: 0px; padding: 0px;">// Keep the EntityManager around to used from the newly introduced methods.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">this</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">entityManager </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityManager</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">this</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">entityInformation </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityInformation</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">}</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="lit" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">@Override</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">public</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> T findOne</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">String</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> filter</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Map</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">String</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Object</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> params</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">{</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">final</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">String</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> jpql </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">"FROM "</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">+</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityInformation</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">getEntityName</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">()</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">+</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">" WHERE "</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">+</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> filter</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">;</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Query</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> query </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> entityManager</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">createQuery</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">jpql</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">);</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">for</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Map</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Entry</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">String</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Object</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">></span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> value</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">:</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">params</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">entrySet</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">())</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">{</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
query</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">setParameter</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">value</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">getKey</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(),</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> value</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">getValue</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">());</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">}</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">return</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">T</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> query</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">getSingleResult</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">();</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">}</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">}</span></code></pre>
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
And configure it as follows</div>
<pre class="lang-java prettyprint prettyprinted" style="background-color: #eff0f1; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="lit" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">@Configuration</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="lit" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">@EnableJpaRepositories</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repositoryBaseClass </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">BaseRepositoryImpl</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">class</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">)</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="lit" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">@EnableTransactionManagement</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">
</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">public</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">class</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">RepoConfig</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">{</span></code></pre>
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
or in XML</div>
<pre class="lang-java prettyprint prettyprinted" style="background-color: #eff0f1; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"><</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">repositories base</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">-</span><span class="kwd" style="border: 0px; color: #101094; margin: 0px; padding: 0px;">class</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="str" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">"….BaseRepositoryImpl"</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">/></span></code></pre>
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
Finally you can use it as follows;</div>
<pre class="lang-java prettyprint prettyprinted" style="background-color: #eff0f1; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">User</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> found </span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">=</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> userRepository</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">findOne</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="str" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">"name = :name"</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="typ" style="border: 0px; color: #2b91af; margin: 0px; padding: 0px;">Collections</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">.</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">singletonMap</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">(</span><span class="str" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">"name"</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">,</span><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;"> </span><span class="str" style="border: 0px; color: #7d2727; margin: 0px; padding: 0px;">"name"</span><span class="pun" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">));</span></code></pre>
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
But you have to make sure that your query WHERE is such that the Query will always return 1 result only.<br /><br /><b>References </b></div>
<div style="background-color: white; border: 0px; clear: both; margin-bottom: 1em; padding: 0px;">
</div>
<ul style="text-align: left;">
<li><span style="color: #242729; font-family: Arial, Helvetica Neue, Helvetica, sans-serif;"><span style="font-size: 15px;"><a href="https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-behaviour-for-all-repositories">https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-behaviour-for-all-repositories</a></span></span></li>
</ul>
<br />
<div style="background-color: white; border: 0px; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; margin-bottom: 1em; padding: 0px;">
<br /></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-59501927030649969052016-12-26T22:31:00.003-08:002016-12-27T00:59:11.993-08:00Hello, World of Google AndroidThings IoT Platform with Raspberry Pi 3<div dir="ltr" style="text-align: left;" trbidi="on">
Earlier this month Google Announced <a href="https://developer.android.com/things/index.html">AndroidThings</a> a Platform with a Stripped down version of Android Operating System which can be run on multiple existing IoT Enabled Hardware like Raspberry Pi 3, Intel Edison and NXP Pico. On the same day I took this picture;<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg81di0MIKIQggoSWj0cSgtaGofygf0dy9hu0o9lT6mPNxxI2Fek-Oh3M_m-dv9vk4dW_7V3lFzhKR_8HTYbf7KUeeH3PCO_gzhlSAtRddRljYWcujbZ5SUu98NbbOs6jEI80OMgCwdo0k/s1600/Android.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg81di0MIKIQggoSWj0cSgtaGofygf0dy9hu0o9lT6mPNxxI2Fek-Oh3M_m-dv9vk4dW_7V3lFzhKR_8HTYbf7KUeeH3PCO_gzhlSAtRddRljYWcujbZ5SUu98NbbOs6jEI80OMgCwdo0k/s320/Android.jpg" width="320" /></a></div>
<br />
<br />
<div style="text-align: justify;">
AndroidThings is primarily focused on reducing the development and deployment time when it comes to IoT. Android being the most popular Mobile Operating System thus far has now made its way into the IoT arena with AndroidThings IoT Platform.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The purpose of this article is to try out the AndroidThings platform and evaluate its Developer friendliness.</div>
<br />
<h3 style="text-align: left;">
Flashing the AndroidThings Operating System</h3>
<div>
<div style="text-align: justify;">
This can be considered as the First Step of the whole process of developing apps for AndroidThings IoT Platform. With your Hardware Device Raspberry Pi 3, Intel Edison or NXP Pico you can download the correct System Image from <a href="https://developer.android.com/things/preview/download.html">https://developer.android.com/things/preview/download.html</a></div>
</div>
<div>
<div style="text-align: justify;">
<br /></div>
</div>
<div>
<div style="text-align: justify;">
And you can follow the Hardware Device's instruction on how to flash the System Image into the Hardware Device. I used a Raspberry Pi 3 so I followed the Instruction available for that.</div>
</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Setting Up Development Environment</h3>
<div>
If you were an Android Developer already just like me, you probably have Development Environment setup already and you can skip this step. If not you can Download Android Studio from <a href="https://developer.android.com/studio/index.html">https://developer.android.com/studio/index.html</a> and install it. Make sure to download the package with Android SDK if you are not comfortable with configuring PATH manually.</div>
<h3 style="text-align: left;">
<br /></h3>
<h3 style="text-align: left;">
Configuring the AndroidThings Operating System to use Wifi</h3>
<div>
This is the third step as it enables to connect to your device over Wifi for Easy Deployment and Debugging of Apps. I followed this <a href="https://developer.android.com/things/hardware/raspberrypi.html">https://developer.android.com/things/hardware/raspberrypi.html</a> as I used a Raspberry Pi 3 but you can follow the correct link based on your Hardware listed here <a href="https://developer.android.com/things/preview/index.html">https://developer.android.com/things/preview/index.html</a><b>.</b></div>
<div>
<b><br /></b></div>
<h3 style="text-align: left;">
Connecting to AndroidThings Operating System via adb command</h3>
<div>
After finding the Ip Address of the Device you can issue the following command to connect to the AndroidThings operating system via adb command</div>
<div>
<br /></div>
<div>
<pre class="prettyprint notranslate" style="-webkit-font-smoothing: subpixel-antialiased; background: rgb(247, 247, 247); border: 1px solid rgb(221, 221, 221); color: #006600; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; font-size: 13px; font-stretch: normal; line-height: 18px; margin-bottom: 6px; margin-top: 6px; overflow: auto; padding: 1em;" translate="no"><code style="-webkit-font-smoothing: subpixel-antialiased; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; font-stretch: normal; line-height: 18px;"><span class="pln" style="color: black;">adb connect </span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">ip</span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: black;">address</span><span class="pun" style="color: #666600;">></span></code></pre>
</div>
<div>
<div style="text-align: justify;">
<br /></div>
</div>
<div>
<div style="text-align: justify;">
This will connect the your development machine and Hardware device. Now it is just like developing any Android App. You can just use the Android Studio IDE to create apps and run it just like you run an app in an Android Mobile Phone.</div>
</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Demo (Blink and Button App)</h3>
<div>
<br />
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/Xf1aGU4X3N0" width="560"></iframe><br />
<br />
<br />
<h3 style="text-align: left;">
Evaluation of the AndroidThings IoT Platform</h3>
</div>
<h4 style="text-align: left;">
Pros</h4>
<div>
<ol style="text-align: left;">
<li>Hardware Device Agnostic</li>
<li>Very Gradual Learning Curve (If you are familiar with Android App Development)</li>
<li>Ability use many many Android Tutorials available</li>
<li>Great Community of Developers backed by Google</li>
</ol>
<h4 style="text-align: left;">
Cons</h4>
</div>
<div>
<ol style="text-align: left;">
<li>Limited no of Hardware Devices supported yet (Only 3 when writing this)</li>
<li>Lack of Libraries for reading Sensors like DHT22 and many more in Java. (It would take some time to on board all sensor libraries to Java)</li>
<li>A Steep Learning Curve (If you are not familiar with Android App Development)</li>
</ol>
<div>
<br /></div>
</div>
<h3 style="text-align: left;">
Conclusion</h3>
<div style="text-align: right;">
<div style="text-align: justify;">
Google AndroidThings IoT Platform is definitely a step in the right direction. There are so many Hardware Devices and Operating Systems for IoT it is almost too easy to get lost. With this Platform Google has tried to bring in the "Write Once, Run on Any Hardware" concept to IoT arena which must be commendable. But as far as I think it is still a work in progress and I am pretty sure with the resources Google has within couple years time this platform has the potential to become a Game changer and backbone of IoT.</div>
</div>
<div>
<div style="text-align: justify;">
<br /></div>
</div>
<div>
<div style="text-align: justify;">
<h3>
Trackbacks/Pings</h3>
</div>
</div>
<div>
<div style="text-align: justify;">
<br />
<ul>
<li><b>IoT Researcher Blog - <a href="http://blog.iotresearcher.com/post/simple/">http://blog.iotresearcher.com/post/simple/</a></b></li>
</ul>
</div>
</div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com1tag:blogger.com,1999:blog-7941666868022023550.post-38464255005444789472016-11-08T00:28:00.000-08:002016-11-22T01:02:55.903-08:00RFID Parking Solution using Raspberry Pi<div dir="ltr" style="text-align: left;" trbidi="on">
Sri Lanka has a lot of Parking Lots Managed by RDA (Road Development Authority) but almost all use paper receipts to maintain Timings of Parked Vehicles. This process is error prone and difficult to keep on days where vehicle come and go out frequently.<br />
<br />
This motivated me to do a small PoC on a Low Cost RFID based Parking Solution. I had a HZ-1050 RFID Reader lying around in my Hack Space and 1 RFID Card with 2 RFID Tags so I decided to use those.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-BxzsC4iD3AAAMeio60ShztdQapxOOKc_WuAj-9gaZvNyULBtQdlJYfeX0g29QdQ-sJ2ih9I-L8NTlge5c1jWMQXVrH6sp1saeQjj4ZrUSp125iNg5b5l5_4OWy0Qn4w47S0Gaeb4mXU/s1600/20161108_122054.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-BxzsC4iD3AAAMeio60ShztdQapxOOKc_WuAj-9gaZvNyULBtQdlJYfeX0g29QdQ-sJ2ih9I-L8NTlge5c1jWMQXVrH6sp1saeQjj4ZrUSp125iNg5b5l5_4OWy0Qn4w47S0Gaeb4mXU/s320/20161108_122054.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
Above is the Setup which is really simple, Just need to wire up Raspberry Pi 5v -> HZ-1050 5v, Raspberry Pi Ground -> HZ-1050 Ground, HZ-1050 D0 -> Raspberry Pi Pin 14, HZ-1050 D1 -> Raspberry Pi Pin 15<br />
<br />
Finally the Code.<br />
<br />
<b>Reading RFID Card/Tag and Storing in Database</b><br />
<br />
I used PiGPIO Library with a Small a Python Script modified from their Example to read the Wiegand Format RFID No and Store it in a SQLite3 Database.<br />
<br />
<b><br /></b>
<b>Displaying in Dashboard</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp2qq1Qf7xavB63-6blSqOEY52X2CmVxbTYhGzCLBzUOGbON1q6KgA6E8VvX3TYzhRZjjmYoA6DUSwkajIMWNh6dDw1Iw3yLroN9PvlrJEfjJ-uZ-X4813-txs5gPT3FjF7Sx0dikUkQE/s1600/20161108_122038.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp2qq1Qf7xavB63-6blSqOEY52X2CmVxbTYhGzCLBzUOGbON1q6KgA6E8VvX3TYzhRZjjmYoA6DUSwkajIMWNh6dDw1Iw3yLroN9PvlrJEfjJ-uZ-X4813-txs5gPT3FjF7Sx0dikUkQE/s320/20161108_122038.jpg" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
I wrote a small Dashboard which has a PHP backend and runs on Light Web Server and connect to the same SQLite3 Database to display data. The Front End Bootstrap with JQuery and Javascript.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
Following is a Demonstration on everything working together. I will upload code once I clean those up. This can store different Vehicle Types and their Charges for the First Hour and Additional Hours and Calculate the Duration and Charge. Which will be great for the Parking Operator.</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/L3eFGjyVWYE" width="560"></iframe></div>
<div class="separator" style="clear: both; text-align: left;">
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: left;">
<b>References</b></div>
<ol style="text-align: left;">
<li><a href="http://www.penguintutor.com/linux/light-webserver">http://www.penguintutor.com/linux/light-webserver</a></li>
<li><a href="http://abyz.co.uk/rpi/pigpio/">http://abyz.co.uk/rpi/pigpio/</a></li>
</ol>
<div>
<b>Parts can be bought at IoT Researcher Shop</b></div>
<ol style="text-align: left;">
<li>HZ-1050 RFID Reader - <a href="http://shop.iotresearcher.com/index.php/en/sensors-c-1/hz-1050-rfid-reader-p-9">http://shop.iotresearcher.com/index.php/en/sensors-c-1/hz-1050-rfid-reader-p-9</a></li>
<li>RFID Card - <a href="http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-card-p-7">http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-card-p-7</a></li>
<li>RFID Tag - <a href="http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-tag-p-6">http://shop.iotresearcher.com/index.php/en/accessories-c-3/rfid-tag-p-6</a></li>
</ol>
<b>Trackbacks/Pings</b><br />
<ol style="text-align: left;">
<li>Hackaday - <a href="http://hackaday.com/2016/11/21/faulty-parking-meter-tracking-system-rfid-to-the-rescue/">http://hackaday.com/2016/11/21/faulty-parking-meter-tracking-system-rfid-to-the-rescue/</a></li>
<li>BricoGeek - <a href="http://blog.bricogeek.com/noticias/raspberry-pi/control-de-parking-con-rfid-y-raspberry-pi/">http://blog.bricogeek.com/noticias/raspberry-pi/control-de-parking-con-rfid-y-raspberry-pi/</a></li>
</ol>
<br />
<br /></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com1tag:blogger.com,1999:blog-7941666868022023550.post-91919632979623090402016-11-01T22:21:00.003-07:002016-11-01T22:21:43.592-07:00Real Time Streaming Data Processing with Arduino + Raspberry Pi + Apache Kafka + Apache Spark + MQTT<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
World is constantly going towards a fully automated environment where Smart Industries to Self Driving Cars coming into the scene everyday with the hype of Internet of Things (IoT). Almost all of these automation generate Gigabytes if not Terabytes of sensor data which needs to be processed to make sense of what is happening in the automation.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
So for me the most important thing of this would be the ability to develop Scale-able backend systems which could process these data and make them into information which can be used to make strategic decisions whether it be in a Smart Industry to predict which part needs to be replaced and for Self Driving Cars when to hit the Garage for a Service.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
With this in mind I researched on how to build these types of backend and found out that Apache Spark is a go to open source framework tailor made for this. It has the capability to process data in Batch, Real Time and to make sense of those data with its Machine Learning and Graphing capabilities.</div>
<div style="text-align: justify;">
This made me intrigued to learn more about it and this is my attempt on trying to implement an end to end system from pushing sensor data to real time processing of it.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
For this I am using a hypothetical Smart Home with a Thermostat which sends data to a backend server which then processes and understands what's going on. For Demo purpose I will be sending Temperature and Humidity readings from My home hall to the Backend Server via the following flow and calculate in real time the minimum, maximum and average readings of Temperature and Humidity in every 30 second window. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<b>System Architecture </b><br />
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC-AOLeK4XgznedfRX2pfFSJcwJn_QxY4ybjOz8nqNQkkAZ6qmxBWuOpBDJnwz1jkC0tI43P3jCQaJHWt-eBKJ-3NHGXt3Gix4q3kyu7POQ8Kxm2HvCzxGNrFwxN7Qn2hIu3b5I_ZmqnE/s1600/SystemArchitecture.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="340" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC-AOLeK4XgznedfRX2pfFSJcwJn_QxY4ybjOz8nqNQkkAZ6qmxBWuOpBDJnwz1jkC0tI43P3jCQaJHWt-eBKJ-3NHGXt3Gix4q3kyu7POQ8Kxm2HvCzxGNrFwxN7Qn2hIu3b5I_ZmqnE/s400/SystemArchitecture.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: justify;">
<span style="text-align: left;"><b>Arduino with Ethernet Shield and DHT22 Sensor</b></span></div>
<div class="separator" style="clear: both; text-align: justify;">
<span style="text-align: left;"><b><br /></b></span></div>
<script src="https://gist.github.com/iotresearcher/f3db2776b31e592f237a43ab61d02588.js"></script>
<br />
<div class="separator" style="clear: both; text-align: justify;">
<span style="text-align: left;"><b><br /></b></span></div>
<div style="text-align: justify;">
<b>Demonstration</b><br />
<b><br /></b>
In the demonstration I show how the DHT22 Sensor Readings are coming into Apache Kafka and then finally being processed in Apache Spark. To differentiate the Temperature and Humidity I placed a Tub of Ice Cream near the DHT22 Sensor.<br />
<br />
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/571iVDqkkFI" width="560"></iframe>
<b><br /></b>
<b><br /></b><br />
<b>References</b></div>
<div style="text-align: justify;">
</div>
<ol>
<li><a href="https://github.com/iotresearcher/mqtt-kafka-bridge">https://github.com/iotresearcher/mqtt-kafka-bridge</a></li>
<li><a href="https://github.com/iotresearcher/spark-streaming">https://github.com/iotresearcher/spark-streaming</a></li>
<li><a href="https://www.infoq.com/articles/apache-spark-introduction">https://www.infoq.com/articles/apache-spark-introduction</a></li>
<li><a href="https://kafka.apache.org/quickstart">https://kafka.apache.org/quickstart</a></li>
</ol>
<br />
<div style="text-align: justify;">
</div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com1tag:blogger.com,1999:blog-7941666868022023550.post-20654742761014817712016-09-24T00:37:00.002-07:002016-09-24T00:37:56.418-07:00Advanced Arduino Programming : Threads, Semaphores and Mutexs with ChibiOS-Arduino<div dir="ltr" style="text-align: left;" trbidi="on">
Arduino is a great tool for rapid prototyping. It has a good Hardware and Software platform to quickly get started. But when it comes to Advanced Software concepts like Threads, Semaphores and Mutexes the Vanilla Arduino IDE doesn't have much to offer.<br />
<br />
That is where ChibiOS comes in. ChibiOS is Real Time Operating System (RTOS) which is intended for Embedded system. Likely ChibiOS has a port for Arduino which can be used in Arduino IDE as a Library.<br />
<br />
Using this allows Parallel code execution, Synchronized code excution and many more possible in Arduino. Previously I have done a small project to show <a href="http://shazsterblog.blogspot.com/2016/03/temperature-and-humidity-measuring.html">Temperature and Humidity on 8x2 LCD Display</a>.<br />
<br />
This project has two parts, a Sensor Input Reading and LCD Output Writing both of which are done inside the <a href="https://www.arduino.cc/en/Reference/Loop">void loop()</a> section of the Arduino Sketch. But as we all know Input and Output are tasks can sometimes take longer than expected. For Ex:- During Network Input and Output.<br />
<br />
So It is better to Parallelize the Input and Output. This can be achieved with ChibiOS-Arduino Library.<br />
<br />
So I used the same setup from the aforementioned post with Threads this time to separate Read and Write. Plus Reading will take place every 200 Milliseconds but Writing will take place every 1000 Milliseconds (1 Second). And Read Thread and Write Thread communication is done via a Semaphore. Write Thread will wait on the Semaphore for data to be available and Read Thread will Signal on the Semaphore whenever data is available. So the Serial output for the Arduino Program will look like following, 1 Output Thread for 5 Input Threads<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVMam3TcfM6rQTFYWrxIcRb0ooDHjAwKedYK5zGAbZ12D0kA9SrVQVvMe2l_l-aeaI-wXQDmsCJEeKUK8GOFqAvEvF-LAmh5nC82MoRftCwPuukqtruPJPE1q6EWep-2SJxAzYhUBEqQM/s1600/Output.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="245" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVMam3TcfM6rQTFYWrxIcRb0ooDHjAwKedYK5zGAbZ12D0kA9SrVQVvMe2l_l-aeaI-wXQDmsCJEeKUK8GOFqAvEvF-LAmh5nC82MoRftCwPuukqtruPJPE1q6EWep-2SJxAzYhUBEqQM/s320/Output.png" width="320" /></a></div>
<br />
The full Arduino Sketch is following;<br />
<br />
<br />
<script src="https://gist.github.com/shazin/c59c3b9de54021225d473a1ee9f3e9c0.js"></script>
<br />
<b>References</b><br />
<br />
<ol style="text-align: left;">
<li><a href="https://www.blogger.com/"><span id="goog_1919313251"></span>ChibiOS<span id="goog_1919313252"></span></a></li>
<li><a href="https://github.com/greiman/ChibiOS-Arduino">ChibiOS-Arduino</a></li>
<li><a href="https://www.youtube.com/watch?v=JXy86GrjVso">Getting Started With ChibiOS</a></li>
</ol>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-27977847405341790092016-09-04T23:41:00.001-07:002016-09-05T01:02:14.386-07:00Quick Hack Arduino Marine Fish Feeder<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
Anyone who owned a pet would know that the biggest worry you have is when you leave your pets at home on vacation is how to feed them.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I am a novice Marine fish keeper with a 32 Litre Nano Saltwater Fish Only Aquarium. The livestock is not much but I have 2 Clarks Clownfish (Amphiprion clarkii) which are common in Sri Lankan waters, 1 Ocellaris clownfish (Amphiprion ocellaris) which is actually not so common in Sri Lankan waters but imported from Pacific Oceanic waters specifically Indonesia and 1 Sand Gobi which usually stays under the rock. </div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Sri Lanka has strict policies on handling Live Rock and Corals in order to protect our shores, so it is almost impossible to find a legal pet store having those. Importing also strictly controlled.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Anyway I wanted a way to feed the fish while I was away on Vacation for 2 days and 1 night. So with this intention I put up a quick hack fish feeder within couple of hours which actually managed to do the job and keep my fish fed and alive. I had to come up with this because Automatic Fish Feeders are also not so common in Sri Lanka and one have to import it from online shops if they really want one.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I had some servos and an Arduino UNO Rev 3 hanging around and managed to put a small step up where Arduino pin 9 is connected to Servo's actuating pin and 5v to power pin and ground pin to ground.</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVqfcAqwAWNTzfA0ILyjP7-Z1Hio8zok4MwEVc28EBGVvl4_Edpn3af_rUq271DsKLcW7MD2G3Yy4f_7O-JNy5VlcAzN_Fm-mNGUxumCmBjX1Hn9KtRFpea0kIe4UaoBWcHBK-kBolZFw/s1600/20160901_230034.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVqfcAqwAWNTzfA0ILyjP7-Z1Hio8zok4MwEVc28EBGVvl4_Edpn3af_rUq271DsKLcW7MD2G3Yy4f_7O-JNy5VlcAzN_Fm-mNGUxumCmBjX1Hn9KtRFpea0kIe4UaoBWcHBK-kBolZFw/s400/20160901_230034.jpg" width="225" /></a></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
With this small setup I took the Example for Servo Sweep and Managed to modify it so that the Servo does 180 Degrees rotation 5 times every 6 hours.<br />
<br />
<br />
<script src="https://gist.github.com/shazin/e87bb0d4510494a450ab88eed8bca31a.js"></script>
<br />
I took a candy container, cut it to make it shorter and put some holes near the bottom with a hot nail. Then went in the fish food. Finally I used to box lid to hold the servo in place and managed to secure it with rubber bands, powered the Arduino using a 12v power adaptor and voila! I have a Quick hack fish feeder.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
You can see the feeder in action below in video,</div>
<div style="text-align: justify;">
<br /></div>
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/DQXL4tXc7aY" width="560"></iframe>
<br />
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<b>References</b></div>
<div style="text-align: justify;">
<b><br /></b></div>
<div style="text-align: justify;">
</div>
<ol>
<li><a href="http://www.instructables.com/id/Arduino-Fish-Feeder/">http://www.instructables.com/id/Arduino-Fish-Feeder/</a></li>
</ol>
<br />
<div style="text-align: justify;">
<br /></div>
</div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com0tag:blogger.com,1999:blog-7941666868022023550.post-19222836050014848302016-04-26T02:04:00.004-07:002017-01-04T05:22:43.497-08:00Spring Security 4.1.0 Features implemented by Me<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
Recently I got the chance to contribute to an open source project which I have been using for a really long time. Spring Security is one of the de-facto projects when it comes to protecting Web Applications as well as some Standalone applications.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
It provides many features with regards to Authentication and Authorization out of the box and it is highly customizable and extendable to work with any third party, proprietary Security implementations (SSO, OAuth, Openid etc).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
I took the dive in by forking the Spring security project available at <a href="https://github.com/spring-projects/spring-security">github</a> and went through the issues and found issues that are interesting to me and sent pull requests. I must thank <a href="https://github.com/rwinch">Rob Winch</a> who is the Spring security Project Lead who instructed me on how things need to work.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Before I knew it I have submitted 4 pull requests which have been merged with of course minor changes based on Project Lead's review. The Spring Security version 4.1.0.RC2 is now available to use if you are using the Milestone repository of the Spring project. Which means the features I implemented are almost available to the rest of the world.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Well I took that opportunity to write about the features I implemented which are also documented in the <a href="http://docs.spring.io/spring-security/site/docs/4.1.0.RC2/reference/htmlsingle/">Spring Security reference guide</a>. Following are the features I partially/fully contributed to Spring Security.</div>
<br />
<b>SCryptPasswordEncoder</b><br />
<br />
Spring Security ships alot of Password encoders such as MD5, SHA which the developers can use to encode the password before storing in the database. This gives added security in case of Security breaches because all the hackers will be getting is an hash not a clear text password.<br />
<br />
<a href="https://en.wikipedia.org/wiki/Scrypt">Scrypt</a> is a similar encoding algorithm and I implemented the PasswordEncoder implementation using the Bouncy Castle library to be used in Spring Security.<br />
<br />
<br />
<script src="https://gist.github.com/shazin/8d7a80b55bd40a7119418952e81d6e2d.js"></script>
<b>ForwardAuthenticationSuccessHandler</b><br />
<br />
<div style="text-align: justify;">
AuthenticaionSuccessHandlers give the ability to extend the capabilities of the Spring Security to allow the developers to do a task after a successful login by a user. This can be auditing, etc. In order to forward to a URL after the successful authentication the user had to write custom implementation of AuthenticationSuccessHandler. Well not any more.</div>
<b><br />
ForwardAuthenticationFailureHandler</b><br />
<br />
<div style="text-align: justify;">
<span style="font-weight: bold;"><br /></span></div>
<div style="text-align: justify;">
AuthenticaionFailureHandlers give the ability to extend the capabilities of the Spring Security to allow the developers to do a task after a failed login by a user. This can be auditing, locking out etc. In order to forward to a URL after the failed authentication the user had to write custom implementation of AuthenticationFailureHandler. Well not any more.</div>
<br />
<br />
<b>FormLoginConfigurer.successForwardUrl</b><br />
<b><br /></b>
Since creating and initializing ForwardAuthenticationSuccessHandler can be cumbersome at times. The lead wanted a convenient method to set successForwardUrl in a fluid API way. Now the same thing can be accomplished using XML as well using <span style="white-space: pre-wrap;">authentication-success-forward-url under form-login XML tag.</span><br />
<b><br /></b>
<b>FormLoginConfigurer.failureForwardUrl</b><br />
<br />
Since creating and initializing ForwardAuthenticationFailureHandler can be cumbersome at times. The lead wanted a convenient method to set failureForwardUrl in a fluid API way. Now the same thing can be accomplished using XML as well using <span style="white-space: pre-wrap;">authentication-failure-forward-url under form-login XML tag.</span><br />
<br />
<script src="https://gist.github.com/shazin/54b2a592ed97593522af097aba68108b.js"></script>
<b>DelegatingLogoutSuccessHandler</b><br />
<b><br /></b>
LogoutSuccessHandlers are an extension in Spring Security where an action can be performed based on Success of the logout. For this there can be different use cases for example. If a request came from a web browser the logout may redirect to a web page after logout. But if the request came from within an AJAX can there seems to be no point of redirecting to page but better to return a status code alone. This implementation takes care of that in a configurable way. A request matcher and logout success handler is mapped based on the request criteria.<br />
<br />
<script src="https://gist.github.com/shazin/388bab07e0ce302c30072fc44e1db5ed.js"></script>
The complete source codes for the examples is available at <a href="https://github.com/shazin/spring-security-examples">https://github.com/shazin/spring-security-examples</a> and following image shows the example in action.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqRHkstBrj38uOpPDP66GHUVQ_3df5gNXm6f2m2Dt6vps57aHCS3u5-bHmDYNBG41hkzGXfSidMznVWB54a9sXOkyXxWUMKqfayjAXwMN2msYYb5gfQsGUqjCqoS6uy1TQjSjkkS9wqUo/s1600/spring-security.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="103" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqRHkstBrj38uOpPDP66GHUVQ_3df5gNXm6f2m2Dt6vps57aHCS3u5-bHmDYNBG41hkzGXfSidMznVWB54a9sXOkyXxWUMKqfayjAXwMN2msYYb5gfQsGUqjCqoS6uy1TQjSjkkS9wqUo/s400/spring-security.png" width="400" /></a></div>
<br />
<b>Mentions</b><br />
<b><br /></b>
Spring Blog - <a href="https://spring.io/blog/2016/03/24/spring-security-4-1-0-rc1-released">https://spring.io/blog/2016/03/24/spring-security-4-1-0-rc1-released</a><br />
<br />
<br /></div>
shazsterblog.blogspot.comhttp://www.blogger.com/profile/16033913838278307155noreply@blogger.com2