Monday, December 31, 2012

Java WeakReference, SoftReference and WeakHashMap

Strong reference

A strong reference is an ordinary Java reference. Usually Java references will be implemented as pointers, but that's not required by the language specification. They may be using an additional layer of indirection to enable easier garbage collection. References interacts with garbage collector.  Specifically, if an object is reachable via a chain of strong references (strongly reachable), it is not eligible for garbage collection. This is normally exactly what you want.

Weak reference

One problem with strong references is caching, particular with very large structures like images, sounds, etc. Cache is common source of memory leaks. Once you put an object reference into a cache, it’s easy to forget that it’s there and leave it in the cache long after it becomes irrelevant. 

To solve this problem you need to implement a cache for which an entry is relevant exactlyso long as there are references to its key outside of the cache.

Weak reference leverage the garbage collector's ability to determine reachability for you.

You can create and use Weak reference like this (for some imaginary Car object and car object instance):

//Create weak car reference.
WeakReference<Car> weakCar = new WeakReference<Car>(car);
//Then to get actual car instance use following:
Car weakCarRef = weakCar.get();
 Weak reference isn't strong enough to prevent garbage collection, so you may find that .get() suddenly starts returning null. Which is basically what you want in the first place, but usually WeakReferences are used together with WeakHashMap class.

In WeakHashMap entries will be removed automatically after they become obsolete. Remember that WeakHashMap is useful only if the desired lifetime of cache entries is determined by external references to the key, not the value.

Using WeakHashMap is simple because it uses same Map interface that is used for HashMap and all basic Map data structures in Java.

Soft reference

soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. In practice softly reachable objects are generally retained as long as memory is in plentiful supply.

Use it or not to use it?
Personally I don't like weak, soft and phantom references because you basically hand control of you program to GC and he will decide when is the time to remove reference, not you. 

Sunday, December 30, 2012

Desing Patterns in Java - Command pattern

Description

