Wednesday 25 December 2013

Finding Memory Leak in Java

Here is a blog with an example how to find a memory leak in java application with analysing tools: http://www.captaindebug.com/2013/12/investigating-memory-leaks-part-2.html

Tuesday 24 December 2013

StampedLock

Java 8 includes a new synchronization mechanism called StampedLock
Great article about it is here: http://www.javaspecialists.eu/archive/Issue215.html

Monday 28 October 2013

ExpectedException JUnit 4.9

The ExpectedException Rule allows in-test specification of expected exception types and messages: Example here: http://junit.org/javadoc/4.9/org/junit/rules/ExpectedException.html

Sunday 27 October 2013

Optional in guava

Optional is a way of replacing a nullable T reference with a non-null value. An Optional may either contain a non-null T reference (in which case we say the reference is "present"), or it may contain nothing (in which case we say the reference is "absent"). It is never said to "contain null."

Same concept Optional class is going to be added to the core java.util library in jdk8

More detalails here: https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained

Monday 21 October 2013

JSR 303 Beans Validation - Groups

With JSR 303 we can validate depending on the context by using groups.
Read whole story here: http://www.jroller.com/eyallupu/entry/jsr_303_beans_validation_using

Friday 18 October 2013

RestX - the lightweight Java REST framework

Pros:
  • Fast (It is build using annotation processors, so it is not using reflection but generated code during compilation)
  • Has very simple and neat console for testing rests
  • Has wizard for generating project from scratch
  • Can be used in the current project with simple configuration http://restx.io/docs/manual-app-bootstrap.html
  • Has well constructed DI
  • Very nice usage of Rest Specifications notated in yuml
  • Examples of mongo and couchbase integrations
  • Integrations with tomcat, jetty, simpleframework

For me it's worth trying!

Read more here: http://restx.io

Monday 14 October 2013

How to connect Java Mission Control to your application with Flight Recorder enabled?

You have to:
  • Run application with java version1.7.0_40 or higher
  • Start application with flags -XX:+UnlockCommertialFeatures -XX:+FlightRecorder

Worth watching: http://www.youtube.com/watch?v=WMEpRUgp9Y4

Saturday 5 October 2013

Spring core started to have cglib dependency by default

Due to: https://jira.springsource.org/browse/SPR-9669
Spring Core is going to be have by default cglib library dependancy, as it is commited into the 3.2 branch.

The efect of this change is eliminating the need to manually add CGLIB to the application classpath, what is needed for using Java-based configuration functionality and proxy-target-class and method injection. Those things will work out of box.

All of this will work from the next released version.

Tuesday 1 October 2013

How to set a thread, that it will not stop program from exiting?

Just set him as a deamon:
  thread.setDaemon(true);

Sunday 15 September 2013

Tempus-fugit - library for concurrent code

The tempus-fugit library helps you write and test concurrent code.
It helps you...
Write cleaner concurrent code with less boiler plate code
Introduce determinism back into your concurrent tests
Leverage your synergies and generally be awesome

More here: http://tempusfugitlibrary.org/

Monday 19 August 2013

FEST library for fluent assertions in junit tests

Here you can read more about library that make junit tests more readable using fluent assertions: https://code.google.com/p/fest/

Sunday 18 August 2013

Tuesday 6 August 2013

IntelliJ IDEA useful key shortcuts

  • ALT + UP/DOWN - mark text contextual 
  • CMD + F12 - members in file
  • F8, F7, SHIFT + F8, F9  - Debug over, into, out, resume
  • CMD + SHIFT + F8 - view breakpoints
  • CMD + N - generate in class
  • CMD + E - recent open files
  • CMD + ALT + B - go to implementation
  • F1 - javadoc of content
  • CTRL + H - class hierarchy
  • CMD + J - live templates
  • CMD + ALT + T - surrount with
  • CTRL + T - refactor menu
  • CMD + P - show method types arguments
  • CMD + SHIFT + BACKSPACE - cycle history actions
  • CMD+ SHIFT + V - paste from last 5 copies
  • CMD + SHIFT + E - recently changed files
  • CMD + R - replace
  • ALT + F7 - find usage
  • ALT + SHIFT + UP/DOWN - move text
  • CTRL + SPACE - code completion
  • ALT + CTRL + L - format code
  • ALT + CTRL + O - reformat imports
  • CMD + BACKSPACE - delete line
  • CMD + SHIFT + F - find in path
  • CMD + SHIFT + R - replace in path
  • CMD + O - open class
  • CMD + SHIFT + O - open file
  • CMD + L - go to line
  • CMD + SHIFT + A - suggests actions
  • CMD + B - go to declaration
  • CTRL + SHIFT + R - run test
  • CMD + ALT + SHIFT + F7 - find usage
  • CMD + / - comment line
  • SHIFT + F6 - change name
  • CTRL +SHIFT + R - run
  • CTRL + SHIFT + D - debug
  • CMD + SHIFT + T - create or go to test

