What's New Here?

apache-maven-logo

Maven Archetype Creation Tips

I recently needed to create some Maven Archetypes for the Turmeric SOA project. A Maven Archetype for those that don’t know is a way to generate a project based off some pre-canned project templates. In the case of the current Turmeric SOA archetype, it will create a multi-module maven project, that contains the Interface, and Service projects, along with a basic WSDL, and the POMs configured appropriately. This can be run either at the Command Line or through the New Maven Project wizard of m2eclipse. Other IDE’s such as NetBeans, IntelliJ, and others can use it as well, through their support of Maven Archetypes. The hardest thing with getting started with Maven Archetypes is the relatively poor documentation that exists. Beyond the most basic use (archetype:create-from-project), which creates an archetype given an existing maven project or parent-project, there isn’t much to go on. So here are some tips. Directory/Filename Substitution There are cases during template generation where you want to output different module names and sometimes different filenames. To do this rename the portion of the directory or file you want to have substitued with a variable name. For example, if you specify __rootArtifactId__ as part of a Directory name or Filename, what ever is entered as the artifactId during the mvn archetype:generate will be substituted. You can do the same with custom properties. Example. Built in properties There are some built in properties that can be used during substitution. These are:groupId artifactId rootArtifactId version package – a base java package name, that is placed in src/main/java during project creation.You can use these in your POM and other file templates as well. Archetype-metadata.xml The archetype-metadata.xml stored in the archetype-resources/META-INF/maven folder after the project is created, is where you need to tweak what you want generated and how. For modules, you need to tweak attributes:id – This is the name of the module that will be generated. dir – the template dir name – the artifact Id that will be put in the pom file.For the id and name attributes you can use the standard maven properities specification: ${someproperty}. For the dir, you need to use the special __someproperty__ notation. In most cases this will be __rootArtifactId__. An example from the Turmeric SOA archetype. Required Properties Required Properties is how you prompt for additional information that can be substituted in your templates. Any properties you specify will be prompted for during generation. You can pass them in from the command line as well using the -D option. Required properties are referenced in your templates as properties and are substituted during generation. Be careful of specifying default values, default values are not prompted for during generation. They can still be overridden using the -D option. Archetype Catalogs It is important to have an archetype catalog. If you are using Nexus 1.9.x it will automatically search your repositories and generate an archetype catalog for each repository. It scans for all archetypes and as soon as an archetype is deployed the catalog entry is updated. If you don’t have an archetype catalog already, maven can create one for you from your local repository. Just run mvn archetype:crawl. The output will be put in your .m2 repository. Archetypes are important as they allow others to know about your custom archetype, and provide a way for them to be shared and discovered. Hopefull with these few tips, you can spare yourself some of the headaches that I endured. My next task is to help the Minerva project have a few Archetypes created, so that eclipse projects can get started with Tycho a bit easier. Do you have other tips, or items to watch out for? If so, please feel free to add a comment. Reference: Maven Archetype Creation Tips from our JCG partner David Carver at the Intellectual Cramps blog....
jcg-logo

Best Of The Week – 2012 – W08

Hello guys, Time for the “Best Of The Week” links for the week that just passed. Here are some links that drew Java Code Geeks attention: * Java.next: In this presentation, the argument that Java is still the best programming language for the JVM (if simplified idioms are used along with proper tooling) is posed. Also see The Most Powerful JVM Language Available and Hate Java? You’re fighting the wrong battle. * The ultimate guide to passwords: Nice article on passwords and security. It discusses password hashing, rainbow attacks, salted hashes, offline cracking, secure passwords (those complex enough to make cracking it uneconomical) and password vaults. Also check out Introduction to Strong Cryptography for a developer’s perspective on the matter. * Installation: Where Software Remains in the Dark Ages: In this article the author criticizes some bad practices related to software installation, such as irreversible changes in the system, extensive use of environment variables, forced reboots and others. * Tracking User Behavior with Google Analytics SDK for Android: This tutorial show how to use the Google Analytics SDK for Android in order to track Android application events like screen loads and button clicks in order to determine what your application’s users are doing. Also check out Android Game Postmortem – ArkDroid Development and “Android Full Application Tutorial” series. * Should All Web Traffic Be Encrypted?: Jeff Atwood discusses web traffic encryption and argues that over the medium to long term, adopting encrypted web connections as a standard for logged-in users is the healthiest direction for the future of the web. Also see Hints for writing secure code. * The 7 habits of highly effective developers: Nice article on the habits that effective developers should adopt, such as adopting proactivity, thinking win-win, beginning with the end in mind and others. * Hidden evils of Java’s String.split() and replace(): This article explores the performance of String’s methods split and replace. Regular expressions should be considered instead of those. Also check out String performance and Exact String Matching. * Smell my Code. Code smell ? What’s code smell?: Short, but to the point, article on code smell, i.e any symptom in the source code of a program that possibly indicates a deeper problem. Common coding smells include duplicated code, large class, feature envy and others. * Tailoring Spring for Custom Usage: This presentation uncovers some of the hooks available in the Spring framework: life cycles, scopes, beans, resources, XML marshallers, REST, transactions, caching, Spring Integration adapters, and others. * Figure out why is JAVA eating CPU?: Short guide on how to identify which Java thread is consuming the system’s CPU. It uses Java’s debugging tools (e.g. jstack) along with Linux’s built-in tools (e.g. ps, grep). * Getting out of the way – Monitoring: In this article, the author suggests that we should be working to make the monitoring happen automatically without Ops involvement. This means that Ops selects monitoring systems that allow for discovery of new metrics & automatic collection of those metrics without additional incremental work each time. Also see Devops has made Release and Deployment Cool. That’s all for this week. Stay tuned for more, here at Java Code Geeks. Cheers, Ilias Tsagklis...
java-logo

Java 7: WatchService

