Why do Java Developers wear glasses?

Because they are nerdy? 😎

No, because they don’t C# (see sharp). 😛

hahhaa. ok but seriously I am ramping up on some C# ASP.NET legacy project. Luckily I do not wear glasses!

So today I came across this code that has to do with events, but I have no idea once the event is raised, who’s listening, and what follows.

public delegate void MyEventHandler(String someArg);

public event MyEvent MyEvent;

private void onMyEvent() {
  if (MyEvent != null) {

public void doSomething() {
  // do something and trigger the event

So first up, what’s a delegate? it’s a function pointer that’s safe. ok.
and a keyword “event”. That’s self-explanatory.
I see that the code did something and has raised the event. BUT who’s listening?? and what happens after the event is raised?
Visual Studio’s find call hierarchy, view reference, Alt+F12 are not helping.

and then… few searches later, I found this code in one of the aspx pages…

<uc15:UCMySection ID="UCMySection" runat="server" OnMyEvent="UCMySection _DoSomethingElse"/>

Ohhhhhhhhh. It’s the aspx page user control listening… I get it now.

I like how on the C# side, the raising event is easy to do. But not sure about the ASP.NET part. Glad we can say bye to that soon.

On a semi-related note on events and publish and subscribe, I am working on another pubsub thing for reliable message delivery. Excited to get real project experience with Kafka (previous project) and RabbitMQ (new project)!


Mocking methods that return DeferredResult

I have a controller that returns DeferredResult for asynchronous usage. In the method, I am using a Timer to invoke the task.

My Controller:
public DeferredResult process()
DeferredResult deferredResult = new DeferredResult();
CallbackTask task = new CallbackTask (myProcessor, deferredResult);
timer.schedule(task, 0);
logger.info(“The request is being processed…”);

    return deferredResult;

My CallbackTask
public class CallbackTask extends TimerTask {

public void run() {
ProcessStatus processResult = myProcessor.process();

How do I JUnit test it?
First, I added some Thread sleep time and wait for the result.
The problem is, when the test server becomes too busy, the test would fail intermittently because my async operation is not completed before the first thread is returned.

I then decided to mock the timer.

doAnswer to the rescue!!!

stub the method, here I am stubbing the void method

doAnswer(new CallbackAnswer()).when(the(Timer.class)).schedule(any(CallbackTask.class), anyLong());

private class CallbackAnswer implements Answer
    public Void answer(InvocationOnMock invocation) throws Throwable
        CallbackTask taskArgument = (CallbackTask) invocation.getArguments()[0];
        return null;

What happens here?

When timer.schedule(…) is called, I am asking the CallbackTask to run immediately. Effectively making it a synchronous call.

Mocking File is a Pain in the@$$

so you have a program that needs to interact with the filesystem, but how do you mock it?

nio Files from JDK 7 is very handy, however due to the handy method being static, you cannot easily mock it (Except with PowerMock, but we will skip that for now).

so for places where I had to do
File myFile = new File(“…”)

I build a FileFactory to redirect the call
FileFactory fileFactory = new FileFactory() (which is injected to my component)
File myFile = fileFactory.getFile(“…”);

then I can mock my fileFactory
FileFactory mockFileFactory = mock(FileFactory.class);
File mockFile = mock(File.class);


Please let me know if you find a better way to mock the File, preferrably without too many wrappers, or how we could keep using static nio Files and not use PowerMock.

What’s also hard to test? Asynchronous method…..
I will write another post about it.

Skip the strip helicopter


Well, here’s a tip for vegas.

How can one get a birds eye view without paying for a helicopter ride ( which costs ~$100)?

If you are flying to north of vegas, take the right window seat (seat A), preferably row 10 or less, otherwise your view will be obstructed by the airplane wing.

Tip – do an online check in early ( up to 24 hours prior to flight) to ensure the best seat selection.

Specify Gradle version for Eclipse

Trying to import Gradle project into Eclipse, got an error

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘myProject\build.gradle’ line: 3
  • What went wrong:
    Could not compile build file ‘myProject\build.gradle’.

    startup failed:
    build file ‘myProject\build.gradle’: 3: unable to resolve class org.apache.ivy.plugins.resolver.URLResolver
    @ line 3, column 13.
    add(new org.apache.ivy.plugins.resolver.URLResolver()) {


Gradle version incompatibility – the URLResolver requires Gradle version 2.0 or lower.

Gradle version – If you do not specify a Gradle version, Eclipse uses the latest from services.gradle.org, which is 2.2 at time of posting.


To specify the version, add a folder “gradle” and underneath it another folder called “wrapper”. Then add a file “gradle-wrapper.properties” in the wrapper folder

..|_ gradle

like the above structure.

In gradle-wrapper.properties, put

Save the file.

In this case I am using 1.11. You may pick your version from https://services.gradle.org/distributions

Eclipse will use the specified version to build. Viola, no more errors. 🙂



Ghost files

Halloween has passed but I’ve been experiencing ghost files. I am trying to delete a file after reading it, but it gave me AccessDeniedException.
Here are the symptoms:

file.delete() returns false
file.exists() returns false

dir.listFiles(…) -> I see the file
in Windows Explorer -> I see the file

when I try to open the file in notepad++ – it says it doesn’t exists and asks if I want to create it — I click yes — and it says it cannot be created.

After some investigation, I have solved the mystery.

I had

turns out the stream wasn’t closed. So it was holding on to the file and kept it in a weird state.

So when I change it to

try(Stream<String> lines = Files.lines(Paths.get(filename))) {

The stream closes on exit, and I no longer get AccessDeniedException when I try to delete the file.

Although the Stream from Files gave me some hiccups (including not mockable when testing in Mockito because it is static), I still love it for its convenience.

Gradle Tutorial : Part 4 : Java Web Applications


Welcome to Part 4 of the Gradle Tutorial. This part takes off from Part 3 , where we covered building multiple interdependent Java projects.

LAST UPDATE : January 22, 2015 :
i) Fixed minor Text
ii) Updated code in Github
iii) Updated web page screenshot
iv) Fixed web/build.gradle : Added stopKey and stopPort to allow “gradle jettyStop” task to function correctly.

In this part of the tutorial, we shall look at building Java Web Applications via Gradle. As is the pattern, we shall have a multi-project scenario where we will have one Java Project that has some utility class and which is built separately. And then we have a Java Web Project that is dependent on this project and has JSP/Servlets and so on.

View original post 1,697 more words

ASP n00b

I have been doing almost exclusively Java development in previous years.

I started my new job and got thrown into ASP (VBScript), ASP.NET, IIS…


Debug – attach process

asp uses vbscript and asp.net (.cs) uses .NET

so in debug mode, pick Script or Managed code (v4.0, v4.5) accordingly

I hope no body needs to know about asp because asp is SOOOO old.

I find most success with enabling debugging on server & client side (settings in IIS), and let that do the work, instead of attaching process in Visual Studio

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

is where the cache is, delete that whenever you need to have a fresh state

Err object – reference

You can view description with Err.Description and maybe Err.Number (not always populated)

Eclipse, Maven, and JDK 1.8


You need to run build with JDK or have tools.jar on the classpath.If this occures during eclipse build make sure you run eclipse under JDK as well

Make sure you uninstall the older version of Java from your computer

I had Java 7 installed and then I installed Java 8 on top of it. Go to Program Features and remove all Java 7s.

Launch Eclipse with your JDK:

in eclipse.ini, add -vm [path to your 1.8 jdk] ABOVE -vmargs

In Eclipse, open Window > Perference > type Installed JREs

Add your JDK there as a Standard VM, ensure it is checked, apply.

If your maven project still give you the error, add the jdk to the build path of your project

Right click your project > Build Path > Configure Build Path > go to Libraries tab

Add Library > find your jdk that you added to Eclipse

move the JDK to the top, remove other JRE from the build path window.

My maven project can build without error now. 🙂