Category Archives: java

Spring xercesImpl SAXParseException Problem

Found a really tricky and obscure problem. Whenever running my Spring MVC project it fail at startup with this exception:

org.xml.sax.SAXParseException: cos-all-limited.1.2: An ''all'' model group must appear in a particle with '{'min occurs'}'='{'max occurs'}'=1, and that particle must be part of a pair which constitutes the '{'content type'}' of a complex type definition.

Thanks to this Graham Hacking’s blog article, I figured out this was because commons-dbcp pulling and older version of xercesImpl.

The solution was to add newer version of xercesImpl on pom:


  xerces
  xercesImpl
  2.11.0

Debugging Maven Unit Test

I don’t trust debugging Maven unit tests straight using Eclipse’s JUnit plugin, sometime it’s buggy and the classpath don’t match with Maven.

Here’s how you can attach eclipse debugger straight from Maven process. First setup few breakpoints of the suspicious code as per normal, and setup a Maven run configuration like this:

maven-debug-runconfig

When you run this configuration, Maven will halt right before unit tests are run:

maven-debug-console

Now create a Remote Java Application Debug configuration pointing to localhost port 5050

maven-debug-debugconfig

Happy debugging!

Financial Time in Java

Yes dealing with time is always one good source of confusion. If you deal with financial application, many uses New York City as a guideline. A trading platform I’m working at on a daily basis uses NY+7 time zone such that 5pm in New York coincide with midnight. This is how you can format current NY+7 time (keep in mind USA do have DST and your local time zone might/not have it):

TimeZone nyPlus7 = TimeZone.getTimeZone("America/New_York");
nyPlus7.setRawOffset(nyPlus7.getRawOffset() + 7*3600*1000);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(nyPlus7);
String nyPlus7TimeNow = df.format(new Date());

And here’s a unit test proof the above method indeed works:

String pattern = "yyyy-MM-dd HH:mm:ss";
Date expected, actual;

// Setup UTC and NY+7 time zones
TimeZone utcTz = TimeZone.getTimeZone("UTC");
TimeZone nyp7Tz = TimeZone.getTimeZone("America/New_York");
nyp7Tz.setRawOffset(nyp7Tz.getRawOffset() + 7*3600*1000);

// Setup DateFormat for parsing
DateFormat utc = new SimpleDateFormat(pattern);
utc.setTimeZone(utcTz);
DateFormat nyp7 = new SimpleDateFormat(pattern);
nyp7.setTimeZone(nyp7Tz);

// US DST off, NY is UTC-5, NY+7 is UTC+2
expected = utc.parse("2014-03-09 06:59:59");
actual = nyp7.parse("2014-03-09 08:59:59")
Assert.assertEquals(expected, actual);

// US DST on, NY is UTC-4, NY+7 is UTC+3
expected = utc.parse("2014-03-09 07:00:00");
actual = nyp7.parse("2014-03-09 10:00:00")
Assert.assertEquals(expected, actual);

It’s important to understand the best practice is to never store time data in string. Stick to java Date object or alike. Only format your time to string whenever you need to present it visually

Debugging Maven Unit Test in Eclipse / STS

Maven unit tests (run by surefire plugin) can be debugged with eclipse. Firstly check you’re using surefire plugin version 2.16 or newer. Observe / add following in your pom.xml


  maven-surefire-plugin
  2.16

Ensure you’ve set some breakpoints to debug the faulty codes. Then on your run configuration, add -Dmaven.surefire.debug=true parameter:

maven-debug1

When this run configuration is executed, maven will wait for you to attach the remote debugger instead of launching the tests:

maven-debug2

Now open eclipse debug configuration, create a Remote Java Application config with your source code and set the port to 5005:

maven-debug3

When the debugger attaches the unit test will proceed and when any breakpoints hit you can debug it like a normal java/eclipse application

Debugging Event Driven Multithreaded Java Code

So here’s the scenario: you have few potentially time consuming work need to be done. They need to be executed in the order of submission, but only one work at a time can run. Simple solution is to use single thread executor.

First let’s define the Job class. This is a simple class implementing Runnable, when the job runs / stops it will print to console. A random sleep between 1 to 5 seconds is introduced to simulate some reality.

public class Job implements Runnable {

  private String id;

  public Job(String id) {
    this.id = id;
  }
  
  
  public void run() {
    System.out.println("Job " + id + " started");
    
    long duration = (new Random(System.currentTimeMillis()).nextInt(5) + 1) * 1000;
    try {
      Thread.sleep(duration);
    } catch (InterruptedException e) {
      System.out.println("Job " + id + " interrupted");
      e.printStackTrace();
    }
    
    System.out.println("Job " + id + " completed");
  }

}

To run the job I have a JobRunner class that setups the ExecutorService. Just call submit(job) and the job will be queued and run when the worker thread is free.

public class JobRunner {

  private ExecutorService executor = Executors.newSingleThreadExecutor();

  public void enqueue(Job job) {
    executor.submit(job);
  }

}

But a debugging problem starts to appear. It is now obscure where does the code goes after calling submit()? Which thread runs it? Unless the programmer put a big comment informing it is actually a Runnable this could be very hard to guess.

