package examples.time;

import weblogic.time.common.*;
import weblogic.common.*;
import weblogic.jndi.*;
import javax.naming.*;
import java.util.*;

/**
 * This class illustrates use of client-side timers.  By defining a "schedule" method
 * and a "trigger" method, a scheduled recurring action can be established.  Note that
 * The "schedule" method is called for in each rescheduling operation, allowing arbitrarily
 * complex scheduling semantics. In this example, the timer starts out at a 0 second
 * delay, but with each event, the delay is increased by 1 second. When it reaches 10,
 * the trigger is cancelled.
 * <p>
 * Assuming that the server is running on port 7001 of the local machine, 
 * you would run this example using the following command:
 * <br>
 * <pre>  java examples.time.ClientTimer t3://localhost:7001</pre>
 *
 *
 * @author                    Copyright (c) 1996-2000 by BEA Systems, Inc.
 *                            All Rights Reserved.
 */

public class ClientTimer implements Schedulable, Triggerable {
  
  private int delay; // in milliseconds
  private boolean running = true;
  
  public ClientTimer(T3ServicesDef services) {
    try {
      ScheduledTriggerDef std = null;
      std = services.time().getScheduledTrigger(this, this);
      delay = 0;
      std.schedule();
    } catch (TimeTriggerException ttx) {
      ttx.printStackTrace();
      delay = 10000;
    }
  }
  
  // Schedulable Interface
  
  public long schedule(long t) {
    System.out.println("-----------------------------------------");
    if (delay > 10000) {
      System.out.println("Cancelling Timer");
      this.running = false;
      synchronized(this) {
	notify();
      }
      return 0;
    } else {
      System.out.println("Scheduling Next Callback, delay="+ delay/1000 + " seconds");
      return t+delay;
    }
  }
  
  
  // Triggerable Interface
  
  public void trigger(Schedulable sched) {
    System.out.println("Trigger Called");
    delay += 1000;
  }
  
  
  /**
   * This application connects to the WebLogic Server, and sets up a 
   * client-side scheduled trigger (a recurring action).  The 
   * main thread waits until the timer is cancelled.
   */
  public static void main(String argv[]) 
       throws Exception 
  {
    T3ServicesDef t3s;
    
    if (argv.length != 1) {
      System.out.println("Usage:   java examples.time.ClientTimer t3url");
      System.out.println("Example: java examples.time.ClientTimer t3://localhost:7001");
      return;
    }
    
    try {
      t3s = getT3Services(argv[0]);
    } catch (NamingException ne) {
      ne.printStackTrace();
      return;
    }
    
    ClientTimer ct = new ClientTimer(t3s);
    
    // Wait until timer has completed before exiting.
    while( ct.running == true ) {
      synchronized(ct) {
	try {
	  ct.wait();
	} catch (InterruptedException ie) {}
      }
    }
  }  
  
  private static T3ServicesDef getT3Services(String wlUrl) 
       throws javax.naming.NamingException
  {
    T3ServicesDef t3s;
    Hashtable env = new Hashtable();
    env.put(Context.PROVIDER_URL, wlUrl);
    env.put(Context.INITIAL_CONTEXT_FACTORY,
            weblogic.jndi.WLInitialContextFactory.class.getName());
    Context ctx = new InitialContext(env);
    t3s = (T3ServicesDef) ctx.lookup("weblogic.common.T3Services");
    ctx.close();
    return(t3s);
  }

}

