Saturday, February 9, 2013

Fun with Grails (part 1)

I've been playing on and off with a personal project written in Grails over the last few weeks.

 The Massachusetts Bay Transportation Authority (MBTA) has an API that provides real-time train information for the Red Line, Blue Line and Orange Line (sadly, nothing for the Green Line).

 I wrote a Grails application that displays real time Train Arrival information at Train Stops (the same data that the MBTA uses for their displays in some MBTA stations). I wrote this application because I take the Red Line to and from work and it would be nice to see real time data as I took the bus from home to the train station and as I walk to the Red Line after leaving work.

So far it works great.

 The project is still in it's infancy and the look and feel is pretty basic, but I will improve the appearance over the next few weeks.
It is currently deployed in the cloud (Cloudbees.net, a service we previously used for one of our projects at work).

http://wheresmytrain.wmk.cloudbees.net/

I will be doing a series of posts later on as the application evolves.

There are a lot of things that Grails and its plugin architecture enables (like using the Quartz plugin to poll MBTA data periodically, the MBTA User Agreement states that you cannot poll more than once every 10 seconds, and there are 3 endpoints, one for each line so I assume you cannot poll any ONE endpoint more than once every 10 seconds).


http://grails.org/plugin/quartz

The class that does the polling is as follows

package com.trainschedule
class GetScheduleBlueLineJob {

 static triggers = {  
  cron name:'cronTriggerBlue', startDelay:1000, cronExpression: '0/30 * * * * ?'
 }
  
 def scheduleInformationService
 def concurrent = false
 def grailsApplication
  
 def group = "MyGroupBlue"
   def execute(){
    log.info "called for blue info at ..." + new Date()
    scheduleInformationService.cacheNewDataForLineFromMBTA('blue')  
 }
}

No comments:

Post a Comment