Mother F**k the ScheduledExecutorService!

Thats right! Motherfuck this service.

Deep hidden in the javadoc of this magnificent class is this gem “If any execution of the task encounters an exception, subsequent executions are suppressed.”. In other words, if your runnable task has any fuckups, your task will no longer be run. What sucks about this, is there is no clear indication that there was a fuckup in the task. No warning, just silenty the task gets canceled leading you to say: “Dude! where my task!?”
Read the rest of this entry »

JDK Thread Pool Configuration

Introduction

In any non-trivial application of somewhat significant size you will need Executors to background processing or asynchronous processing, task splitting etc. This is where the JDK executors framework comes in.

Read the rest of this entry »

Identifying which Java Thread is consuming most CPU

I didn’t come up with this. I was shown how to do this by an esteemed college at work.

Introduction

Most (if not all) productive systems doing anything important will use more than 1 java thread. And when something goes crazy and your cpu usage is on 100%, it is hard to identify which thread(s) is/are causing this. Or so I thought. Until someone smarter than me showed me how it can be done. And here I will show you how to do it and you too can amaze your family and friends with your geek skillz.

Read the rest of this entry »

An Improved RMI Tutorial with Eclipse

Introduction

There are/have-been heaps of remoting frameworks in java, but RMI being part of the JRE/JDK and therefore having no external dependencies is my personal preference for remoting with java. Its main drawback: the wire protocol is not web-friendly and therefore difficult to go through firewalls (although it is possible). But if used behind the firewall, it makes for an excellent way to do distributed computing using only the JDK (OMG! only the JDK?! no Spring? or JMS?!). It does have several killer features that are found in very few (if any) remoting frameworks: callbacks and remote classloading. In this tutorial, you will see remote classloading. Ever since JRE 5.0, you don’t need to compile stubs (meaning you don’t need an extra compile-time step to get RMI working). I am guessing someone decided to do away with those and use the jdk dynamic proxies.

Read the rest of this entry »

Simulating dropped packets (aka crappy internets) with iptables

Disclaimer #1

I am not an iptables expert. This tip will work best on your local linux development box where you probably have no iptables rules setup. Do not play around with this shit on production machines, unless you know what you are doing. Speak with your friendly sysadmins before doing this on any machine for which they feel responsible.

Disclaimer #2

According to this tip, do not drop more than 14% of the packets otherwise you will cause all tcp sockets to stall.

Introduction

Why would you want to do this? sometimes people (aka customers) might complain that your super-duper application that they use via the Internets (aka a series of tubes) is slow. One reason could be a dodgy internet connection and/or packet loss. See this great article about how packet-loss affects web applications and how to drop packets with a microwave oven.

And now finally…

To simulate a dropped packets with iptables, you can use the following commands (as root):

# for randomly dropping 10% of incoming packets:
iptables -A INPUT -m statistic --mode random --probability 0.1 -j DROP

# and for dropping 10% of outgoing packets:
iptables -A OUTPUT -m statistic --mode random --probability 0.1 -j DROP

Once done, you can use the following for removing these packet-drops:

# for the incoming packets:
iptables -D INPUT -m statistic --mode random --probability 0.1 -j DROP

# and for the outgoing packets
iptables -D OUTPUT -m statistic --mode random --probability 0.1 -j DROP

Auto Install the SUN JDK on Linux

Sometimes (yes it does happen), you need to install the JDK from SUN automatically (without having to answer yes/no to the license). Why? For example in a Kick Start script to auto-install the JDK.

And so to do this, here a bash & perl script for this exact purpose. This script works only with the .bin install package that you download from SUN’s website.

#!/bin/bash

jdkBinFile=$1

echo $1

# just spew out the agreement
perl -p -i -e 's/^more/cat/g' $1

# set the 'agreed' value for jdk 1.5
perl -p -i -e 's/\s+agreed=$/agreed=1/g' $1

# set the agree value for jdk 1.6
perl -p -i -e 's/`agree`/yes/g' $1

# do not call the register_jdk method
perl -p -i -e 's/\s+register_JDK/#register_JDK/g' $1

# and now run the installation
bash $1

How to

Create a new script file called autoinstall.sh containing the above script. and run it as follows:

$ cd /install/to/directory
$ autoinstall.sh /my/downloads/jdk-6u16-linux-x64.bin

Ruby FIX Message Viewer 1.0

Today I released my Ruby FIX Message Viewer 1.0. This is a command line based FIX message viewer programmed in Ruby. I made it command line because most, if not all, of the FIX logs that I have at work are on a server when I have only ssh access. Rather than copy them over and use some GUI tools, I find it quicker just to use this tool on the server.

scp2here: scp command to here

I usually need to scp stuff from/over-to servers on a regular basis. And here is a function in .bashrc that I use:

#function to get the scp path to here
function scp2here
{
    user=`whoami`
    server=`hostname`
    pathToHere=`pwd`

     output=$user"@"$server":"$pathToHere

    if [ ! -z "$1" ]
    then
        output=$output"/"$1
    fi

    echo $output
}

This function prints out the scp command argument to copy a file/directory to a location.

Examples

Then to copy something to a target server where I have scp2here, I can cd to the target directory and do this:

srasul@some.cool.host ~ $ scp2here
srasul@some.cool.host.com:/home/srasul
...
srasul@another.cooler.host ~ $ scp bigassfile.txt srasul@some.cool.host.com:/home/srasul

Or if I want to copy a file from a location that has scp2here:

srasul@some.cool.host ~ $ scp2here somefile.txt
srasul@some.cool.host.com:/home/srasul/somefile.txt
...
srasul@another.cooler.host ~ $ scp srasul@some.cool.host.com:/home/srasul/somefile.txt .


                    

How to import java projects with eclipse JDT

During the coding of my Bulk Import Plugin, I had the needs to import java projects into a workspace via code. Via Eclipse’s API to be precise. After reading the code of what happens when you import a project, here is the critical bit of code:

Runnable runnable = new Runnable() {
    public void run() {
    try {
        IPath projectDotProjectFile = new Path(pathToMyProjectDir+ "/.project");
            IProjectDescription projectDescription =
                workspace.loadProjectDescription(projectDotProjectFile);
            IProject project = workspace.getRoot().
                getProject(projectDescription.getName());
            JavaCapabilityConfigurationPage.createProject(project, projectDescription
                .getLocationURI(), progressMonitor);
        }
        catch(CoreException e) {
            e.printStackTrace();
        }
    }
};

// and now get the workbench to do the work
final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getDisplay().syncExec(runnable);

Eclipse Bulk Import V 1.0

Today I released the V 1.0 of my Eclipse Bulk Import plugin. The source is also available at github.

I created this plugin because at work we have too many eclipse projects (over 200). And sometimes the workspaces get corrupt. Using this plugin, I can save the locations of the current projects in my workspace. And I can also import those projects with one-click. Check out the movie of it in action. This plugin saves me quite a bit of time in creating new workspaces (and then importing over 200 projects), and recovering from corrupted workspaces.

Certified “Works on my machine” :) . If you try it out let me know how it goes.