5.7.3 Developer Tips

 

General Tips

General Tips


NmsPolicyAPI api = (NmsPolicyAPI)NmsUtil.getAPI("NmsPolicyAPI");

PolicyObject po = new WebNmsBackupPolicy(); po.setName("WebNmsBackupPolicy");

Vector v = api.getPolicyNames() // gives you names of all

available policy objects

if(!(v.contains("WebNmsBackupPolicy")))

{

// If object doesn't exist add it

api.addPolicy(po);

}

else

{

// don't add as the policy objects already exists. This will help you to avoid adding same object again during warm start.

 


      // create a new Scheduler or use existing schedulers in WebNMS

      // for creating a Scheduler

      Scheduler sch = Scheduler.createScheduler("Test",4);

      sch.start();

      // write a class implementing Runnable let's call it as MyRunnable

      Runnable run = new MyRunnable();

      // To schedule it after (say)10 seconds from current time

      long timeToSchedule = System.currentTimeMillis() + 10000;

      sch. scheduleTask(run, timeToSchedule);

      // once it is scheduled after 10 seconds run method of your Runnable

      implementation class MyRunnable will be called

      public void run()

      {

      // check whether the task is over

      boolean completed = <check the operation>

      // if the task is completed don't do anything else reschedule the same

      class again for next 10th second. Continue this operation till the task is

      over

      if(!completed)

      {

      long timeToSchedule = System.currentTimeMillis() + 10000;

      sch. scheduleTask(this, timeToSchedule);

      }

      }

For further details refer to the documentation for scheduling services and the Javadocs of Scheduler.

 




To schedule a non-periodic policy, you can use the method 'setTimeVector()' available in the class PolicyObject. Please look here for details on Time Vector (com.adventnet.management.policydb.PolicyObject.setTimeVector()). We provide below a code snippet to show how you can execute a policy (this is for executing a policy at any point of time and not for scheduling).

 

        /* 'napi' denotes "NmsPolicyAPI" , 'examplepolicy' denotes the name of the policy*/

 

PolicyEvent pe = new PolicyEvent(napi.getPolicy("examplepolicy"));

pe.addPolicyNamesToTrigger("examplepolicy");

napi.executePolicy(pe);

                

TimeVectorStructure: TimeVector in PolicyObject contains information about scheduling pattern configured for it. TimeVector contains either

In each and every vector, information about scheduling hours will be stored as Integer Objects.

 

For example: If you have scheduled PolicyObject at 1, l5 and 20 hrs for 5 dates then time vector looks as

{

    [1, 15, 20]

    [1, 15, 20]

    [1, 15, 20]

    [1, 15, 20]

    [1, 15, 20]

}

 

Here 1, 5 and 20 are IntegerObjects holding the value 1 , 5 and 20 respectively. If you have scheduled PolicyObject as

     1st date 1,10 hrs

     6th date 18 , 22 hrs

     9th date  5 , 16 hrs

then the vector will look like

{

[1,10]

[]

[]

[]

[]

[18,22]

[]

[]

[5,15]

:

:

:

[]

}

 

In case of day wise scheduling, Vector size will be seven. If you are going to schedule the Policy in day wise mode as

Sunday 3, 6 hrs, Monday 6, 7 hrs, Tuesday 4, 9 hrs, Wednesday 9, 11 hrs, Friday 9, 11 hrs, Saturday 3, 13 hrs, then timeVector looks as

{

[ 3, 6]

[ 6 , 7]

[ 4 , 9]

[ 9 , 11]

[]

[9,11]

[3 , 13]

}

If no time is mentioned for a particular day / date then the corresponding vector size for that particular day / date is zero


You must use NonPeriodic policy to achieve the above requirement. To schedule certain policies for execution at specific hours, you need to use Non Periodic Policy. This sometimes appear to be Periodic Policy, but is actually a non periodic policy. Periodic Policy is for scheduling a policy for execution at periodic intervals from the time of server startup. For example, if you specify the time interval as 30 seconds, the policy is scheduled for execution for every 30 seconds from server startup.

 

As per the asked query, this requirement can be achieved only through non-periodic policy for the following reason. Assume the server is started on Sunday, 3:00 PM, and is scheduled for execution for the first time on Monday, 1:00 AM which is 10 hrs from server startup. Again on Tuesday, it is scheduled for 1:00 AM. Here, the time interval becomes 24 hrs. Hence, it can be inferred that this type of scheduling can be executed only by non periodic policy. In this case, choose the option 'day' as 'All Days' and timing as '1:00 A.M' to achieve your requirement.


 



Copyright © 2011, ZOHO Corp. All Rights Reserved.