Hi! Welcome...

Thread.currentThread().join() I am Ashish. I am Member of Apache MINA PMC, ASF Committer and avid Code hacker.

23 August 2011 ~ 0 Comments

Updating Google Calendar with MS Project Tasks – 2 – Revisited


This is the 2nd part in the series. Please refer to Updating Google Calendar with MS Project Tasks - Revisited for Part 1, including how to build the code.

In the previous post we saw, how to list all the Calendar's. In this post we shall build upon the code to list all the calendar's for a given user account, and update the MS Project tasks to the selected calendar.

We shall need to following three steps:

  • List Calendars and select one
  • Parse MS Project tasks and convert them to Calendar events
  • Update Google Calendar with Events


Step 1: List All calendar's

We have already seen the code for this in previous post

Step 2: Parse MS Project task and convert to Calendar Event

Lets look at the code for the conversion

protected CalendarEventEntry convertTaskToCalenderEntry(Task task) {
    System.out.println("Task "+task);
    CalendarEventEntry eventEntry = new CalendarEventEntry();
    eventEntry.setTitle(new PlainTextConstruct(task.getName()));

    When date = new When();
    date.setStartTime(new DateTime(task.getStart()));
    eventEntry.addTime(date);
    return eventEntry;
}



The code takes Start and End date of task, as well as title, and creates an CalendarEventEntry for the same. There can be additional logic built into this, like filtering events based on some rules etc.

Step 3: Updating entries into Google Calendar

public void updateCalenderWithEntry(List<Task> tasks, CalendarService calendarService, URL url) {
    for (Task task : tasks) {
        CalendarEventEntry entry = convertTaskToCalenderEntry(task);
        try {
            calendarService.insert(url, entry);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServiceException e) {
            e.printStackTrace();
        }
    }
}



Here we just iterate through the tasks and update the events using CalendarService. The Calendar URL can be retrieved from the CalendarEntry class.

A complete working example is part of com.ashishpaliwal.mpputils.examples.MppToGoogleCalendar

Using the sample program

>com.ashishpaliwal.mpputils.examples.MppToGoogleCalendar [MPP File] [Google User Name] [password]

What's Next?

Eager to hear from user, what they would like to see. Adding a UI and out-of-box working package is something that would be good. However, please do try using this and I shall try my best to improve this.


10 August 2011 ~ 0 Comments

Hunting down CPU hogging Java Thread


Most of us have encountered a situation to find cause of high CPU usage in Java application. Profiling is the best way, but at times running Profiler in production is not an option. Fortunately, there is a simple way, if you are running your app on *nix.

Lets explore how to find this.

  • Find the pid of the application, using top or jps command
  • Once you get the pid, run following command
    $ ps -L pid
  • We get an output as shown in the figure

    ps output

    The output displays all the Threads in the application along with the time spent. Find the Thread that has spent highest time in execution (Entry circled on right). Once we identify this, get the LWP ID of the Thread (Entry circled on left).

  • Using jstack or visualvm, take a Thread dump.
  • Convert LWP ID to Hex and search for the ID in Thread dump.

Then you can narrow down the thread which is consuming max CPU and investigate further.

Tags: , ,

11 July 2011 ~ 2 Comments

Updating Google Calendar with MS Project Tasks – Revisited


Back in 2008, wrote the post on Updating Google Calendar with MS Project. Since then, lot many people had asked for the code, and myself felt that getting the code on github would be great. I shall keep complete the post in 2 parts, starting with setup, listing the calendars and then winding up with updating Google Calendar with MS Project entries.

To begin, you can get the code form github at following location

https://github.com/paliwalashish/mpputils

Building the code

To build the code and download the dependencies, please refer to the wiki page https://github.com/paliwalashish/mpputils/wiki/Build

Retrieving Calendar's

Now lets move to retrieving the list of all the Calendar's for a user
The code is simple enough, here is the function which does the

public static List<CalendarEntry> getAllCalendars(String userName, String password) throws Exception {
        CalendarService myService = new CalendarService("CalendarService-"+userName);
        myService.setUserCredentials(userName, password);

        // Send the request and print the response
        URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
        CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);

        return resultFeed.getEntries();
    }


This snippet shall return all the Calendar's for the specified user account.

The code has an example of using the same. Please refer to com.ashishpaliwal.mpputils.examples.ListAllCalendars class on the usage.

In the next part we shall see the code for updating of Google Calendar with MS Project task.


13 May 2011 ~ 0 Comments

A Simple LRU Cache


Recently, while working on a module has a need of very light weight fixed size LRU cache. There are definitely lot of caching solutions around, but decided to try a simple Map based solution. Surprisingly, the solution using LinkedHashMap turned out to be simplest.

Here is the snippet. Removed all additional validation and other stuff for simplicity.

public class LruCache {
    // cache data holder
    Map<Object, Object> cache;

    // Max size
    private int maxCacheSize;

    public LruCache(int maxSize) {
        this.maxCacheSize = maxSize;
        cache = new LinkedHashMap(maxSize, 0.75f, true) {
            @Override
            protected boolean removeEldestEntry(Map.Entry eldest) {
                return size() > maxCacheSize;
            }
        };
    }

    public void put(Object key, Object value) {
        cache.put(key, value);
    }

    public Object get(Object key) {
        return cache.get(key);
    }
}

The implementation is fairly simple. The third parameter while initialization of LinkedHashMap is the key. a value of "true" means the implementation should use access-order while evicting the entry.

17 February 2011 ~ 0 Comments

Terracotta Toolkit – Part 4 (Clustered Barrier)


Recently I had a need to coordinate test drivers across JVM's, so that they all fire the load at the same time. There were many options, needed flexibility in terms that each had its own warm-up time and other dependencies. Terracotta's Clustered Barrier was a natural choice for this. Reason's were simple, I had already seen it working and it was simple enough to integrate it within application.

What you need to run this sample
1. Terracotta Release Download here
2. Include terracotta-toolkit-1.1-runtime-2.1.0.jar in classpath. You can find the jar in terracotta_install/common folder

The image below describes the scenario

Scenario

Lets see how we use the Toolkit barrier to achieve this

public class ClusteredBarrierSample {
    static final String BARRIER_NAME = "RUN_BARRIER";

    Barrier barrier;

    // Handle to the toolkit
    ClusteringToolkit clusterToolkit = null;

    public void initializeToolKit(String serverAdd, int barrierParties) {
        clusterToolkit = new TerracottaClient(serverAdd).getToolkit();
        barrier = clusterToolkit.getBarrier(BARRIER_NAME, barrierParties);
    }

    /**
     * waits till all the parties have joined and once all drivers are ready
     * fires the test
     */
    public void fireTest() {
        try {
            barrier.await();
            // fire my network drivers
            System.out.println("firing the driver");
        } catch (Exception e) {
            e.printStackTrace();
       }
    }

    public static void main(String[] args) {
        ClusteredBarrierSample barrierSample = new ClusteredBarrierSample();
        barrierSample.initializeToolKit(args[0], Integer.parseInt(args[1]));
        barrierSample.fireTest();
    }
}

The implementation is straight simple and in 3 steps
1. Intialize Terracotta Client
2. Get the Barrier, with number of parties to wait for
3. Call await() on the barrier

Using it was pretty simple and took less than 5 minutes to have the requirements met :)

Next Steps:

Refer Terracotta Documentation