Tuesday, December 18, 2012

Active Batch File Trigger

Active Batch manages to take automation to all new level by providing cool ways to handle automation.One of which is Event Based Triggers.Active Batch provides quite a few but most important of events to be configured as triggers which can be used to execute any code/work flow/functionality.

Mentioned are event triggers provided by Active Batch-
  • Email Trigger
  • File Trigger
  • Growl Trigger
  • JMS Event Trigger
  • JMX Event Trigger
  • MSMQ Trigger
  • System Startup Trigger
  • Twitter Trigger
  • Web Service Trigger
  • WMI Event Trigger
Most of us have seen majority of them, but personally I found Twitter Trigger very interesting.It allows me to trigger numerous different functionality by just tweeting a pre defined tweet from any where in the world.While most of them are supposed to be used in production environment, triggers like email and twitter provide simple and secure platform to control from any where via open Internet.

I have used most of the above but the most frequently used trigger was "File Trigger". This trigger allows to configure an event which is fired whenever a file is manipulated inside a configured directory.

Its very easy to configure a file based event, just mention the directory path ,Filter,execution queue,user account.





Its works like a charm !
However I faced hell of issue when i need to deploy the same solution in different environment.In that case the directory path was a configurable variable which is environment specific and its value was modified after every deployment as part of localization. But every time after deployment the file trigger never worked. I investigated on this further and found for some reason the triggers are not taking new localized values and the only way to make it work was to open the trigger and save it back by making small reversible change.This was frustrating as every time there is deployment, manual effort is required and we had load of environment.Thankfully ABAT exposes powerful inter op assemblies to access ABAT objects pro-grammatically.

Below is the c# code which will help to refresh the file trigger-

public ReArmFileTrigger()
{//connect to job scheduler

AbatJobScheduler scheduler = new AbatJobScheduler();
scheduler.Connect("<JobSchedulerName>");//search jobs

AbatObjectsLite GJOBS = scheduler.Search("/", "*", 3, "Name;", true);
foreach(object lJob in GJOBS) {if (lJob is IAbatJobLite) {
IAbatJobLite aj = (IAbatJobLite)lJob;
if (aj.Enabled.Equals(Boolean.TrueString)) {
refreshJobtrigger(aj.GetAbatObject());
}
}if (lJob is AbatPlanLite) {
AbatPlanLite aj = (AbatPlanLite)lJob; refreshJobtrigger(aj.GetAbatObject());
}

}

}

void refreshJobtrigger(Job50 job) {
try {EventTriggers50 ev = job.GetEventTriggers();
foreach (EventTrigger50 e in ev) {
if (e.Type == enumJobEventTriggerType.abatJETT_File) {
string eventLabel = e.Label;e.Label = "Rearmed";
job.Update();
e.Label = eventLabel;
job.Update();

Console.WriteLine(job.Name + " Re-armed ");

}


}

}

catch(Exception e) {
Console.WriteLine(" Exception Occurred "+e.ToString());
}


}
void refreshJobtrigger(Plan50 plan)
{
EventTriggers50 ev= plan.GetEventTriggers();
try
{
foreach (EventTrigger50 e in ev)
{
if (e.Type == enumJobEventTriggerType.abatJETT_File)
{
string eventLabel = e.Label; e.Label = "Rearmed";
plan.Update();
e.Label = eventLabel;
plan.Update();
Console.WriteLine(plan.Name+ " Re-armed ");
}


}
}
catch (Exception e)
{
Console.WriteLine(" Exception Occurred " + e.ToString());
}


}
This can be executed whenever the file trigger need to be refreshed.It modifies the label by some junk value and then reverts back the change. However its trying to update the job twice in this code  which is just to give example and is just a waste of CPU cycles which can be optimized depending on requirement.

This can also be converted as BAT/Ps1/VBs and can be integrated as  Active Batch plan.



2 comments:

  1. Hi Anup,

    My client is using Active batch , I am new to active batch , need your help

    Req: Suppose If my workflow is running 4 hrs i need to send mail every 2 hrs

    Thnaks in Advance

    ReplyDelete
  2. Hi,

    could you please share where i can find ABAT inter op assemblies and documentation about search method to search on folders.

    ReplyDelete