Further Problems with Observer Pattern

Another common patterns often used is the observer pattern. The idea is you want specific objects to “react” when a particular event occured.

For example, let’s do two observers: OddJobObservers and EvenJobObservers. When an odd/even job completed the corresponding observer will print to console.

public class OddJobObserver implements Observer {

  
  public void update(Observable arg0, Object arg1) {
    int id = (int) arg1;
    if(id % 2 == 1)
      System.out.println("Odd job " + id + " completed");
  }

}
public class EvenJobObserver implements Observer {

  
  public void update(Observable o, Object arg) {
    int id = (int) arg;
    if(id % 2 == 0)
      System.out.println("Even job " + id + " finished");
  }

}

For this purpose we’ll refactor JobRunner to extend Observable so we can add the observers into it. The observer instances are created and registered on the constructor.

Each time a job is created it will also have reference back to JobRunner. We’ll also add a method jobFinished(int id) for the job to call when it’s done running.

public class JobRunner extends Observable {

  private ExecutorService executor = Executors.newSingleThreadExecutor();

  public JobRunner() {
    addObserver(new OddJobObserver());
    addObserver(new EvenJobObserver());
  }

  public void enqueue(Job job) {
    job.setJobRunner(this);
    executor.submit(job);
  }

  /**
   * This will be invoked by the Job's run method in the worker thread
   * to indicate the runner that this particular job is finished
   */
  public void jobFinished(int id) {
    setChanged();
    notifyObservers();
  }
}

And on the Job class once it’s finshed running we’ll notify the observers

public class Job implements Runnable {

  // ...
  
  
  public void run() {
    System.out.println("Job " + id + " started");
    
    // ...

    System.out.println("Job " + id + " completed");
    jobRunner.jobFinished(id);
  }

}

Consider debugging this code where you’re at the end of run() method which takes you to jobFinished(). It is now even more obscure because notifyObservers() is a Java API method, not your own code. Which code gets executed next?

Unless you did a good job on commenting, it’s very hard for someone reading your code to understand that at that point odd jobs will be observed by OddJobObserver and even jobs / EvenJobObserver.

The problem gets much worse with production client / server application with 50+ observers handling different scenario.

One approach is probably to put breakpoints on every single observers and see which one is actually “interested” in our event.

I hope this highlights how important properly commenting your code is (especially for your poor colleague who has to fix your code sometime in the future).

concurrencythreading

Java and XML Injectable Properties on Spring

Here’s how you can create a property file which can be injected on Java classes as well as on bean XML configuration file.

Firstly ensure annotation config is switched on:

Create your properties file, in this case I create config.properties on my classpath root (src/main/resources/config.properties on Maven-compliant project)

name=Gerry

Register this property file on your spring context using tag.


Whenever you want to inject the value into a Java class, you can use annotation


public class Person {

  ("${name}")
  private String name;

  //...
}

This will only work if your java beans are scanned by the same Spring context. If on different context you will need to create a separate property placeholder

Similarly you can do the same to xml bean config file


  

Multiple Environment Trick

If the value of your configuration item differ accros multiple environments (JDBC URL and context path are a good example of these), you can use the same method to inject value form environment variable.

Returning JSON View on Spring MVC

Another simple way to return JSON object is by using jackson-mapper-asl. Similar to how we can map server-bound post, this method can also be used to write response.

Firstly, on your Spring MVC enabled project, add following maven dependency:


  org.codehaus.jackson
  jackson-mapper-asl
  1.9.12

Spring can automatically convert your POJO into a json string. So say we have this data object we want to return:

public class Customer {
  private String name = "";
  private String email = "";
  // getters & setters...
}

And this is the controller request mapping method. Important bits here is the method returns a POJO object directly, and it is annotated with annotation.

("/customer/{id}")

public Customer getCustomer(("id") long id) {
  Customer customer = // Search customer by given id through repository..
  return customer;
}

On the client side the returned JSON will be something like this:

{
  name = "Tom",
  email = ""
}

Injecting Properties to Spring MVC JSP View

Spring MVC internationalization (i18n) message support can be used for a simple config / property file. Add following bean definition on your container xml config file:


The bean above will read properties key-value pairs from WEB-INF/i18n/site.properties. Make sure you create this file with standard java-style properties:

site.name=Cool Bananas

Then in your JSP views, without any further intervention you can inject the values. Use spring message tag to achieve this

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

  
    <message code="site.name"></message>
  
  
  

Downloading Oracle JDK via Shell / Script

The most common reason you’d want to do this is if you have a remote server which you can only access via SSH. Downloading jdk first to your computer then uploading it is an option but often it’s slow. Here’s how you can download it directly from your SSH shell:

  1. Using Google Chrome browser, go to Oracle JDK download page, pick the jdk package you want to download
  2. Press F12 to open Chrome’s developer tools. Go to Network tab. networktab
  3. Accept the user agreement on Oracle’s website, and click the package link
  4. On developer tools window, find the first entry that has your package name. Right click and select Copy as cURL copycurl
  5. Now the curl command required to download the package is in your clipboard. You can paste this to your remote server’s SSH session and use it to download the package directly