Node Singleton Job

One job per node (instance)

  • If you want to run a service with one instance on every node in the cluster, Node Singleton job is the answer
  • Context Variable: job
  • Running Job Handle Variable: THIS


1) A Binary rule (CityFeeder)

Binary Rule

  • This rule takes a line separated file contains city name per line and pushes into a queue
def queue = grid.stringQueue("CITIES"); //create or get the queue

def lines = msg.lines();
int count = 0;

for(line in lines){
    def city = line.trim();
    if("".equals(city)) continue; //ignore blank lines
}"%d cities feeded", count);

2) NodeSingleton Job - (PollCityData)

  • This job takes the city names from the queue and fetch the weather data from the 3rd party API service
def API_KEY = domain.get("api_key"); //get the stored API key
def API_URL = "{city}&appid={api}&units={units}";
def queue = grid.stringQueue("CITIES"); //create or get the queue

try{"PollCityData running...");

        def city = queue.take(); //wait till a city name arrives in the queue
            def res = rest.get(API_URL)
                            .routeParam("city", city)
                            .routeParam("api", API_KEY)
                            .routeParam("units", "metric")
            def main = res.getObject().getJSONObject("main").toString();
  "Node:%s - %s", THIS.getNodeId(), main);

        }catch(Exception ex){

}finally{"PollCityData stopped.");

See Also

Related REST APIs

[Create / Update Job(ref:upsertjob)
Get Job
Delete Job
Count Jobs
List Jobs
Set Job State
Count Running Jobs
List Running Jobs
Start Job
Restart Job
Count All Running Jobs