Monday 29 July 2013

Default implementation of spring util:map

The default imlementation of <util:map> is LinkedHashMap, but you can change it like it is described underneath:




Sunday 21 July 2013

Groovy and WS

If you try to use Apache CXF, you can have problems with generated inner classes, that are not implemented in groovy.
You can use http://groovy.codehaus.org/GroovyWS

Some groovy gotchas

  • groovy -e “println ‘Hello World’” - execute groovy code from comand line
  • groovy Example.groovy - run groovy code from the file Example.groovy
  • groovyc Example.groovy - compile groovy code from the file to the JVM class. Then you can run this code with JVM adding groovy libraries: java -classpath groovy-all.jar:. Example
  • groovysh - interactive command line shell
  • groovyConsole - interactive GUI shell
  • Arithmetic:
  • BigDecimal is used by default for number literal with fraction
  • Number literals without fraction are the smallest type that fits Integer, Long, BigInteger
  • Strings:
  • Single quoted is standard java String
  • Double quoted is GString (may contain groovy expressions, can be substring using [])
  • Tripple quoted are multiline strings
  • Slashy - no escaping needed - useful for regex
  • Operator overloading - operators map to specific methods f.e. + is same ad method add()
  • Method can be referenced as closure with .& operator
  • Closures has properties:
  • maximumNumberOfParameters - number of parameters provided to closure
  • parameterTypes - array of types of parameters
  • Collection methods: collect - as map in Scala and inject as foldLeft
  • Objects evaluate to false: null, empty collections, iterators, empty GStrings, numbers equals 0, non-matching regexp pattern matchers, everything else evaluete to true
  • == is treated as equals() in java but null safe. If objects implement Comparable then compareTo method is used
  • a.is(b) - for java ==
  • string.contains(gstring) is false due to internal equals method usage
  • Null-safe operator returns null without futher evaluation in case od null ?.
  • Elvis operator shorthand for returning default value ?:
  • Spaceship operator <=> which replace compareTo method
  • regexp operator ~ what creates a Pattern, =~ what creates Matcher, ==~ matching String against regexp
  • spread operator *. is a shorthand for Collection.collect(closure)
  • as operator transforms a variable to another type
  • xml support with classes MarkupBuilder and XmlParser
  • json suppert with JsonBuilder, JsonSlurper
  • Integrations with java:
  • Groovy shell - evaluates expressions in Java with passing in and out variables
  • GroovyClassLoader - parse and compile classes
  • Groovy Joint Compiler - compile all groovy and java sources. Allows circular dependencies between them, but on other layers
  • Spring Groovy Integration - beans can be defined by POGO with lang namespace
  • Changes between groovy and java:
  • this inside static method refers to class
  • do-while loops are not supperted
  • all exceptions are unchecked
  • inner classes are not allowed
  • Built-in AST Transformations: @Singleton, @Delegate, @TupleConstructor, @TimedInterrupt
  • @TypedChecked - analyze at compile-time
  • @CompileStatic - compile like javac compiler, no groovy dynamic features

Sunday 16 June 2013

A tree explorer plugin for navigating the filesystem for vi editor

The NERD tree allows you to explore your filesystem and to open files and directories. It presents the filesystem to you in the form of a tree which you manipulate with the keyboard and/or mouse. It also allows you to perform simple filesystem operations.

More info how to install it you can find here:
http://www.vim.org/scripts/script.php?script_id=1658

Library for cloning java objects

If you need to clone whole objects (f.e. map of maps) here is a library to do it in just one line:

The objects don't have to implement the Cloneable interface. Effectively, this library can clone ANY Java object. It can be used i.e. in cache implementations if you don't want the cached object to be modified or whenever you want to create a deep copy of objects.

Read more here:
https://code.google.com/p/cloning/

Saturday 15 June 2013

bintray

It is a public free repository in the internet: https://bintray.com/
If you want to share artifacts from your github project it could be the easiest way.

  • Easiest way to share your software with the world
  • Get credit and appreciation for YOUR packages
  • Manage your release notes or import them from GitHub
  • Automate your distribution using REST API
  • Easy integration with Maven, Gradle, Yum and Apt
  • Your binaries are easy to find using metadata & text indexing
  • Near real-time stats
  • Interact and get feedback from users
  • Ask to include your software in other repositories
  • Your binaries are available through a fast CDN

Friday 14 June 2013

Benchmarks of the web frameworks