Of all the new features in Java 7, one of the more interesting is the WatchService, adding the capability to watch a directory for changes. The WatchService maps directly to the native file event notification mechanism, if available. If a native event notification mechanism is not available, then the default implementation will use polling. As a result, the responsiveness, ordering of events and details available are implementation specific. (NOTE: There is a companion post on using the Guava EventBus to process WatchService events) Watching A Directory The Path interface implements the register method that takes a WatchService object and varargs of type WatchEvent.Kind as arguments. There are 4 events to watch for:ENTRY_CREATE ENTRY_DELETE ENTRY_MODIFY OVERFLOWWhile the first 3 types are self explanatory, OVERFLOW indicates that events may been lost or discarded. A WatchService is created by calling FileSystem.newWatchService(). Watching a directory is accomplished by registering a Path object with the WatchService: import static java.nio.file.StandardWatchEventKinds.*; Path path = Paths.get("/home"); WatchService watchService = FileSystems.getDefault().newWatchService(); WatchKey watchKey = path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);As we can see from the example, the register method returns a WatchKey object. The WatchKey is a token that represents the registration of the Path with the WatchService. The WatchKey As a result of the registration process, the WatchKey is in a ‘ready’ state and is considered valid. A WatchKey remains valid until one of the following occurs:WatchKey.cancel() is called. The directory being watched is no longer available. The WatchService object is closed.Checking For Changes When a change is detected, the WatchKey state is set to ‘signaled’ and it is placed in a queue for processing. Getting WatchKeys off the queue involves calling WatchService.poll() or WatchService.take(). Here is a basic example: private boolean notDone = true; while(notDone){ try{ WatchKey watchKey = watchService.poll(60,TimeUnit.SECONDS); List<WatchEvent.Kind<?>> events = watchKey.pollEvents(); for(WatchEvent event : events){ ...process the events } if(!watchKey.reset()){ ...handle situation no longer valid } }catch(InterruptedException e){ Thread.currentThread().interrupt(); }On line 5 we are calling the pollEvents method to retrieve all the events for this WatchKey object. On line 9 you’ll notice a call to the reset method. The reset method sets the WatchKey state back to ‘ready’ and returns a boolean indicating if the WatchKey is still valid. If there are any pending events, then the WatchKey will be immediately re-queued, otherwise it will remain in the ready state until new events are detected. Calling reset on a WatchKey that has been cancelled or is in the ready state will have no effect. If a WatchKey is canceled while it is queued, it will reamin in the queue until retrieved. Cancellation could also happen automatically if the directory was deleted or is no longer available. Processing Events Now that we have detected an event, how do we determine:On which directory did the event happen? (assuming more than one directory registered) What was the actual event? (assuming listening for more than one event) What was the target of the event, i.e what Path object was created,deleted or updated?Jumping in to line 6 in the previous example, we will parse the needed information from a WatchKey and a WatchEvent: //WatchKey watchable returns the calling Path object of Path.register Path watchedPath = (Path) watchKey.watchable(); //returns the event type StandardWatchEventKinds eventKind = event.kind(); //returns the context of the event Path target = (Path)event.context();On line 6 we see the WatchEvent.context method being invoked. The context method will return a Path object if the event was a creation, delete or update and will be relative to the watched directory. It’s important to know that when a event is received there is no guarantee that the program(s) performing the operation have completed, so some level of coordination may be required. Conclusion The WatchService is a very interesting feature of the new java.nio.file package in Java 7. That being said, there are two things that about the WatchService to keep in mind:The WatchService does not pick up events for sub-directories of a watched directory. We still need to poll the WatchService for events, rather than receive asynchronous notification.To address the above issues, there is a follow up post using the Guava EventBus to process the WatchService events. Thanks for your time and see you in the next post.Resourcesjava.nio.file package that contains the WatchService, WatchKey and WatchEvent objects discussed here. A unit test demonstrating the WatchServiceReference: What’s New in Java 7: WatchService from our JCG partner Bill Bejeck at the Random Thoughts On Coding blog....
java-logo

Is Java’s String Class a God Object?

In October I wrote a blog entitled Top Trumps in God Objects where I talked about the discovery of an object I’d found with 167 disparate methods that linked this object with all other parts of the application and, as you’d expect, followed the general criteria for a God or Monster Object. It was recently pointed out to me that method count alone isn’t an indication that an object is a God Object, which is quite true. The comment also went on to site the String class as an object that isn’t a God Object, but has a large number of methods. This got me thinking: is the String class a well designed class in terms of the general rules of Object Oriented Design? Does it, in fact, break the single responsibility principle or the laws of demeter and is it tightly coupled to all other parts of an application? Is it a God Object? The easy way to answer this is to look at a String in terms of the Single Responsibility Principle, and the best way to do that is to count the number of method calls it has and figure out what they do. If you do that you’ll find that Java’s String class has 66 method calls in it’s repertoire and you may conclude that a number this high could be the first sign of a God Object. Next, if you look at these 66 method calls you can group them into several different areas of responsibility. For example there are: Conversion MethodsgetBytes(...) getChars(...) valueOf(...) toUpperCase(...) toLowerCase(...) toCharArray(...)Methods that split Stringsubstring(...) split(...) subSequence(...)Equivalency Methodsequals(...) matches(...) compareTo(...)Search MethodsindexOf(..) lastIndexOf(...) charAt(...) contains(...)Combining Methodsappend(...) concat(..) + +=…and more besides. Using the lists above, you could now write a CRC card for a String that goes something like this: “A String is responsible for combining itself with other Strings to create new Strings AND converting itself into other objects or arrays of objects AND splitting itself into different pieces AND comparing itself to other String objects AND performing searches on itself” Sticking to the letter of the law then the lists above show that String’s method calls break down into at least five areas of responsibility, which means that, by the book, the Single Responsibility Principle has been violated. Taking this to its logical conclusion, then you could refactor a String to look something like this….However, and this is the question, why isn’t Java’s String class composed of some data and a bunch of classes that operate on it? The answer is both a matter of opinion and multifaceted. Firstly, history… Java as grown over time as so has the String class; methods have been added piece-meal as the Java version number ticks ever upwards. Secondly, pragmatism. A String object in it’s current design may break the Single Responsibility Rule, but it doesn’t matter, it’s easier to leave it as it is and get on with writing the programs that count. Lastly, and I think that this is important, we all generally know what a String is. Its name and meaning is in common usage. If we had to define what a String’s responsible for using a CRC card, then we’d write something like: “a String is responsible for being s String”. A String may have 66 methods, but I believe that by general consensus it’s not a God Object. However, software is just a matter of opinions, so I’ll leave you answer the other questions I posed at the beginning of this blog and to make your own mind up… Reference: Is Java’s String Class a God Object? from our JCG partner Roger Hughes at the Captain Debug’s Blog ....
spring-logo

Ajax with Spring MVC 3 using Annotations and JQuery

Its always been fun for me to work with Ajax! Is not it ? I will make it easy for you to use Ajax with Spring MVC 3 and JQuery. This post will illustrate you how to use Ajax in real life practices of industrial coding. As usual, we will take an practical example of Ajax in Spring MVC 3 framework and will implement it and I will make the implementation easy by make you understand the topic. Let us see what is our example’s requirement and how Spring MVC 3 Ajax facility will fulfill it : In our example, we will make a list of students with name and highest education level, to send the list to the placement office so that the students can get chance. We will make the “Add Student Form” available to the student online so that they can submit their name online and get registered. As a lot of students will use the system, so the performance of the system may very much low. To increase to performance of the web application we will use Ajax with Spring MVC 3 Framework and JQuery. Following steps we have to go through to implement our example :First of all, we will create a domain class (User.java) that will hold the value of student information. After that we will create our controller class (UserListController.java) to handle HTTP request. Our controller will handle three types of requests. First, to show the “Add Student Form”, second to handle Ajax request came from “Add Student Form” and add the students to a list, third to show the student information as a list. Then, we will create jsp page (AddUser.jsp) to show “Add Student Form” that will use JQuery to send Ajax request to the Spring MVC Controller. The jsp will also confirm to the user that Student has been added to the list. Then, we will create a jsp (ShowUsers.jsp) that will list all users in the list.User.java User.java has two properties name and education to store the student information. Following is the code of User.java : package com.raistudies.domain;public class User {private String name = null; private String education = null; // Getter and Setter are omitted for making the code short }UserListController.java Controllers has three method to handle three request urls. “showForm” method handle the request for showing the form to the user. Bellow code shows the UserListController.java : package com.raistudies.controllers;import java.util.ArrayList; import java.util.List;import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;import com.raistudies.domain.User;@Controller public class UserListController { private List<User> userList = new ArrayList<User>();@RequestMapping(value="/AddUser.htm",method=RequestMethod.GET) public String showForm(){ return "AddUser"; }@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST) public @ResponseBody String addUser(@ModelAttribute(value="user") User user, BindingResult result ){ String returnText; if(!result.hasErrors()){ userList.add(user); returnText = "User has been added to the list. Total number of users are " + userList.size(); }else{ returnText = "Sorry, an error has occur. User has not been added to list."; } return returnText; }@RequestMapping(value="/ShowUsers.htm") public String showUsers(ModelMap model){ model.addAttribute("Users", userList); return "ShowUsers"; } }“addUsers” is same as the controller method that handle form expect that it also contain annotation @ResponseBody, which tells Spring MVC that the String returned by the method is the response to the request, it does not have to find view for this string. So the retuning String will be send back to the browser as response and hence the Ajax request will work. “showUsers” method is used to show the list of the students to the user. AddUser.jsp AddUser.jsp contain a simple form to collect information about the student and uses JQerey JavaScript framework to generate Ajax request to the server. Following is the code in AddUser.jsp : <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Add Users using ajax</title> <script src="/AjaxWithSpringMVC2Annotations/js/jquery.js"></script> <script type="text/javascript"> function doAjaxPost() { // get the form values var name = $('#name').val(); var education = $('#education').val();$.ajax({ type: "POST", url: "/AjaxWithSpringMVC2Annotations/AddUser.htm", data: "name=" + name + "&education=" + education, success: function(response){ // we have the response $('#info').html(response); $('#name').val(''); $('#education').val(''); }, error: function(e){ alert('Error: ' + e); } }); } </script> </head> <body> <h1>Add Users using Ajax ........</h1> <table> <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr> <tr><td>Education : </td><td> <input type="text" id="education"><br/></td></tr> <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr> <tr><td colspan="2"><div id="info" style="color: green;"></div></td></tr> </table> <a href="/AjaxWithSpringMVC2Annotations/ShowUsers.htm">Show All Users</a> </body> </html>You may be little bit confused if you are not aware of JQuery. Here is the explanation of the JQuery code :var name = $(‘#name’).val(); : – here the $ is JQuery selector that uses to select any node in HTML whose identifier is passed as argument. If the identifier is a prefix with # that means it is a id of the HTML node. Here, $(‘#name’).val() contains the value of the HTML node whose is “name’. The text box in which user will enter her/his name is with is as name. so java script variable name will contain the name of the user. $.ajax() :- It is the method in $ variable of JQuery to call Ajax. It has five arguments here. First of all “type” which indicated the request type of Ajax. It can be POST or GET. Then, “url” which indicates the url to be hit of Ajax submission. “data” will contain the raw data to be sent to the server. “success” will contain the function code that has to be call if the request get success and server sends an response to the browser. “error” will contain the function code that has to be call if the request get any error. $(‘#info’).html(response); :- will set the response of the server in to the div. In this way “Hello” + name will be shown in the div whose id is “info“.ShowUsers.jsp Following are the code in ShowUsers.jsp to print all student information from a ArrayList to jsp page : <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Users Added using Ajax</title> </head> <body style="color: green;"> The following are the users added in the list :<br> <ul> <c:forEach items="${Users}" var="user"> <li>Name : <c:out value="${user.name}" />; Education : <c:out value="${user.education}"/> </c:forEach> </ul> </body> </html>Here, we have used JSTL core taglib to iterate through the ArrayList and show every value in browser.<c:forEach items=”${Users}” var=”user”> : tag is used for iterate through the ArrayList. Property “items” is used to define the bean on which the List object has been stored, so items=”${Users}” says that the users list is present in “Users” bean. “var” attribute says the name of the variable in which each user will be stored. <c:out value=”${user.name}” /> : As, a single user will be stored in variable name “user” so to print the name property in User object we use ${user.name}.app-config.xml Our Spring MVC configuration file should be able to handle annotation driven controllers. The configuration are as follows : <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- Scans the classpath of this application for @Components to deploy as beans --> <context:component-scan base-package="com.raistudies" /><!-- Configures the @Controller programming model --> <mvc:annotation-driven /><!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>Deploy the war file to tomcat 6 and hit the url in browser, you will get following page displayed :Ajax Form Using Spring MVC 3Fill student information :Ajax Filled Form Using Spring MVC 3After that click on “Add Users” button, you will get message that user has been added to the list :Ajax Form Submission Confirmation Spring MVC 3To show all student added to the list click on the button “Show All Users”, you will get following page :Show All Users Spring MVC 3That is all from Ajax using Spring MVC 3 and JQuery. You can download source from bellow link. Source : Download Reference: Ajax with Spring MVC 3 using Annotations and JQuery from our JCG partner Rahul Mondal at the Rai Studies blog....
scala-logo

Student Questions about Scala, Part 1

Preface I’m currently teaching a course on Applied Text Analysis and am using Scala as the programming language taught and used in the course. Rather than creating more tutorials, I figured I’d take a page from Brian Dunning’s playbook on his Skeptoid podcast (highly recommended) when he takes student questions. So, I had the students in the course submit questions about Scala that they had, based on the readings and assignments thus far. This post covers over half of them — the rest will be covered in a follow up post. I start with some of the more basic questions, and the questions and/or answers progressively get into more intermediate level topics. Suggestions and comments to improve any of the answers are very welcome! Basic Questions Q. Concerning addressing parts of variables: To address individual parts of lists, the numbering of the items is (List 0,1,2 etc.) That is, the first element is called “0?. It seems to be the same for Arrays and Maps, but not for Tuples- to get the first element of a Tuple, I need to use Tuple._1. Why is that? A. It’s just a matter of convention — tuples have used a 1-based index in other languages like Haskell, and it seems that Scala has adopted the same convention/tradition. See: http://stackoverflow.com/questions/6241464/why-are-the-indexes-of-scala-tuples-1-based Q. It seems that Scala doesn’t recognize the “b” boundary character as a regular expression. Is there something similar in Scala? A. Scala does recognize boundary characters. For example, the following REPL session declares a regex that finds “the” with boundaries, and successfully retrieves the three tokens of “the” in the example sentence. scala> val TheRE = """\bthe\b""".r TheRE: scala.util.matching.Regex = \bthe\bscala> val sentence = "She think the man is a stick-in-the-mud, but the man disagrees." sentence: java.lang.String = She think the man is a stick-in-the-mud, but the man disagrees.scala> TheRE.findAllIn(sentence).toList res1: List[String] = List(the, the, the)Q. Why doesn’t the method “split” work on args? Example: val arg = args.split(” “). Args are strings right, so split should work? A. The args variable is an Array, so split doesn’t work on them. Arrays are, in effect, already split. Q. What is the major difference between foo.mapValues(x=>x.length) and foo.map(x=>x.length). Some places one works and one does not. A. The map function works on all sequence types, including Seqs and Maps (note that Maps can be seen as sequences of Tuple2s). The mapValues function, however, only works on Maps. It is essentially a convenience function. As an example, let’s start with a simple Map from Ints to Ints. scala> val foo = List((1,2),(3,4)).toMap foo: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)Now consider the task of adding 2 to each value in the Map. This can be done with the map function as follows. scala> foo.map { case(key,value) => (key,value+2) } res5: scala.collection.immutable.Map[Int,Int] = Map(1 -> 4, 3 -> 6)So, the map function iterates over key/value pairs. We need to match both of them, and then output the key and the changed value to create the new Map. The mapValues function makes this quite a bit easier. scala> foo.mapValues(2+) res6: scala.collection.immutable.Map[Int,Int] = Map(1 -> 4, 3 -> 6)Returning to the question about computing the length using mapValues or map — then it is just a question of which values you are transforming, as in the following examples. scala> val sentence = "here is a sentence with some words".split(" ").toList sentence: List = List(here, is, a, sentence, with, some, words)scala> sentence.map(_.length) res7: List[Int] = List(4, 2, 1, 8, 4, 4, 5)scala> val firstCharTokens = sentence.groupBy(x=>x(0)) firstCharTokens: scala.collection.immutable.Map[Char,List] = Map(s -> List(sentence, some), a -> List(a), i -> List(is), h -> List(here), w -> List(with, words))scala> firstCharTokens.mapValues(_.length) res9: scala.collection.immutable.Map[Char,Int] = Map(s -> 2, a -> 1, i -> 1, h -> 1, w -> 2)Q. Is there any function that splits a list into two lists with the elements in the alternating positions of the original list? For example, MainList =(1,2,3,4,5,6) List1 = (1,3,5) List2 = (2,4,6) A. Given the exact main list you provided, one can use the partition function and use the modulo operation to see whether the value is divisible evenly by 2 or not. scala> val mainList = List(1,2,3,4,5,6) mainList: List[Int] = List(1, 2, 3, 4, 5, 6)scala> mainList.partition(_ % 2 == 0) res0: (List[Int], List[Int]) = (List(2, 4, 6),List(1, 3, 5))So, partition returns a pair of Lists. The first has all the elements that match the condition and the second has all the ones that do not. Of course, this wouldn’t work in general for Lists that have Strings, or that don’t have Ints in order, etc. However, the indices of a List are always well-behaved in this way, so we just need to do a bit more work by zipping each element with its index and then partitioning based on indices. scala> val unordered = List("b","2","a","4","z","8") unordered: List = List(b, 2, a, 4, z, 8)scala> unordered.zipWithIndex res1: List[(java.lang.String, Int)] = List((b,0), (2,1), (a,2), (4,3), (z,4), (8,5))scala> val (evens, odds) = unordered.zipWithIndex.partition(_._2 % 2 == 0) evens: List[(java.lang.String, Int)] = List((b,0), (a,2), (z,4)) odds: List[(java.lang.String, Int)] = List((2,1), (4,3), (8,5))scala> evens.map(_._1) res2: List = List(b, a, z)scala> odds.map(_._1) res3: List = List(2, 4, 8)Based on this, you could of course write a function that does this for any arbitrary list. Q. How to convert a List to a Vector and vice-versa? A. Use toIndexSeq and toList. scala> val foo = List(1,2,3,4) foo: List[Int] = List(1, 2, 3, 4)scala> val bar = foo.toIndexedSeq bar: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)scala> val baz = bar.toList baz: List[Int] = List(1, 2, 3, 4)scala> foo == baz res0: Boolean = trueQ. The advantage of a vector over a list is the constant time look-up. What is the advantage of using a list over a vector? A. A List is slightly faster for operations at the head (front) of the sequence, so if all you are doing is doing a traversal (accessing each element in order, e.g. when mapping), then Lists are perfectly adequate and may be more efficient. They also have some nice pattern matching behavior for case statements. However, common wisdom seems to be that you should default to using Vectors. See Daniel Spiewak’s nice answer on Stackoverflow: http://stackoverflow.com/questions/6928327/when-should-i-choose-vector-in-scala Q. With splitting strings, holmes.split(“\\s”) – \n and \t just requires a single ‘\’ to recognize its special functionality but why two ‘\’s are required for white space character? A. That’s because \n and \t actually mean something in a String. scala> println("Here is a line with a tab\tor\ttwo, followed by\na new line.") Here is a line with a tab or two, followed by a new line.scala> println("This will break\s.") <console>:1: error: invalid escape character println("This will break\s.")So, you are supplying a String argument to split, and it uses that to construct a regular expression. Given that \s is not a string character, but is a regex metacharacter, you need to escape it. You can of course use split(“””\s”””), though that isn’t exactly better in this case. Q. I have long been programming in C++ and Java. Therefore, I put semicolon at the end of the line unconsciously. It seems that the standard coding style of Scala doesn’t recommend to use semicolons. However, I saw that there are some cases that require semicolons as you showed last class. Is there any specific reason why semicolon loses its role in Scala? A. The main reason is to improve readability since the semicolon is rarely needed when writing standard code in editors (as opposed to one liners in the REPL). However, when you want to do something in a single line, like handling multiple cases, you need the semicolons. scala> val foo = List("a",1,"b",2) foo: List[Any] = List(a, 1, b, 2)scala> foo.map { case(x: String) => x; case(x: Int) => x.toString } res5: List[String] = List(a, 1, b, 2)But, in general, it’s best to just split these cases over multiple lines in any actual code. Q. Is there no way to use _ in map like methods for collections that consist of pairs? For example, List((1,1),(2,2)).map(e => e._1 + e._2) works, but List((1,1),(2,2)).map(_._1 + _._2) does not work. A. The scope in which the _ remains unanambigious runs out past its first invocation, so you only get to use it once. It is better anyway to use a case statement that makes it clear what the members of the pairs are. scala> List((1,1),(2,2)).map { case(num1, num2) => num1+num2 } res6: List[Int] = List(2, 4)Q. I am unsure about the exact meaning of and the difference between “=>” and “->”. They both seem to mean something like “apply X to Y” and I see that each is used in a particular context, but what is the logic behind that? A. The use of -> simply constructs a Tuple2, as is pretty clear in the following snippet. scala> val foo = (1,2) foo: (Int, Int) = (1,2)scala> val bar = 1->2 bar: (Int, Int) = (1,2)scala> foo == bar res11: Boolean = truePrimarily, it is syntactic sugar that provides an intuitive symbol for creating elements of a a Map. Compare the following two ways of declaring the same Map. scala> Map(("a",1),("b",2)) res9: scala.collection.immutable.Map = Map(a -> 1, b -> 2)scala> Map("a"->1,"b"->2) res10: scala.collection.immutable.Map = Map(a -> 1, b -> 2)The second seems more readable to me. The use of => indicates that you are defining a function. The basic form is ARGUMENTS => RESULT. scala> val addOne = (x: Int) => x+1 addOne: Int => Int = <function1>scala> addOne(2) res7: Int = 3scala> val addTwoNumbers = (num1: Int, num2: Int) => num1+num2 addTwoNumbers: (Int, Int) => Int = <function2>scala> addTwoNumbers(3,5) res8: Int = 8Normally, you use it in defining anonymous functions as arguments to functions like map, filter, and such. Q. Is there a more convenient way of expressing vowels as [AEIOUaeiou] and consonants as [BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz] in RegExes? A. You can use Strings when defining regexes, so you can have a variable for vowels and one for consonants. scala> val vowel = "[AEIOUaeiou]" vowel: java.lang.String = [AEIOUaeiou]scala> val consonant = "[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz]" consonant: java.lang.String = [BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz]scala> val MyRE = ("("+vowel+")("+consonant+")("+vowel+")").r MyRE: scala.util.matching.Regex = ([AEIOUaeiou])([BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz])([AEIOUaeiou])scala> val MyRE(x,y,z) = "aJE" x: String = a y: String = J z: String = EQ. The “\b” in RegExes marks a boundary, right? So, it also captures the “-”. But if I have a single string “sdnfeorgn”, it does NOT capture the boundaries of that, is that correct? And if so, why doesn’t it? A. Because there are no boundaries in that string! Intermediate questions Q. The flatMap function takes lists of lists and merges them to single list. But in the example: scala> (1 to 10).toList.map(x=>squareOddNumber(x)) res16: List[Option[Int]] = List(Some(1), None, Some(9), None, Some(25), None, Some(49), None, Some(81), None)scala> (1 to 10).toList.flatMap(x=>squareOddNumber(x)) res17: List[Int] = List(1, 9, 25, 49, 81)Here it is not list of list but just a list. In this case it expects the list to be Option list. I tried running the code with function returning just number or None. It showed error. So is there any way to use flatmap without Option lists and just list. For example, List(1, None, 9, None, 25) should be returned as List(1, 9, 25). A. No, this won’t work because List(1, None, 9, None, 25) mixes Options with Ints. scala> val mixedup = List(1, None, 9, None, 25) mixedup: List[Any] = List(1, None, 9, None, 25)So, you should have your function return an Option which means returning Somes or Nones. Then flatMap will work happily. One way of think of Options is that they are like Lists with zero or one element, as can be noted by the parallels in the following snippet. scala> val foo = List(List(1),Nil,List(3),List(6),Nil) foo: List[List[Int]] = List(List(1), List(), List(3), List(6), List())scala> foo.flatten res12: List[Int] = List(1, 3, 6)scala> val bar = List(Option(1),None,Option(3),Option(6),None) bar: List[Option[Int]] = List(Some(1), None, Some(3), Some(6), None)scala> bar.flatten res13: List[Int] = List(1, 3, 6)Q. Does scala have generic templates (like C++, Java)? eg. in C++, we can use vector<int>, vector<string> etc. Is that possible in scala? If so, how? A. Yes, every collection type is parameterized. Notice that each of the following variables is parameterized by the type of the elements they are initialized with. scala> val foo = List(1,2,3) foo: List[Int] = List(1, 2, 3)scala> val bar = List("a","b","c") bar: List = List(a, b, c)scala> val baz = List(true, false, true) baz: List[Boolean] = List(true, false, true)You can create your own parameterized classes straightforwardly. scala> class Flexible[T] (val data: T) defined class Flexiblescala> val foo = new Flexible(1) foo: Flexible[Int] = Flexible@7cd0570escala> val bar = new Flexible("a") bar: Flexible = Flexible@31b6956fscala> val baz = new Flexible(true) baz: Flexible[Boolean] = Flexible@5b58539fscala> foo.data res0: Int = 1scala> bar.data res1: java.lang.String = ascala> baz.data res2: Boolean = trueQ. How can we easily create, initialize and work with multi-dimensional arrays (and dictionaries)? A. Use the fill function of the Array object to create them. scala> Array.fill(2)(1.0) res8: Array[Double] = Array(1.0, 1.0)scala> Array.fill(2,3)(1.0) res9: Array[Array[Double]] = Array(Array(1.0, 1.0, 1.0), Array(1.0, 1.0, 1.0))scala> Array.fill(2,3,2)(1.0) res10: Array[Array[Array[Double]]] = Array(Array(Array(1.0, 1.0), Array(1.0, 1.0), Array(1.0, 1.0)), Array(Array(1.0, 1.0), Array(1.0, 1.0), Array(1.0, 1.0)))Once you have these in hand, you can iterate over them as usual. scala> val my2d = Array.fill(2,3)(1.0) my2d: Array[Array[Double]] = Array(Array(1.0, 1.0, 1.0), Array(1.0, 1.0, 1.0))scala> my2d.map(row => row.map(x=>x+1)) res11: Array[Array[Double]] = Array(Array(2.0, 2.0, 2.0), Array(2.0, 2.0, 2.0))For dictionaries (Maps), you can use mutable HashMaps to create an empty Map and then add elements to it. For that, see this blog post: http://bcomposes.wordpress.com/2011/09/19/first-steps-in-scala-for-beginning-programmers-part-8/ Q. Is the apply function similar to constructor in C++, Java? Where will the apply function be practically used? Is it for intialising values of attributes? A. No, the apply function is like any other function except that it allows you to call it without writing out “apply”. Consider the following class. class AddX (x: Int) { def apply(y: Int) = x+y override def toString = "My number is " + x }Here’s how we can use it. scala> val add1 = new AddX(1) add1: AddX = My number is 1scala> add1(4) res0: Int = 5scala> add1.apply(4) res1: Int = 5scala> add1.toString res2: java.lang.String = My number is 1So, the apply method is just (very handy) syntactic sugar that allows you to specify one function as fundamental to a class you have designed (actually, you can have multiple apply methods as long as each one has a unique parameter list). For example, with Lists, the apply method returns the value at the index provided, and for Maps it returns the value associated with the given key. scala> val foo = List(1,2,3) foo: List[Int] = List(1, 2, 3)scala> foo(2) res3: Int = 3scala> foo.apply(2) res4: Int = 3scala> val bar = Map(1->2,3->4) bar: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)scala> bar(1) res5: Int = 2scala> bar.apply(1) res6: Int = 2Q. In the SBT tutorial you discuss “Node” and “Value” as being case classes. What is the alternative to a case class? A. A normal class. Case classes are the special case. They do two things (and more) for you. The first is that you don’t have to use “new” to create a new object. Consider the following otherwise identical classes. scala> class NotACaseClass (val data: Int) defined class NotACaseClassscala> case class IsACaseClass (val data: Int) defined class IsACaseClassscala> val foo = new NotACaseClass(4) foo: NotACaseClass = NotACaseClass@a5c0f8fscala> val bar = IsACaseClass(4) bar: IsACaseClass = IsACaseClass(4)That may seem like a little thing, but it can significantly improve code readability. Consider creating Lists within Lists within Lists if you had to use “new” all the time, for example. This is definitely true for Node and Value, which are used to build trees. Case classes also support matching, as in the following. scala> val IsACaseClass(x) = bar x: Int = 4A normal class cannot do this. scala> val NotACaseClass(x) = foo <console>:13: error: not found: value NotACaseClass val NotACaseClass(x) = foo ^ <console>:13: error: recursive value x needs type val NotACaseClass(x) = foo ^If you mix the case class into a List and map over it, you can match it like you can with other classes, like Lists and Ints. Consider the following heterogeneous List. scala> val stuff = List(IsACaseClass(3), List(2,3), IsACaseClass(5), 4) stuff: List[Any] = List(IsACaseClass(3), List(2, 3), IsACaseClass(5), 4)We can convert this to a List of Ints by processing each element according to its type by matching. scala> stuff.map { case List(x,y) => x; case IsACaseClass(x) => x; case x: Int => x } <console>:13: warning: match is not exhaustive! missing combination * Nil * *stuff.map { case List(x,y) => x; case IsACaseClass(x) => x; case x: Int => x } ^warning: there were 1 unchecked warnings; re-run with -unchecked for details res10: List[Any] = List(3, 2, 5, 4)If you don’t want to see the warning in the REPL, add a case for things that don’t match that throws a MatchError. scala> stuff.map { case List(x,y) => x; case IsACaseClass(x) => x; case x: Int => x; case _ => throw new MatchError } warning: there were 1 unchecked warnings; re-run with -unchecked for details res13: List[Any] = List(3, 2, 5, 4)Better yet, return Options (using None for the unmatched case) and flatMapping instead. scala> stuff.flatMap { case List(x,y) => Some(x); case IsACaseClass(x) => Some(x); case x: Int => Some(x); case _ => None } warning: there were 1 unchecked warnings; re-run with -unchecked for details res14: List[Any] = List(3, 2, 5, 4)Q. In C++ the default access specifier is private; in Java one needs to specify private or public for each class member where as in Scala the default access specifier for a class is public. What could be the design motivation behind this when one of the purpose of the class is data hiding? A. The reason is that Scala has a much more refined access specification scheme than Java that makes public the rational choice. See the discussion here: http://stackoverflow.com/questions/4656698/default-public-access-in-scala Another key aspecte of this is that the general emphasis in Scala is on using immutable data structures, so there isn’t any danger of someone changing the internal state of your objects if you have designed them in this way. This in turn gets rid of the ridiculous getter and setter methods that breed and multiply in Java programs. See “Why getters and setters are evil” for more discussion: http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html After you get used to programming in Scala, the whole getter/setter thing that is so common in Java code is pretty much gag worthy. In general, it is still a good idea to use private[this] as a modifier to methods and variables whenever they are only needed by an object itself. Q. How do we define overloaded constructors in Scala? Q. The way a class is defined in Scala introduced in the tutorial, seems to have only one constructor. Is there any way to provide multiple constructors like Java? A. You can add additional constructors with this declarations. class SimpleTriple (x: Int, y: Int, z: String) { def this (x: Int, z: String) = this(x,0,z) def this (x: Int, y: Int) = this(x,y,"a") override def toString = x + ":" + y + ":" + z }scala> val foo = new SimpleTriple(1,2,"hello") foo: SimpleTriple = 1:2:helloscala> val bar = new SimpleTriple(1,"goodbye") bar: SimpleTriple = 1:0:goodbyescala> val baz = new SimpleTriple(1,3) baz: SimpleTriple = 1:3:aNotice that you must supply an initial value for every one of the parameters of the class. This contrasts with Java, which allows you to leave some fields uninitialized (and which tends to lead to nasty bugs and bad design). Note that you can also provide defaults to parameters. class SimpleTripleWithDefaults (x: Int, y: Int = 0, z: String = "a") { override def toString = x + ":" + y + ":" + z }scala> val foo = new SimpleTripleWithDefaults(1) foo: SimpleTripleWithDefaults = 1:0:ascala> val bar = new SimpleTripleWithDefaults(1,2) bar: SimpleTripleWithDefaults = 1:2:aHowever, you can’t omit a middle parameter while specifying the last one. scala> val foo = new SimpleTripleWithDefaults(1,"xyz") <console>:12: error: type mismatch; found : java.lang.String("xyz") required: Int Error occurred in an application involving default arguments. val foo = new SimpleTripleWithDefaults(1,"xyz") ^But, you can name the parameters in the initialization if you want to be able to do this. scala> val foo = new SimpleTripleWithDefaults(1,z="xyz") foo: SimpleTripleWithDefaults = 1:0:xyzYou then have complete freedom to change the parameters around. scala> val foo = new SimpleTripleWithDefaults(z="xyz",x=42,y=3) foo: SimpleTripleWithDefaults = 42:3:xyzQ. I’m still not clear on the difference between classes and traits. I guess I see a conceptual difference but I don’t really understand what the functional difference is — how is creating a “trait” different from creating a class with maybe fewer methods associated with it? A. Yes, they are different. First off, traits are abstract, which means you cannot create any members. Consider the following contrast. scala> class FooClass defined class FooClassscala> trait FooTrait defined trait FooTraitscala> val fclass = new FooClass fclass: FooClass = FooClass@1b499616scala> val ftrait = new FooTrait <console>:8: error: trait FooTrait is abstract; cannot be instantiated val ftrait = new FooTrait ^You can extend a trait to make a concrete class, however. scala> class FooTraitExtender extends FooTrait defined class FooTraitExtenderscala> val ftraitExtender = new FooTraitExtender ftraitExtender: FooTraitExtender = FooTraitExtender@53d26552This gets more interesting if the trait has some methods, of course. Here’s a trait, Animal, that declares two abstract methods, makeNoise and doBehavior. trait Animal { def makeNoise: String def doBehavior (other: Animal): String }We can extend this trait with new class definitions; each extending class must implement both of these methods (or else be declared abstract). case class Bear (name: String, defaultBehavior: String = "Regard warily...") extends Animal { def makeNoise = "ROAR!" def doBehavior (other: Animal) = other match { case b: Bear => makeNoise + " I'm " + name + "." case m: Mouse => "Eat it!" case _ => defaultBehavior } override def toString = name }case class Mouse (name: String) extends Animal { def makeNoise = "Squeak?" def doBehavior (other: Animal) = other match { case b: Bear => "Run!!!" case m: Mouse => makeNoise + " I'm " + name + "." case _ => "Hide!" } override def toString = name }Notice that Bear and Mouse have different parameter lists, but both can be Animals because they fully implement the Animal trait. We can now start creating objects of the Bear and Mouse classes and have them interact. We don’t need to use “new” because they are case classes (and this also allowed them to be used in the match statements of the doBehavior methods). val yogi = Bear("Yogi", "Hello!") val baloo = Bear("Baloo", "Yawn...") val grizzly = Bear("Grizzly") val stuart = Mouse("Stuart")println(yogi + ": " + yogi.makeNoise) println(stuart + ": " + stuart.makeNoise) println("Grizzly to Stuart: " + grizzly.doBehavior(stuart))We can also create a singleton object that is of the Animal type by using the following declaration. object John extends Animal { def makeNoise = "Hullo!" def doBehavior (other: Animal) = other match { case b: Bear => "Nice bear... nice bear..." case _ => makeNoise } override def toString = "John" }Here, John is an object, not a class. Because this object implements the Animal trait, it successfully extends it and can act as an Animal. This means that a Bear like baloo can interact with John. println("Baloo to John: " + baloo.doBehavior(John))The output of the above code when run as a script is the following. Yogi: ROAR! Stuart: Squeak? Grizzly to Stuart: Eat it! Baloo to John: Yawn… The closer distinction is between traits and abstract classes. In fact, everything shown above could have been done with Animal as an abstract class rather than as a trait. One difference is that an abstract class can have a constructor while traits cannot. Another key difference between them is that traits can be used to support limited multiple inheritance, as shown in the next question/answer. Q. Does Scala support multiple inheritance? A. Yes, via traits with implementations of some methods. Here’s an example, with a trait Clickable that has an abstract (unimplemented) method getMessage, an implemented method click, and a private, reassignable variable numTimesClicked (the latter two show clearly that traits are different from Java interfaces). trait Clickable { private var numTimesClicked = 0 def getMessage: String def click = { val output = numTimesClicked + ": " + getMessage numTimesClicked += 1 output } }Now let’s say we have a MessageBearer class (that we may have wanted for entirely different reasons having nothing to do with clicking). class MessageBearer (val message: String) { override def toString = message }A new class can be now created by extending MessageBearer and “mixing in” the Clickable trait. class ClickableMessageBearer(message: String) extends MessageBearer(message) with Clickable { def getMessage = message }ClickableMessageBearer now has the abilities of both MessageBearers (which is to be able to retrieve its message) and Clickables. scala> val cmb1 = new ClickableMessageBearer("I'm number one!") cmb1: ClickableMessageBearer = I'm number one!scala> val cmb2 = new ClickableMessageBearer("I'm number two!") cmb2: ClickableMessageBearer = I'm number two!scala> cmb1.click res3: java.lang.String = 0: I'm number one!scala> cmb1.message res4: String = I'm number one!scala> cmb1.click res5: java.lang.String = 1: I'm number one!scala> cmb2.click res6: java.lang.String = 0: I'm number two!scala> cmb1.click res7: java.lang.String = 2: I'm number one!scala> cmb2.click res8: java.lang.String = 1: I'm number two!Q. Why are there toString, toInt, and toList functions, but there isn’t a toTuple function? A. This is a basic question that leads directly to the more advanced topic of implicits. There are a number of reasons behind this. To start with, it is important to realize that there are many types of Tuples, starting with a Tuple with a single element (a Tuple1) up to 22 elements (a Tuple22). Note that when you use (,) to create a tuple, it is implicitly invoking a constructor for the corresponding TupleN of the correct arity. scala> val b = (1,2,3) b: (Int, Int, Int) = (1,2,3)scala> val c = Tuple3(1,2,3) c: (Int, Int, Int) = (1,2,3)scala> b==c res4: Boolean = trueGiven this, it is obviously not meaningful to have a function toTuple on Seqs (sequences) that are longer than 22. This means there is no generic way to have, say a List or Array, and then call toTuple on it and expect reliable behavior to happen. However, if you want this functionality (even though limited by the above constraint of 22 elements max), Scala allows you to “add” methods to existing classes by using implicit definitions. You can find lots of discussions about implicits by search for “scala implicits”. But, here’s an example that shows how it works for this particular case. val foo = List(1,2) val bar = List(3,4,5) val baz = List(6,7,8,9)foo.toTupleclass TupleAble[X] (elements: Seq[X]) { def toTuple = elements match { case Seq(a) => Tuple1(a) case Seq(a,b) => (a,b) case Seq(a,b,c) => (a,b,c) case _ => throw new RuntimeException("Sequence too long to be handled by toTuple: " + elements) } }foo.toTupleimplicit def seqToTuple[X](x: Seq[X]) = new TupleAble(x)foo.toTuple bar.toTuple baz.toTupleIf you put this into the Scala REPL, you’ll see that the first invocation of foo.toTuple gets an error: scala> foo.toTuple <console>:9: error: value toTuple is not a member of List[Int] foo.toTuple ^Note that class TupleAble takes a Seq in its constructor and then provides the method toTuple, using that Seq. It is able to do so for Seqs with 1, 2 or 3 elements, and above that it throws an exception. (We could of course keeping listing more cases out and go up to 22 element tuples, but this shows the point.) The second invocation of foo.toTuple still doesn’t work — and that is because foo is a List (a kind of Seq) and there isn’t a toTuple method for Lists. That’s where the implicit function seqToTuple comes in — once it is declared, Scala notes that you are trying to call toTuple on a Seq, notes that there is no such function for Seqs, but sees that there is an implicit conversion from Seqs to TupleAbles via seqToTuple, and then it sees that TupleAble has a toTuple method. Based on that, it compiles and the produces the desired behavior. This is a very handy ability of Scala that can really simplify your code if you use it well and with care. Reference: Student Questions about Scala, Part 1 from our JCG partner Jason Baldridge at the Bcomposes blog....
neo4j-logo

Flavor of the month – Neo4j and Heroku

Neo4j launched a challenge earlier this year called “Seed the Cloud” to get folks to create templates or demo applications on Heroku using the Neo4j add-on. After much internal debate, I decided to enter, only to be thrown into despair for lack of an idea. The idea came to me while I was doing nothing in particular- to build a simple app that would help one find ingredients whose flavors complement one another. Basically, you have these ingredients that pair really well together- knowing which ingredients have flavor affinities can produce some amazing new dishes. The app allows you to add pairings with an ‘affinity’- how well they pair together- and search for an ingredient to find others that pair with it. The app is built using Grails 2.0 for the front end, visualizations are the result of the very neat d3.js library, and interaction with Neo4j is done using the Neo4j REST Api. The entire thing is deployed on Heroku (with the Neo4j add-on), while the source is available on github To get started with Grails 2.0, Neo4j add-on and Heroku, read Aldrins post on the topic: http://thought-bytes.blogspot.in/2012/02/grails-20-heroku-and-neo4j-addon.html How to deploy Flavorwocky locally as well as on Heroku is documented in the Readme. The model lends itself very nicely to a graph- as you can see, it is very simple:Every category is color coded- for convenience, it is stored as a property on the category node, but of course, it doesn’t have to be. Flavorwocky was also picked as the basis for this challenge because it is a real world use case for a graph, and I wanted this entry to really focus on Neo4j primarily rather than supporting bells and whistles. Set up When the application starts up, we check if the database is empty. To do this, we perform a traversal from the reference node looking for any categories connected to it. If there are none, then we go ahead and create the set of categories by first creating their nodes, and then a relationship to the reference node. We also create the index “ingredients”, which is used to index ingredient names. Source code: https://github.com/luanne/flavorwocky/blob/master/grails-app/conf/BootStrap.groovy Add pairing: Adding a pairing involvesChecking whether either node already exists to avoid recreating it Creating both nodes and linking them to their categories in a single transaction Creating a relationship between them in the same transaction aboveTo accomplish this, we used the Batch operation (note, this is still experimental). Source: https://github.com/luanne/flavorwocky/blob/master/grails-app/controllers/com/herokuapp/flavorwocky/FlavorwockyController.groovy (fetchOrCreateNodes()) Auto complete: This was just an index lookup matching the partially entered ingredient by name. Source: https://github.com/luanne/flavorwocky/blob/master/grails-app/controllers/com/herokuapp/flavorwocky/FlavorwockyController.groovy (autosearch()) Visualization: We used d3.js to provide two visualizations for the search results. The “Explore” visualization is based on the Node-Link tree; we used a Cypher query to find all ingredients that pair with the searched ingredient up to 3 levels deep, transform it into the appropriate data structure, and render it as JSON. Note, although the visualization interactive, the fetching of data is not. The entire set of data for 3 levels is grabbed at once- a future enhancement would be fetching children only when you expand a node. Source: https://github.com/luanne/flavorwocky/blob/master/grails-app/controllers/com/herokuapp/flavorwocky/FlavorwockyController.groovy (getSearchVisualizationAsTreeJson()) Although the tree is pretty slick, one shortcoming is that it is a tree- so if children are linked to each other, then you see multiple instances of that ingredient in the tree. Hence we tried another visualization to capture the interconnections between ingredients and also surface interesting facts such as flavor trios- there’s a pretty high chance that if you see a triangle in the network visualization, those three ingredients can be combined together well. We used the Force directed graph for this. Again, a Cypher query came to the rescue (this time 5 levels deep to produce a richer model). Source: https://github.com/luanne/flavorwocky/blob/master/grails-app/controllers/com/herokuapp/flavorwocky/FlavorwockyController.groovy (getSearchVisualizationAsNetworkJson()) In both examples, the affinity of the pairing is used to calculate the length of the connector between ingredients, indicating shorter connections have ingredients that pair much better than those with longer connections. That’s about it! There’s so much more that can be done with this application, but it’s going to have to wait for a bit. Please tweet about Flavorwocky if you like it or just want me to win. Voting also helps! Reference: Flavor of the month- Neo4j add-on for Heroku & Flavor of the month- Neo4j and Heroku, part 2 from our JCG partners Aldrin & Luanne Misquitta at the Thought Bytes blog....
java-logo

Investigating the HashDoS issue

Nearly one month ago I have written some thoughts on how the HashDoS problem presented at the 28C3 or other code defects could perhaps be fixed temporarily without interaction of vendors. Now it’s time to deeper investigate the complexity attack and have at look at the sources. I quitely assume that java.util.HashMap and java.util.Hashtable are the most common used data structures of Java which are affected by this attack, so this article will only focus the code behind these types. A brief kick start to hash functions and indexed data structures Hash indexed data structures are very popular because of their simple usage and benefits:no bothering with index tables to find the right position of desired data access to data by using keywords instead of index numbers nearly constant time for add or delete operationsTo achieve these benefits hash indexed data structures follow a clever idea on how to index data. The index is computed by hashing a keyword which is associated with data behind it. Consider the following example for an easy code-like example: myHashIndexedDataStructure[hash(keyword)] = particular data Sounds perfect, but it has one major drawback: the used hash functions are in most cases not cryptograhic ones. According to Wikipedia the only mandatory characteristic for a function to call itself hash function is to “map[s] large data sets of variable length, called keys, to smaller data sets of a fixed length” In contrast to call itself a cryptographic hash function (once again, a definition taken from Wikipedia) it has to fulfill further, even much stronger, requirements: ”it is easy (but not necessarily quick) to compute the hash value for any given message it is infeasible to generate a message that has a given hash it is infeasible to modify a message without changing the hash it is infeasible to find two different messages with the same hash” To make a long story short let’s summarize what we learned and what’s to conclude with this knowledge:hash indexed data structures utilize hash functions hash functions are not necessarily collision resistant as long as they are not cryptographic ones lack of collision resistance implies means to easily compute multiple values with the same hash valueIn case of colliding keywords a hash indexed data structure needs some kind of a plan b) – a fall-back algorithm – on how to handle multiple data sets with equal hash values of the keywords . In practice are several approaches feasible:probing (shift to fixed or computable intervals) multiple hashing entry chaining (building lists of colliding entries) overriding existing entriesWhich of these strategies takes Java? At first we will inspect the code of java.util. Hashtable (only the interesting parts are presented, the rest of the code is left out for clarity reasons: public synchronized V put(K key, V value) { ... // Makes sure the key is not already in the hashtable. Entry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) %tab.length; for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) { if((e.hash == hash) && e.key.equals(key)) { V old = e.value; e.value = value; return old; } } ... // Creates the new entry. Entry<K,V> e = tab[index]; tab[index] = new Entry<>(hash, key, value, e); count++; return null; }As can be seen this class uses the hashCode() function of the key object (the keyword) to compute the hash value. It follows an ANDing (& operator), for proper representation as Integer, MODULO (% operator) the table size (building a cyclic ring structure: (table.length + 1) mod table.length ? 1, division with remainders) to always address an entry in tab[]. After this all Entry(-ies) are considered and checked if the hash values are identical and the object itself is identical. The if-clause prevents storing of multiple instances of an identical object – the older one is simply replaced by the newer. In case no identical object (concerning hash value and equals() method) was found at the current position identified by key.hashCode() a new Entry will be created, placed at the current position and handled the old Entry object at this position. So far it looks like java.util.Hashtable is using some kind of list as data structure behind each tab[]. This assumption is confirmed when looking at the code for java.util.Hashtable.Entry<K,V> which is a private inner class. private static class Entry<K,V> implements Map.Entry<K,V> { int hash; K key; V value; Entry<K,V> next;The next Entry-object simply points to the following Entry. This represents a custom built linked list. The code of java.util.HashMap is more complex and behaves partly different (null values are allowed, !unsynchronized!), but is based on the same idea. Investigating the code here will not reveal anything new, except the fact that Entry is reimplemented once again…). Both implementations rely on linked lists behind each entry of the hash indexed array. Attack idea Now that we know the implementation details behind java.util.Hashtable and java.util.HashMap we could come back to the attack referred as HashDoS. The attack implements the idea of Crosby, S.A., Wallach, D.S.: Denial of Service via Algorithmic Complexity Attacks. In: Proceedings of the 12th conference on USENIX Security Symposium – Volume 12, USENIX Association (2003) To summarize: a hash indexed data structure can be extremely slowed down by provoking an unfavorable state. The ideal hash indexed data structure looks like this: ... table[hash(keyA)] = dataA table[hash(keyB)] = dataB table[hash(keyC)] = dataC table[hash(keyD)] = dataD ...In this case time for altering, deleting or adding data with a keyword with different hash value is nearly constant. The position can easily be found by using the hash value of the keyword as index and the data is instantly present without iterating a list. Let’s have a look at a different, unfavorable state of a hash indexed data structure: ... hash(keyA) == hash(keyB) == hash(keyC) == hash(keyD) = k table[k] = dataA -> dataB -> dataC -> dataD ...With a structure like this times of constant time for CRUD operations are over… It is necessary tocompute the hash value for the keyword iterate through the linked list compare each entry’s keyword if it matches the one the application is looking forThis significantly slows down the processing thread. A very fast data structure has turned into a linked list with additional overhead (computing a hash value). All benefits of a hash indexed data structure are wiped. As if it isn’t bad enough most hash indexed data structures enable a feature called rehashing. When the data structure exceeds a defined load (in Java for example 75%) the table is rehashed for optimization reasons. Most times this feature is absolutely desirable, but in this special case it even more slows down the whole process. Exploiting the problem To exploit this behavior it is necessary to compute a whole bunch of colliding keywords. If we assume the keyword to be of type java.lang.String for example we may have a look at its hashCode() function: public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }This seems to be a customized version of a function called DJBX33A, designed by D.J. Bernstein where collisions can easily be found. The function has an interesting property which will be demonstrated on the following example: "0w".hashCode() = 1607 "1X".hashCode() = 1607 "29".hashCode() = 1607 "0w1X".hashCode() = 1545934 "0w29".hashCode() = 1545934 "1X0w".hashCode() = 1545934 "1X29".hashCode() = 1545934 "290w".hashCode() = 1545934 "291X".hashCode() = 1545934 ...We see that concatenation of colliding values results again in colliding values. We can do this further on and get a huge collection of colliding keywords. This makes finding collisions even more easy than simply bruteforcing. We tested this against a local web service and could significantly slow down the running web application server by using the colliding keywords as tag attributes. I’m uncertain if one could actually crash the machine or if there is some kind of non-obvious mechanism to prevent the server from killing itself (we have not investigated the processing code at server side), but one can definitely prevent the server from acting properly in an acceptable period of time. Requests to the web service can so easily be delayed. Maybe I will put some effort in collection measurement data (#colliding keys – system response time) in the near future. If I do, you will find the data on this blog… Corner points to take with younever rely on hashCode() alone – it can be error prone avoid things like if(password.hashCode() == referencePassword.hashCode()) {    return godMode; } else {    return simpleMode; }  spend a few seconds on implementation details when making your decision for/against a data type/structure filter incoming data – crop its size, reject overlong parameters etc. be careful and always look out for coding best practices!Further interesting points to look at In this example we used java.lang.String as keyword objects. It would be interesting what else could be used and where at the JRE code or at heavy used projects colliding hash values are used for data structuring or even worse purposes. One could have a look at how Object.hashCode() is implemented (it’s native code) – this would be a great target since all other objects extend this base class. If an extending class does not override the hashCode() function and relies on proper, collision-free output this could be useful for more sophisticated attacks. Consider what happens if serialization relies on corresponding code…. If anybody already knows some vulnerable places please let us know! We are very interested, but couldn’t look as deep as we would like to, due to lack of time. Thanks Once again I would like to thank Juraj Somorovsky for rich joint research work! Further on, our thanks go to Andrea Barisani from the oCERT team and Vincent Danen from the Red Hat Security Response Team who discussed this problem with us! Reference: Investigating the HashDoS issue from our JCG partner Christopher Meyer at the Java security and related topics blog....
enterprise-java-logo

State Does Not Belong In The Code

What is “state” in your web application? It’s the data that gets stored (regardless of the destination – memory, database, file-system). The application itself must not store any state in the code. This means your classes should only have fields with objects that are also stateless. In other words – you should not store anything in your services, DAOs or controllers during the program flow. This is a complete “must” for your service layer. Why? Your application needs to be scalable. This means it needs to be run in a cluster, and state is the hardest thing to distribute. If you minimize the places where state is stored, you minimize the complexity of clustering. But state should exist, and here is where it is fine to have it:the database – be it SQL, NoSQL or even a search engine, it’s the main thing that stores state. It is the thing that is supposed to support clustering, or a huge dedicated machine that handles requests from multiple other “code” servers. The code communicates with the database, but the code itself does not store anything for more than one client request; cache – caching is relatively easy to distribute (it’s basically key-value). There are many ready-to-use solutions like EhCache and memcached. So instead of computing a result, or getting it from the DB on each request, you can configure caching and store the result in memory. But again – code does not store anything – it just populates and queries the cache; HTTP session – in web components (controllers, managed beans, whatever you call it). It is very similar to caching, though it has a different purpose – to allow identifying subsequent actions by the same user (http itself is stateless). But as your code runs on multiple machines, the load-balancer may not always send subsequent requests to the same server. So the session should also be replicated across all servers. Fortunately, most containers have that option built-in, so you just add one configuration line. Alternatively you can instruct the load-balancer to use a “sticky session” (identify which server to send the request depending on the session cookie), but it moves some state management to the load-balancer as well. Regardless of the option you choose, do not put too much data in the session the file system – when you store files, you need them to be accessible to all machines. There are multiple options here, including SAN or using a cloud storage service like Amazon S3, which are accessible through an APIAll these are managed outside the code. Your code simply consumes them through an API (the Session API, the cache API, JDBC, S3/file system API). If the code contained any of that state (as instance-variables of your objects) the application would be hard to support (you’d have to manage state yourself) and will be less scalable. Of course, there are some rare cases, where you can’t go without storing state in the code. Document these and make sure they do not rely on working in a cluster. But what can go wrong if you store state in the objects that perform the business logic? You have two options then:synchronize access to fields – this will kill performance, because all users that make requests will have to wait in queue for the service to manage its fields; make new instance of your class for each HTTP request, and manage the instances somehow. Managing these instances is the hard part. People may be inclined to choose the session to do it, which means the session grows very large and gets harder to replicate (sharing a lot of data across multiple machines is slower, and session replication must be fast). Not to mention the unnecessarily increased memory footprint.Here’s a trivial example of what not to do. You should pass these kinds of values as method arguments, rather than storing them in the instance: class OrderService { double orderPrice;void processOrder(OrderDto order) { for (Entry entry : order.getEntries() { orderPrice += entry.getPrice(); } boolean discounts = hasDiscounts(order); } boolean hasDiscounts(OrderDto order) { return order.getEntries().length > 5 && orderPrice > 200; } }So, make all your code stateless – this will ensure at least some level of scalability. Reference: State Does Not Belong In The Code from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog....
spring-logo

Spring Thread Pool Services

Thread Pools are very important to execute synchronous & asynchronous processes. This article shows how to develop and monitor Thread Pool Services by using Spring. Creating Thread Pool has been explained via two alternative methods. Used Technologies : JDK 1.6.0_21 Spring 3.0.5 Maven 3.0.2 STEP 1 : CREATE MAVEN PROJECT A maven project is created as below. (It can be created by using Maven or IDE Plug-in).STEP 2 : LIBRARIES Spring dependencies are added to Maven’ s pom.xml. <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>For creating runnable-jar, below plugin can be used. <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.3.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.otv.exe.Application</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin>STEP 3 : CREATE TASK CLASS A new TestTask Class is created by implementing Runnable Interface. This class shows to be executed tasks. package com.otv.task; import org.apache.log4j.Logger; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public class TestTask implements Runnable { private static Logger log = Logger.getLogger(TestTask.class); String taskName; public TestTask() { } public TestTask(String taskName) { this.taskName = taskName; } public void run() { try { log.debug(this.taskName + " : is started."); Thread.sleep(10000); log.debug(this.taskName + " : is completed."); } catch (InterruptedException e) { log.error(this.taskName + " : is not completed!"); e.printStackTrace(); } } @Override public String toString() { return (getTaskName()); } public String getTaskName() { return taskName; } public void setTaskName(String taskName) { this.taskName = taskName; } }STEP 4 : CREATE TestRejectedExecutionHandler CLASS TestRejectedExecutionHandler Class is created by implementing RejectedExecutionHandler Interface. If there is no idle thread and queue overflows, tasks will be rejected. This class handles rejected tasks. package com.otv.handler; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import org.apache.log4j.Logger; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public class TestRejectedExecutionHandler implements RejectedExecutionHandler { private static Logger log = Logger.getLogger(TestRejectedExecutionHandler.class); public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) { log.debug(runnable.toString() + " : has been rejected"); } }STEP 5 : CREATE ITestThreadPoolExecutorService INTERFACE ITestThreadPoolExecutorService Interface is created. package com.otv.srv; import java.util.concurrent.ThreadPoolExecutor; import com.otv.handler.TestRejectedExecutionHandler; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public interface ITestThreadPoolExecutorService { public ThreadPoolExecutor createNewThreadPool(); public int getCorePoolSize(); public void setCorePoolSize(int corePoolSize); public int getMaxPoolSize(); public void setMaxPoolSize(int maximumPoolSize); public long getKeepAliveTime(); public void setKeepAliveTime(long keepAliveTime); public int getQueueCapacity(); public void setQueueCapacity(int queueCapacity); public TestRejectedExecutionHandler getTestRejectedExecutionHandler(); public void setTestRejectedExecutionHandler(TestRejectedExecutionHandler testRejectedExecutionHandler); }STEP 6 : CREATE TestThreadPoolExecutorService CLASS TestThreadPoolExecutorService Class is created by implementing ITestThreadPoolExecutorService Interface. This class creates a new Thread Pool. package com.otv.srv; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import com.otv.handler.TestRejectedExecutionHandler; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public class TestThreadPoolExecutorService implements ITestThreadPoolExecutorService { private int corePoolSize; private int maxPoolSize; private long keepAliveTime; private int queueCapacity; TestRejectedExecutionHandler testRejectedExecutionHandler; public ThreadPoolExecutor createNewThreadPool() { ThreadPoolExecutor executor = new ThreadPoolExecutor(getCorePoolSize(), getMaxPoolSize(), getKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue(getQueueCapacity()), getTestRejectedExecutionHandler()); return executor; } public int getCorePoolSize() { return corePoolSize; } public void setCorePoolSize(int corePoolSize) { this.corePoolSize = corePoolSize; } public int getMaxPoolSize() { return maxPoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } public long getKeepAliveTime() { return keepAliveTime; } public void setKeepAliveTime(long keepAliveTime) { this.keepAliveTime = keepAliveTime; } public int getQueueCapacity() { return queueCapacity; } public void setQueueCapacity(int queueCapacity) { this.queueCapacity = queueCapacity; } public TestRejectedExecutionHandler getTestRejectedExecutionHandler() { return testRejectedExecutionHandler; } public void setTestRejectedExecutionHandler(TestRejectedExecutionHandler testRejectedExecutionHandler) { this.testRejectedExecutionHandler = testRejectedExecutionHandler; } }STEP 7 : CREATE IThreadPoolMonitorService INTERFACE IThreadPoolMonitorService Interface is created. package com.otv.monitor.srv; import java.util.concurrent.ThreadPoolExecutor; public interface IThreadPoolMonitorService extends Runnable { public void monitorThreadPool(); public ThreadPoolExecutor getExecutor(); public void setExecutor(ThreadPoolExecutor executor); }STEP 8 : CREATE ThreadPoolMonitorService CLASS ThreadPoolMonitorService Class is created by implementing IThreadPoolMonitorService Interface. This class monitors created thread pool. package com.otv.monitor.srv; import java.util.concurrent.ThreadPoolExecutor; import org.apache.log4j.Logger; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public class ThreadPoolMonitorService implements IThreadPoolMonitorService { private static Logger log = Logger.getLogger(ThreadPoolMonitorService.class); ThreadPoolExecutor executor; private long monitoringPeriod; public void run() { try { while (true){ monitorThreadPool(); Thread.sleep(monitoringPeriod*1000); } } catch (Exception e) { log.error(e.getMessage()); } } public void monitorThreadPool() { StringBuffer strBuff = new StringBuffer(); strBuff.append("CurrentPoolSize : ").append(executor.getPoolSize()); strBuff.append(" - CorePoolSize : ").append(executor.getCorePoolSize()); strBuff.append(" - MaximumPoolSize : ").append(executor.getMaximumPoolSize()); strBuff.append(" - ActiveTaskCount : ").append(executor.getActiveCount()); strBuff.append(" - CompletedTaskCount : ").append(executor.getCompletedTaskCount()); strBuff.append(" - TotalTaskCount : ").append(executor.getTaskCount()); strBuff.append(" - isTerminated : ").append(executor.isTerminated()); log.debug(strBuff.toString()); } public ThreadPoolExecutor getExecutor() { return executor; } public void setExecutor(ThreadPoolExecutor executor) { this.executor = executor; } public long getMonitoringPeriod() { return monitoringPeriod; } public void setMonitoringPeriod(long monitoringPeriod) { this.monitoringPeriod = monitoringPeriod; } }STEP 9 : CREATE Starter CLASS Starter Class is created. package com.otv.start; import java.util.concurrent.ThreadPoolExecutor; import org.apache.log4j.Logger; import com.otv.handler.TestRejectedExecutionHandler; import com.otv.monitor.srv.IThreadPoolMonitorService; import com.otv.monitor.srv.ThreadPoolMonitorService; import com.otv.srv.ITestThreadPoolExecutorService; import com.otv.srv.TestThreadPoolExecutorService; import com.otv.task.TestTask; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public class Starter { private static Logger log = Logger.getLogger(TestRejectedExecutionHandler.class); IThreadPoolMonitorService threadPoolMonitorService; ITestThreadPoolExecutorService testThreadPoolExecutorService; public void start() { // A new thread pool is created... ThreadPoolExecutor executor = testThreadPoolExecutorService.createNewThreadPool(); executor.allowCoreThreadTimeOut(true); // Created executor is set to ThreadPoolMonitorService... threadPoolMonitorService.setExecutor(executor); // ThreadPoolMonitorService is started... Thread monitor = new Thread(threadPoolMonitorService); monitor.start(); // New tasks are executed... for(int i=1;i<10;i++) { executor.execute(new TestTask("Task"+i)); } try { Thread.sleep(40000); } catch (Exception e) { log.error(e.getMessage()); } for(int i=10;i<19;i++) { executor.execute(new TestTask("Task"+i)); } // executor is shutdown... executor.shutdown(); } public IThreadPoolMonitorService getThreadPoolMonitorService() { return threadPoolMonitorService; } public void setThreadPoolMonitorService(IThreadPoolMonitorService threadPoolMonitorService) { this.threadPoolMonitorService = threadPoolMonitorService; } public ITestThreadPoolExecutorService getTestThreadPoolExecutorService() { return testThreadPoolExecutorService; } public void setTestThreadPoolExecutorService(ITestThreadPoolExecutorService testThreadPoolExecutorService) { this.testThreadPoolExecutorService = testThreadPoolExecutorService; } }STEP 10 : CREATE Application CLASS Application Class is created. This class runs the application. package com.otv.start; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author onlinetechvision.com * @since 17 Oct 2011 * @version 1.0.0 * */ public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Starter starter = (Starter) context.getBean("Starter"); starter.start(); } }STEP 11 : CREATE applicationContext.xml applicationContext.xml is created. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Beans Declaration --> <bean id="TestTask" class="com.otv.task.TestTask"></bean> <bean id="ThreadPoolMonitorService" class="com.otv.monitor.srv.ThreadPoolMonitorService"> <property name="monitoringPeriod" value="5" /> </bean> <bean id="TestRejectedExecutionHandler" class="com.otv.handler.TestRejectedExecutionHandler"></bean> <bean id="TestThreadPoolExecutorService" class="com.otv.srv.TestThreadPoolExecutorService"> <property name="corePoolSize" value="1" /> <property name="maxPoolSize" value="3" /> <property name="keepAliveTime" value="10" /> <property name="queueCapacity" value="3" /> <property name="testRejectedExecutionHandler" ref="TestRejectedExecutionHandler" /> </bean> <bean id="Starter" class="com.otv.start.Starter"> <property name="threadPoolMonitorService" ref="ThreadPoolMonitorService" /> <property name="testThreadPoolExecutorService" ref="TestThreadPoolExecutorService" /> </bean> </beans>STEP 12 : ALTERNATIVE METHOD TO CREATE THREAD POOL ThreadPoolTaskExecutor Class provided by Spring can also be used to create Thread Pool. <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="1" /> <property name="maxPoolSize" value="3" /> <property name="queueCapacity" value="3" /> </bean> <bean id="testTaskExecutor" class="TestTaskExecutor"> <constructor-arg ref="threadPoolTaskExecutor" /> </bean>STEP 13 : BUILD PROJECT After OTV_Spring_ThreadPool Project is build, OTV_Spring_ThreadPool-0.0.1-SNAPSHOT.jar will be created. STEP 14 : RUN PROJECT After created OTV_Spring_ThreadPool-0.0.1-SNAPSHOT.jar file is run, below output logs will be shown : 18.10.2011 20:08:48 DEBUG (TestRejectedExecutionHandler.java:19) - Task7 : has been rejected 18.10.2011 20:08:48 DEBUG (TestRejectedExecutionHandler.java:19) - Task8 : has been rejected 18.10.2011 20:08:48 DEBUG (TestRejectedExecutionHandler.java:19) - Task9 : has been rejected 18.10.2011 20:08:48 DEBUG (TestTask.java:25) - Task1 : is started. 18.10.2011 20:08:48 DEBUG (TestTask.java:25) - Task6 : is started. 18.10.2011 20:08:48 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 2 - CompletedTaskCount : 0 - TotalTaskCount : 5 - isTerminated : false 18.10.2011 20:08:48 DEBUG (TestTask.java:25) - Task5 : is started. 18.10.2011 20:08:53 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 0 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:08:58 DEBUG (TestTask.java:27) - Task6 : is completed. 18.10.2011 20:08:58 DEBUG (TestTask.java:27) - Task1 : is completed. 18.10.2011 20:08:58 DEBUG (TestTask.java:25) - Task3 : is started. 18.10.2011 20:08:58 DEBUG (TestTask.java:25) - Task2 : is started. 18.10.2011 20:08:58 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 2 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:08:58 DEBUG (TestTask.java:27) - Task5 : is completed. 18.10.2011 20:08:58 DEBUG (TestTask.java:25) - Task4 : is started. 18.10.2011 20:09:03 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 3 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:09:08 DEBUG (TestTask.java:27) - Task2 : is completed. 18.10.2011 20:09:08 DEBUG (TestTask.java:27) - Task3 : is completed. 18.10.2011 20:09:08 DEBUG (TestTask.java:27) - Task4 : is completed. 18.10.2011 20:09:08 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 0 - CompletedTaskCount : 6 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:09:13 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 0 - CompletedTaskCount : 6 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:09:18 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 0 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 0 - CompletedTaskCount : 6 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:09:23 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 0 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 0 - CompletedTaskCount : 6 - TotalTaskCount : 6 - isTerminated : false 18.10.2011 20:09:28 DEBUG (TestTask.java:25) - Task10 : is started. 18.10.2011 20:09:28 DEBUG (TestRejectedExecutionHandler.java:19) - Task16 : has been rejected 18.10.2011 20:09:28 DEBUG (TestRejectedExecutionHandler.java:19) - Task17 : has been rejected 18.10.2011 20:09:28 DEBUG (TestRejectedExecutionHandler.java:19) - Task18 : has been rejected 18.10.2011 20:09:28 DEBUG (TestTask.java:25) - Task14 : is started. 18.10.2011 20:09:28 DEBUG (TestTask.java:25) - Task15 : is started. 18.10.2011 20:09:28 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 6 - TotalTaskCount : 12 - isTerminated : false 18.10.2011 20:09:33 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 6 - TotalTaskCount : 12 - isTerminated : false 18.10.2011 20:09:38 DEBUG (TestTask.java:27) - Task10 : is completed. 18.10.2011 20:09:38 DEBUG (TestTask.java:25) - Task11 : is started. 18.10.2011 20:09:38 DEBUG (TestTask.java:27) - Task14 : is completed. 18.10.2011 20:09:38 DEBUG (TestTask.java:27) - Task15 : is completed. 18.10.2011 20:09:38 DEBUG (TestTask.java:25) - Task12 : is started. 18.10.2011 20:09:38 DEBUG (TestTask.java:25) - Task13 : is started. 18.10.2011 20:09:38 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 9 - TotalTaskCount : 12 - isTerminated : false 18.10.2011 20:09:43 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 3 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 3 - CompletedTaskCount : 9 - TotalTaskCount : 12 - isTerminated : false 18.10.2011 20:09:48 DEBUG (TestTask.java:27) - Task11 : is completed. 18.10.2011 20:09:48 DEBUG (TestTask.java:27) - Task13 : is completed. 18.10.2011 20:09:48 DEBUG (TestTask.java:27) - Task12 : is completed. 18.10.2011 20:09:48 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 0 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 0 - CompletedTaskCount : 12 - TotalTaskCount : 12 - isTerminated : true 18.10.2011 20:09:53 DEBUG (ThreadPoolMonitorService.java:39) - CurrentPoolSize : 0 - CorePoolSize : 1 - MaximumPoolSize : 3 - ActiveTaskCount : 0 - CompletedTaskCount : 12 - TotalTaskCount : 12 - isTerminated : trueSTEP 15 : DOWNLOAD OTV_Spring_ThreadPool Reference: How to develop and monitor Thread Pool Services by using Spring from our JCG partner Eren Avsarogullari at the Online Technology Vision blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books