Wednesday, December 28, 2011

Comparable vs. Comparator

http://grdurand.com/static/presentation_four/comparable.html

Classes should implement the Comparable interface to control their natural ordering.

Objects that implement Comparable can be sorted by Collections.sort() and Arrays.sort() and can be used as keys in a sorted map or elements in a sorted set without the need to specify aComparator.

« Interface »
Comparable
+ compareTo(Object) : int
compareTo() compares this object with another object and returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the other object.

Use Comparator to sort objects in an order other than their natural ordering.

« Interface »
Comparator
+ compare(Object, Object) : int
+ equals(Object) : boolean
compare() compares its two arguments for order, and returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Saturday, December 24, 2011

[Play!] tips

  1. If you do as many tasks as possible such as validation in the application instead of the database it is always easier to scale.
  2. Do not use something like the DAO pattern in Play, as this is not natural for the framework and would pretty much break its flow. The anemic domain model—pulling logic from the object into data access objects—should be an absolute no-go when developing with Play.
  3. Every HTML file in the app/views/tags directory is automatically used as a tag (such as #{loginStatus /}, which makes creating tags pretty simple.
  4. Be aware that the session referenced in the code is actually not a HttpSession as in almost all other Java based frameworks. It is not an object stored on the server side, but rather its contents are stored as an encrypted cookie on the client side. This means you cannot store an arbitrary amount of data in it.
  5. If you need more performance, there is a mechanism called FastTags inside of Play. These tags are actually compiled, as they are written in pure Java. This speeds up execution time.



[Play!] How to use URL arguments to list multiple match results ?

You can even create a List from the arguments in the URL with the following line of code in conf/routes:

GET /showUsers/{<[\0-9]+>ids}/? Application.showUsers

In your application code you know you could use a List IDs and show several users at once, when the URL /showUsers/1234/1/2 is called. Your controller code would start like this:

public static void showUsers(@As("/") List ids) {
}

This introduces some new complexity in your application logic, so always be aware if you really want to do this. One of the usecases where this is useful is when you want to use some sort of hierarchical trees in your URLs, like when displaying a mailbox with folders and arbitrary
subfolders

How do deploy Play! on CloudFoundry ...

(prerequisite ruby and gem)
> sudo gem install vmc
> vmc target api.cloudfoundry.com
> vmc login
> cd helloworld (the Play app you created)
> play install cloudfoundry-0.4
> cat conf/dependencies.yml
require:
- play 1.2.4
- play -> cloudfoundry 0.4


> play war --output=../hello --zip
> mv ../hello.war .
> vmc push
Would you like to deploy from the current directory? [Yn]:
Application Name: dmakplayhello
Application Deployed URL [dmakplayhello.cloudfoundry.com]:
Detected a Java Web Application, is this correct? [Yn]:
Memory Reservation (64M, 128M, 256M, 512M, 1G) [512M]:
Creating Application: OK
Would you like to bind any services to 'dmakplayhello'? [yN]:
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (265K): OK
Push Status: OK
Staging Application: OK
Starting Application: OK


> vmc update dmakplayhello (to update your changes to Cloud Foundry)

Friday, December 23, 2011

How do deploy Play! on Jelastic and Heroku ...

http://www.youtube.com/watch?v=SHA5aITE7Ak

HEROKU:
1. create a helloworld app by following http://www.playframework.org/documentation/1.1/firstapp
2. > cd helloworld
3. > git init
4. create .gitignore and add folders to be ignored
5. > git add .
6. > git commit -m init
7. > heroku create -s cedar
8. > git push heroku master
...
-----> Launching... done, v5
http://growing-warrior-4634.herokuapp.com deployed to Heroku

note: if you want to change a new name instead of using growing-warrior-4634, then you can do the following.
1. helloworld> git remote rm heroku
2. helloworld> git remote add heroku git@heroku.com:david-play-hello.git
3. helloworld> git push heroku master
then the new URL will be http://david-play-hello.herokuapp.com
- or -
1. heroku rename david-play-hello --app growing-warrior-4634

JELASTIC:
1. cd helloworld
2. > play war --output=../hello --zip
3. > cd ..
4. > ls hello.war
5. Navigate to jelastic.com and login
6. Upload hello.war and deploy it under Play

Tuesday, December 20, 2011

Spring annotations - @Service and @Repository

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

@Service serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. What this means is that you could annotate your service-layer classes with @Component, but by annotating them with @Service instead, your classes are more properly suited for processing by tools or associating with aspects, since @Service makes an ideal target for pointcuts.

@Service
public class WorldServiceImpl implements WorldService {
@Autowired
private CountryDao countryDao;
...
}
@Service - business service facade (it makes use of DAO)
@Repository - indicate that a class function as a repository or a data access object (DAO)

http://www.slideshare.net/kensipe/spring-3-annotated-development

The @Repository annotation (introduced in Spring 2.0) and @Service annotation (introduced in Spring 2.5) are specialization of the @Component annotation.
The main advantage of using @Repository or @Service over @Component is that it's easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository.
Also, the specialized annotations help to clearly demarcate application layers (in a standard 3 tiers application).
http://stackoverflow.com/questions/6256331/spring-annotations-repository-and-service

Then the only thing you need in the xml is the context:component-scan tag to tell Spring which package to scan for @Component annotations. @Repository extends @Component so @Repository is an @Component.
http://www.coderanch.com/t/61389/oa/autowired-annotation


Saturday, December 17, 2011

Where to save the hibernate.cfg.xml ?

hibernate.cfg.xml must be found in the root of the classpath when the webapp is started.

If you are using maven to build the project, put hibernate.cfg.xml in the src/main/resources directory so that when you build the war package, it will be automatically be placed in /WEB-INF/classes

If not using maven, place the file directly in your WEB-INF/classes dir

http://stackoverflow.com/questions/4934330/org-hibernate-hibernateexception-hibernate-cfg-xml-not-found

Friday, December 16, 2011

How does BigMemory hide objects from the Java garbage collector?

BigMemory achieves off-heap storage using Direct ByteBuffers, which is a feature introduced with NIO in Java 1.4 to allow zero copy buffers between Java applications and the operating system as it's not possible with memory belonging to Java heap because of unstable location.

An interesting additional property of Direct ByteBuffers is that they give access to memory that won't be managed by the Garbage Collector, hence avoiding its overhead.

Note: The point of BigMemory is not that native memory is faster, but rather, it's to reduce the overhead of the garbage collector having to go through the effort of tracking down references to memory and cleaning it up.

http://www.quora.com/How-does-BigMemory-hide-objects-from-the-Java-garbage-collector


Terracotta's BigMemory takes advantage of non-garbage-collected, off-heap "native memory," and apparently this is about 10x slower than heap-storage due to serialization/deserialization issues.

http://stackoverflow.com/questions/5863316/is-java-native-memory-faster-than-the-heap

Java VM option to setup Direct Memory Size: -XX:MaxDirectMemorySize

Tuesday, December 13, 2011

What do you use NoSQL for ?

General Use Cases:
- Bigness
- Massive write performance
- Fast key-value access
- Flexible schema and flexible datatypes
- Schema migration
- Write availability
- Easier maintainability, administration and operations
- No single point of failure
- Generally available parallel computing
- Programmer ease of use
- Use the right data model for the right problem
- Avoid hitting the wall
- Distributed systems support
- Tunable CAP tradeoffs

http://highscalability.com/blog/2010/12/6/what-the-heck-are-you-actually-using-nosql-for.html

http://www.infoq.com/presentations/Using-Spring-with-NoSQL-Databases (Spring Data)

The 10 Most Important Open Source Projects of 2011

The 10 Most Important Open Source Projects of 2011
Hadoop
Git
Cassandra
LibreOffice
OpenStack
Nginx
jQuery
Node.js
Puppet
Linux 3.0

Saturday, December 3, 2011

How to setup Maven home in IntelliJ ?

http://devnet.jetbrains.net/thread/278634

A valid Maven installation should contain bin/m2.conf

$ sudo find / -name m2.conf
/home/david/.hudson/maven/slavebundle/bundled-maven/bin/m2.conf
/home/david/tools/springsource/maven-2.2.1.RELEASE/bin/m2.conf
/usr/share/maven2/bin/m2.conf
/etc/maven2/m2.conf

$ export M2_HOME=/usr/share/maven