Command pattern is an object behavioral pattern that allows us to achieve complete decoupling between the sender and the receiver. (A sender is an object that invokes an operation, and a receiver is an object that receives the request to execute a certain operation.

This pattern allows the requester of a particular action to be decoupled from the object that performs the action.

This pattern encodes the details needed to send a message to an object. Such messages can be invoked along different points of time or location in a general way without having to hard-code its details. It allows messages to be invoked one or more times, or passed along to different parts of the system or multiple systems without requiring the details of a specific invocation to be known before execution.

Object references VS function pointers

Some languages (C for example) support function pointers facilities that allow programs to store and transmit the ability to invoke a particular function. Java does not provide function pointers, but object references together with dynamic loading and binding mechanism can be used to achieve a similar effect. This is where command pattern comes into play.

Example

We will use simple remote control for CD Player as example. This simple CD Player support only basic commands (Play, Stop and Pause), but that is good enough for out example of command pattern.

//Cd Player that will be controlled by remote control
public class CdPlayer {

  public enum State {
    PAUSE, PLAY, STOP
  }

  private State state;

  public void pause() {
    state = State.PAUSE;
  }

  public void stop() {
    state = State.STOP;
  }

  public void play() {
    state = State.PLAY;
  }

  public State getState() {
    return state;
  }

}
//Simple command interface.
public interface Command { 
  void execute(); 
}
//Cd Player remote control that will control Cd Player through button press.
public class CdPlayerRemoteControl {
 
  private Command command;
 
  public void setCommand(Command command) {
    this.command = command;
  } 
 
  public void pressButton() {
    command.execute();
  }
}

//Cd Player play command.
public class CdPlayerPlayCommand implements Command {

  private CdPlayer cdPlayer;
 
  public CdPlayerPlayCommand(CdPlayer cdPlayer) {
    this.cdPlayer = cdPlayer;
  }
 
  public void execute() {
    cdPlayer.play();
  }
 
}
//Cd Player pause command.
public class CdPlayerPauseCommand implements Command {

  private CdPlayer cdPlayer;
 
  public CdPlayerPauseCommand(CdPlayer cdPlayer) {
    this.cdPlayer = cdPlayer;
  }
 
  public void execute() {
    cdPlayer.pause();
  }
 
}
//Stop command...
public class CdPlayerStopCommand implements Command {
 
  private CdPlayer cdPlayer;
 
  public CdPlayerStopCommand(CdPlayer cdPlayer) {
    this.cdPlayer = cdPlayer;
  }
 
  public void execute() {
    cdPlayer.stop();
  }
 
}
//Client class that uses remote control for CD Player control.
public class Client {
 
  public static void main(String[] args) {
    CdPlayerRemoteControl cdPlayerRemoteControl = new CdPlayerRemoteControl();  
    CdPlayer cdPlayer = new CdPlayer();
    
    Command play = new CdPlayerPlayCommand(cdPlayer);
    Command pause = new CdPlayerPauseCommand(cdPlayer);
    Command stop = new CdPlayerStopCommand(cdPlayer);
  
    //Play music  
    cdPlayerRemoteControl.setCommand(play);
    cdPlayerRemoteControl.pressButton();
    
    //Stop music
    cdPlayerRemoteControl.setCommand(stop);
    cdPlayerRemoteControl.pressButton();
  
    //prints STOP
    System.out.println(cdPlayer.getState());
  
  }
 
}

Wednesday, November 21, 2012

Un-merge (delete) changes from SVN

If you ever need to reset trunk to previous revision, or for that matter remove any revision from any directory in SVN (including trunk of course) just do reverse merge:

svn merge -r1001:1000 .

1001 - new revision you want to remove
1000 - old previous revision you want merge to calculate changes from

commit

Wednesday, October 10, 2012

ROME - RSS Java library

If you are looking for simple library for RSS feeds, than look no further. 
Just download ROME library for Java and get RSS feeds inside your Java program in 10 minutes. Just don't forget to put JDOM library inside your CLASSPATH because ROME has dependency on JDOM.

This code sample bellow is really self explanatory. What a great library!


public void getRssFeeds(String rssFeedurl) {
  URL url = new URL(rssFeedurl);
  HttpURLConnection httpcon = 
          (HttpURLConnection)url.openConnection();
          
  SyndFeedInput input = new SyndFeedInput();
  SyndFeed feed = input.build(new XmlReader(httpcon));
  List entries = feed.getEntries();
  Iterator<SyndEntry> itEntries = entries.iterator();

  while (itEntries.hasNext()) {
    SyndEntry entry = itEntries.next();
    log.info("Title: " + entry.getTitle());
    log.info("Link: " + entry.getLink());
    log.info("Author: " + entry.getAuthor());
    log.info("Publish Date: " + entry.getPublishedDate());
    log.info("Description: " + 
          entry.getDescription().getValue());            
  }
}

Sunday, September 23, 2012

Douglas Crockford and JavaScript

Why to write anything about Douglas Crockford?


Well he is very important in JavaScript community. His book JavaScript: The Good Parts is considered one of the best book on JavaScript. But why this book is so good and why is JavaScript all of a sudden so important?

In the past JavaScript was simply the language of the browser. But now JavaScript is finding it's way to multiple different areas like server side programming (look at node.js for example),  new old fat client programming with HTML5 (canvas, sockets), cross-platform mobile development (PhoneGap), games development (Unity), Microsoft Windows 8 applications, etc.

I don't think this is happening because JavaScript is good language. It isn't and it's design is driven by browser's DOM which is awful. This is happening because people simply are used to it and more and more people are using it whether they want to or not. I see more and more really good Java programmers using it and struggling and hating it because concepts are not event close to Java's. You are really forced to use it if you want to make good web application, or cross-platform mobile application or to develop games on certain engines. And JavaScript simply works in all of these scenarios.

It's like JavaScript is becoming glue in this highly fragmented proprietary world.

Now why is Douglas Crockford important? It is because he manage to find beauty in JavaScript. And I must agree that there is beauty in it. I love functional programming and functions are first-class citizens in JavaScript. He also make good point that transferring what he calls "classical methodologies" (class designs and pattern) into JavaScript is sub-optimal solution and that we need new way of thinking when code in JavaScript.

You can learn more about  Douglas Crockford from his website: www.crockford.com also you can watch some of his great lectures on youtube.

Wednesday, August 15, 2012

Just Koch snowflake in canvas (html5)

Koch snowflake is simple fractal. This fractal has fairly simple implementation. You just describe this recursive curve within recursive function. I will demonstrate this implementation in canvas element animation. You simply must love beauty of recursion implementations of any kind.
Your browser doesn't support the canvas element. Code:
$(function() {
  var canvas = $("#mycanvas");
  var context = canvas.get(0).getContext("2d");
  context.fillStyle = "rgb(63, 169, 245)";                   
  var x;
  var y;           
  var angle;
  var level = 0;
            
  var animateKochSnowflake = function() {
    initialize();
    save();
    drawKochSnowflake();
    flush();
    level++;              
  }

  //Create snowflake  
  var drawKochSnowflake = function() {               
    drawKochCurve(level, 150);
    angle += 120;
    drawKochCurve(level, 150);
    angle += 120;
    drawKochCurve(level, 150);                
  };

  //Recursive implementation          
  var drawKochCurve = function(level, sideLength) {
    if (level < 1) {
      draw(sideLength);    
    }
    else {
      drawKochCurve(level - 1, sideLength / 3);
      angle -= 45;
      drawKochCurve(level - 1, sideLength / 3);
      angle += 90;
      drawKochCurve(level - 1, sideLength / 3);
      angle -= 45;
      drawKochCurve(level - 1, sideLength / 3);                    
    }                
  };           
            
  var draw = function(sideLength) {                
    x += sideLength * Math.sin(angle * Math.PI/180);
    y -= sideLength * Math.cos(angle * Math.PI/180);                
    context.lineTo(x, y);
  };
            
  var initialize = function() {
    angle = 90;                
    x = 170;
    y = 100;
    if (level > 5) {
      level = 0;
    }  
  };
            
  var save = function() {
    context.save();
    context.beginPath();
    context.clearRect(0, 0, 640, 480);
    context.translate(-level*10, 0);                               
                
    context.moveTo(x, y);    
  };
            
  var flush = function() {
    context.closePath();
    context.fill();                
    context.restore();                 
  };
            
  var animate = function() {                
    setInterval(animateKochSnowflake, 1000);              
  };            
            
  animate();
            
});

Monday, August 13, 2012

Java Concurrency

Introduction

Thread is similar to lightweight OS process except that can access shared memory.
Every thread has each own memory cache. Java runs in its own process and threads in Java runs within this process. Java have native support for multithreaded programming. This is one of many reasons why Java currently hold its status as best overall programming language.

Multithreaded programming is about speed and handling deadlocks (concurrent access) and safety issues (incorrect data when threads read old or inconsistent data).

Java provides several locking mechanism that will force exclusivity (only one thread is accessing certain parts of code).

Let’s look at some strategies how we can do concurrent programming in Java.

But first we will need some fairly long task so we can see differences in performance for each strategy.
For this I created something called FibonaciCalculator that will create array of Fibonaci numbers. Generation of Fibonaci array definitely cannot be characterized as CPU intensive task so I added Thread.sleep between each iteration to slow it down.


public class FibonaciCalculator {

    public static final int LONG_TASK_SIMULATED_DELAY = 150;

    public static int calculateFibonaciNumber(int n) {
        int previous = 0;
        int result = 1;
        for (int i = 0; i <= n; i++) {
            simulateCostlyTask();
            int sum = result + previous;
            previous = result;
            result = sum;
        }
        return result;
    }

    private static void simulateCostlyTask() {
        try {
            Thread.sleep(LONG_TASK_SIMULATED_DELAY);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

Single threaded strategy

First we will look at simple single threaded model. This is normal non-recursive implementation of Fibonaci number algorithm in Java.
public class SingleThreadedFibonaciArrayConstructor extends FibonaciArrayConstructor{

    protected int[] constructFibonaciNumbersArray(int size) {
        int[] fibonaciNumbers = new int[size];
        for (int i = 0; i < fibonaciNumbers.length; i++) {
            fibonaciNumbers[i] = FibonaciCalculator.calculateFibonaciNumber(i);
        }
        return fibonaciNumbers;
    }       

}


Simple multi threaded strategy

Second strategy is simple brute force multi threaded implementation. In this example we will use base means for concurrency in Java: “java.lang.Threads”. We will implement Runnable interface and create new thread for each runnable instance (in our case for each Fibonaci number calculation in array). Because each thread has access to currentIndex field we need to synchronize modification (incrementation) and retrieval on it. To achieve this we are using synchronization method. Synchronization is simplest way to lock certain part of code. This keyword ensure that only single thread can access marked section of code.
public class SimpleMultiThreadedFibonaciArrayConstructor extends FibonaciArrayConstructor{

    private int[] fibonaciNumbers;
    private int currentIndex = 0;

    protected int[] constructFibonaciNumbersArray(int size) {    
        fibonaciNumbers = new int[size];        

        List<thread> threads = new ArrayList<thread>();

        for (int i = 0; i < size; i++) {
            Runnable task = new MyRunnable();
            Thread worker = new Thread(task);
            worker.setName(String.valueOf(i));
            worker.start();
            threads.add(worker);
        }

        int running;
        do {
            running = 0;
            for (Thread thread : threads) {
                if (thread.isAlive()) {
                    running ++;
                }
            }
        } while (running > 0);
        

        return fibonaciNumbers;
    }

    private synchronized int getNextIndex() {
        return currentIndex++;
    }

    private class MyRunnable implements Runnable {
                
        public void run(){
            int nextIndex = getNextIndex();
            int calculatedFibonaci = FibonaciCalculator.calculateFibonaciNumber(nextIndex);
            fibonaciNumbers[nextIndex] = calculatedFibonaci;
        }

    }
}
Thread pool strategy

In previous example we are using maximum number of threads (size of array equals number of threads) for calculating this Fibonaci array. This can have disadvantages because it can cause performance overhead for creating new thread and switching between these threads. java.util.concurrent package offers improved support for concurrent programming. When we use this package we create pool of worker threads. The thread pools contains a work queue which holds tasks waiting to get executed. This thread pool can be described as a collection of runnables (work queue) and a connections of running threads. These threads are constantly running and are checking the work query for new work. If there is new work to be done they execute this Runnable. The Thread class itself provides a method, e.g. execute(Runnable r) to add runnables to the work queue. The Executor framework provides example implementation of the java.util.concurrent.Executor interface, e.g. Executors.newFixedThreadPool(int n) which will create n worker threads.
public class ThreadPooledMultiThreadedFibonaciArrayConstructor extends FibonaciArrayConstructor {

    private int[] fibonaciNumbers;
    private AtomicInteger currentIndex = new AtomicInteger(0);

    protected int[] constructFibonaciNumbersArray(int size) {
        fibonaciNumbers = new int[size];

        ExecutorService executor = Executors.newFixedThreadPool(5);

        for (int i = 0; i < size; i++) {
            Runnable worker = new MyRunnable();
            executor.execute(worker);
        }

        executor.shutdown();

        while (!executor.isTerminated()) {
            
        }

        return fibonaciNumbers;
    }

    

    private class MyRunnable implements Runnable {

        public void run(){
            int nextIndex = currentIndex.getAndAdd(1);
            int calculatedFibonaci = FibonaciCalculator.calculateFibonaciNumber(nextIndex);
            fibonaciNumbers[nextIndex] = calculatedFibonaci;
        }

    }
}

Please note that we are not using synchronized method anymore but we are using AtomicInteger type which is part of Java nonblocking algorithm support that is based on low-level atomic hardware primitives. getAndAdd(1) will retrieve value and modify it (in his case add one to it) as a single operation, so we do not need to synchronize it because there is no danger of dirty read or deadlock.

Feature and callables strategy

In case you expect your threads to return a computed result you can use java.util.concurrent.Callable. Callables allow to return values after competition. If you submit a callable to an executor the framework returns a java.util.concurrent.Future. This futures can be used to check the status of a callable and to retrieve the result from the callable.

In both previous example we are force using stuff like non blocking algorithm support (AtomicInteger) or method synchronization because we have shared variable (currentIndex). Threads also share Fibonaci array, but there are no synchronization issues. Wouldn’t be better if we remove dependency for this currentIndex variable? Yes of course it will be. We can do this by creating defensive copies of our thread execution algorithm.

public class FeaturesAndCallablesMultiThreadedFibonaciArrayConstructor extends FibonaciArrayConstructor{
        
    protected int[] constructFibonaciNumbersArray(int size) {

        int[] fibonaciNumbers = new int[size];

        ExecutorService executor = Executors.newFixedThreadPool(5);
        List<Future<Integer>> list = new ArrayList<Future<Integer>>();

        for (int i = 0; i < size; i++) {
            Callable<Integer> worker = new MyCallable(i);            
            Future<Integer> submit = executor.submit(worker);
            list.add(submit);
        }

        for (int i = 0; i < list.size(); i++) {
            try {
                Future<Integer> future = list.get(i);
                int futureResult = future.get();                
                fibonaciNumbers[i] = futureResult;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
        
        return fibonaciNumbers;
    }

    private class MyCallable implements Callable<Integer> {

        int currentIndex;

        public MyCallable(int currentIndex) {
            this.currentIndex = currentIndex;
        }

        public Integer call() throws Exception {            
            return FibonaciCalculator.calculateFibonaciNumber(currentIndex);
        }
    }
}

Testing and conclusion 

test result summary:


Calculation with SingleThreadedFibonaciArrayConstructor took 48 seconds. (1 thread)
Calculation with SimpleMultiThreadedFibonaciArrayConstructor took 3 seconds. (25 threads)
Calculation with ThreadPooledMultiThreadedFibonaciArrayConstructor took 11 seconds. (5 threads)
Calculation with FeaturesAndCallablesMultiThreadedFibonaciArrayConstructor took 11 seconds. (5 threads)


When we run all these implementations through test, we get following results. You can see that off course the more threads we have the execution is faster. In third and fourth implementation we are using 5 thread as opposite to first where we are using 25 threads.

Java 7 Fork-Join mechanism 

Java 7 introduce a new parallel mechanism for compute intensive tasks, the fork-join framework. The fork-join framework allows you to distribute a certain task on several workers and then wait for the result. But I have not yet install Java 7 (who does?), so it will not be described in this thread.

Complete implementation (with tests) can be found here.

Friday, April 20, 2012

Apache Nutch & Solr integration

I was searching for good tutorial on Apache Solr and Apache Nutch integration but couldn't find any. Then I realize that there is pretty good one on (go figure!) Apache Nutch wiki page. So if you looking tutorial that will help you with Solr/Nutch integration look here.

Tuesday, April 10, 2012

Faces generator for Oracle ADF (1.0alpha)

Introduction

Previously I was talking about this generator for ADF. It is currently in such state that it is working (kind of). So I decided to publish it and see if there are ADF developers interested in something like this.

Basic idea is automatic creation of view part of ADF Fusion Web Application using model part...well as model. I suggest that you first look at videos I prepared so you can see what it is all about, and if you like it then there are installation instructions below. Please be noted that this is alpha version and therefore it is unstable.

This extension work with all JDeveloper version greater than 11.1.1.2.0. But I recommend using it with JDeveloper version 11.1.1.4.0 and newer because in previous version there was issues with "Oracle Dynamic Tabs Shell" page template. This issues present themselves as instability (random exceptions) while running Fusion Web Application. 11.1.2.1.0 is not supported for now.

First example - Shows how generator can generate almost complete view of ADF Web application.


Second example - Shows how generator changes view when you introduce LOVs into model.


Third example - Shows how changes to model are reflected on view when you for example remove or add view instances from application module


OK. So, if you want to download and try generator, here are the steps. First and most important, generator can be downloaded from here. Installation is very simple, just copy this jar file into your <Middleware Home>/jdeveloper/jdev/exstensions and that's it.

Generation consists of four parts.

1. Create Fusion Web Application and create complete model for this application. Basic ADF stuff. I suggest that you stick with defaults when wizard ask you about project names and default packages (view for ViewController and model for Model Project)

2. Initialize your ADF view project. This is necessary, because JDeveloper need to add some libraries to view project. To do this you need to create JSF page that will use "Oracle Dynamic Tabs Shell" page template and then just drag and drop random data control (one data control is enough, just to initialize it's application module in view project) from each application module you plan to use on this page. You can create multiple JSF pages for each application module but that is not necessary.

3. You need to copy some files to this newly created view project. You need to do this, because this plugin is dependent on some Java classes and predefined bindings. You can download this files from here. When you unpack them please copy them according to this pattern:
  • public_html/images/* -> ViewController/public_html/images/
  • public_html/js/* -> ViewController/public_html/js/
  • public_html/WEB-INF/adfc-config.xml -> ViewController/WEB-INF/adfc-config.xml (you can also just edit yours adfc-config.xml file and add necessary definitions).
  • adfmscr/view/AdfFacesUtil.java -> ViewController/adfmscr/view/AdfFacesUtil.java
  • adfmscr/view/Launcher.java -> ViewController/adfmscr/view/Launcher.java
  • adfmscr/view/pageDefs/NavigationPageDef.xml -> ViewController/adfmscr/view/pageDefs/NavigationPageDef.xml 

4. Final step is to run generator by right-clicking on Model or ViewController project and choose Generate ADF Faces. Then just repeat step 4 if you change anything in model, you don't need to run steps 1 - 3 anymore.

For end I will share with you final version of  complete application I use in videos. It might come in handy. Thanks and ciao. :)

Thursday, March 29, 2012

Testing controllers in Grails

In other to unit test controller in Grails you need to extend ControllerUnitTestCase. Also your test class needs to be in same package as the controller class unsed test and the name of your test class needs to be "Tests".

That's it. Now let's see code example.

Simple controller:
class SimpleController {

    def dateService

    def firstAction = {
        render "first"
    }

    def secondAction = {
        render "second"
    }

    def thirdAction = {
        render dateService.currentDateFormated()
    }
}

Service:
class DateService {

    boolean transactional = true

    public String currentDateFormated() {
        return (new Date()).format("MM-dd-yyyy")
    }
}

Unit test for controller.
class SimpleControllerTests extends ControllerUnitTestCase {
    
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    public void testFirstAction() {
        controller.firstAction()
        assertEquals("first", controller.response.contentAsString)
    }

    public void testSecondAction() {
        controller.secondAction()
        assertEquals("second", controller.response.contentAsString)
    }

    public void testThird() {
        DateService dateService = new DateService()
        controller.dateService = dateService
        controller.thirdAction()
        assertEquals(dateService.currentDateFormated(), controller.response.contentAsString)
    }
}

If your controller references a service you have to explicitly initialise the service from your test like we did testThird() test in SimpleControllerTests tests.