Here is the site where you can see comparison of plenty of web frameworks:
http://www.techempower.com/benchmarks/

GVM - the Groovy enVironment Manager

GVM is a tool for managing groovy, grails and other groovy based systems on your computer. With it you can switch with just one command between its versions. It is working on Unix and Mac.

To install it just run in the terminal:

curl -s get.gvmtool.net | bash

f.e. to install grovy and grails run in the terminal following commands:

gvm install groovy

gvm install grails

To list installed applications run:

gvm list

For help:

gvm help

To read more about gvm go to the following page: http://gvmtool.net/

Saturday 1 June 2013

Diagnosing Java Application tools

Console tools:

jps -v - lists java processes

jcmd - runs command on the jvm
Some examples:

  • jcmd 31435 VM.version - shows information of the virtual machine of the process with pid 31435
  • jcmd 31435 help - shows possible commands for the process with pid 31435
  • jcmd 31435 Thread.print - prints ThreadDump (same as jstack pid)
  • jcmd 31435 GC.class_histogram - prints statistics of classes in the Heap
  • jcmd 31435 GC.heap_dump file - Dump Heap to the given file
  • jcmd 31435 GC.run - runs Garbage Collection
  • jcmd 31435 VM.uptime - time that process work
  • jcmd 31435 VM.flags - flags of the VM for this process
  • jcmd 31435 VM.system_properties -m properties what process was run with
  • jcmd 31435 VM.command_line - java command that started process
  • jcmd 31435 VM.version - the virtual machine version process was run with

jstat - prints jvm statistics

  • jstat -options - lista all options
  • jstat -gc 3773 1s - prints Garbage Collection info every second for process with pid 3773
  • jstat -class 3773 1s - class loaded stats

jvisualvm - graphical tool monitoring jvm processes. We can see CPU, Thread, Heap usage. It allows comparing Heap Dumps, inspecting all objects and references to them. We can use Sampler and Profiler to check where most of the time is spent and where object are mostly allocated.

jstack -F pid - suspends process in unknown state

Friday 26 April 2013

The target release date for Java 8 is now 2014/3/18

The target release date for Java 8 is now 2014/3/18.
Read more on: http://openjdk.java.net/projects/jdk8/

Thursday 18 April 2013

Java 8 delayed

We've got bad news for Java.
Mark Reinhold has posted a blog with a proposal to delay the release of Java 8 to the first quarter in 2014. http://mreinhold.org/blog/secure-the-train

Friday 22 March 2013

JDK8 will be released at 9th September 2013

We will get Lambdas, new DataTime API, default implementations in the interfaces, static references to the methods, Stream interface with Map Reduce operations on Collections, removal of Permanent Generation in the Heap, repeating annotations, annotations on type use, Nashorn - new JavaScript engine and some smaller changes.
You can see examples here: https://github.com/awislowski/jdk8

Thursday 21 March 2013

Sunday 10 March 2013

Red Hat Assumes Leadership of OpenJDK 6 Community

Red Hat, Inc. (NYSE: RHT), the world’s leading provider of open source solutions, today announced it has transitioned into a leadership role for the OpenJDK 6 project, effectively extending support for the technology and its users.
Read more here: http://www.redhat.com/about/news/press-archive/2013/3/red-hat-reinforces-java-commitment

Thursday 21 February 2013

Netty

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
Read more here : netty.io

Thursday 14 February 2013

StampedLock - new implementation for a lock

In java 8 we there will be a new implementation of Lock class. It will allow to have a lock for reading and writing. Possibly upgrading lock from read to write and some other features.
More to be read here: http://cr.openjdk.java.net/~chegar/8005697/ver.00/javadoc/StampedLock.html

Monday 4 February 2013

Testing Spring services that uses scoped objects

I have faced problem of testing Java Services that used Session Scoped objects (Same would by with f.e. Request Scope). My Scoped object was treated as a data, but contained simple operations managing its state. My service did some operation on this object changing it state. First I tried to mock this object and mocking its methods to verify correct behavior. But it was not easy due to complicated structure of mocking object. Then I decided to use a Spy object, but the only reason is to inject this object into my Service with annotation @InjectMocks. I did not change my object at all in the Spy, but used it just for dependency injection, as I did not want to have a setter method only for testing.
So the conclusion is: In Mockito Session/Request Scope Objects can be easily tested when defined as Spy objects injected by @InjectMocks

Tuesday 8 January 2013

Java collections for primitive types

Java collections that save memory, when elements are primitive types:
http://trove.starlight-systems.com/overview

Ease of creating immutable collection woth Guava

In case of need od creating immutable collections in java, that are nice created, safe and efficient look for guava implementations
Read examples here: http://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained