FileDocCategorySizeDatePackage
BillingClient.javaAPI DocExample6893Thu Mar 16 11:52:50 GMT 2000None

BillingClient

public class BillingClient extends UnicastRemoteObject implements RemoteEventListener

Fields Summary
MailboxRegistration
mailbox
LeaseRenewalSet
renewalSet
boolean
doneProcessing
int
numRecords
Constructors Summary
public BillingClient(String logDir)

        BillingClientLogHandler bclh = new BillingClientLogHandler();
        ReliableLog log = new ReliableLog(logDir, bclh);
        log.recover();
        if (mailbox == null)
            initServices();
        renewalSet.renewFor(mailbox.getLease(), TimeConstants.DAYS * 3);
        processRecords();
        log.snapshot();
        System.exit(0);
    
Methods Summary
private voidinitServices()

        if (mailbox == null) {
            // First time we've run -- find all the services and ask for
            // conversion events to be send to us

            // Find the event mailbox service and register with it
            ServiceFinder sfem = new ServiceFinder(EventMailbox.class);
            EventMailbox em = (EventMailbox) sfem.getObject();
            mailbox = em.register(Long.MAX_VALUE);

            // Find the lease renewal service, and use it to renew the
            // mailbox lease. We intend to run every day, but set the
            // renewal time to 3 days if we don't run over a weekend.
            ServiceFinder sflrs = new ServiceFinder(LeaseRenewalService.class);
            LeaseRenewalService lrs = (LeaseRenewalService) sflrs.getObject();
            renewalSet = lrs.createLeaseRenewalSet(TimeConstants.DAYS * 3);

            // Find the convert service and ask that it send events to the
            // mailbox forever (when we cancel the lease renewal set, this
            // lease will get cancelled).
            ServiceFinder sf = new ServiceFinder(ConvertService.class);
            ConvertService cs = (ConvertService) sf.getObject();
            EventRegistration er = cs.trackConversions(Long.MAX_VALUE,
                                    mailbox.getListener(), null);
            renewalSet.renewFor(er.getLease(), Long.MAX_VALUE);
        }
    
public static voidmain(java.lang.String[] args)

        System.setSecurityManager(new RMISecurityManager());
        new BillingClient(args[0]);
    
public synchronized voidnotify(RemoteEvent re)

        if (!(re instanceof ConvertEvent))
            throw new UnknownEventException("BillingClient");
        doneProcessing = false;
        ConvertEvent ce = (ConvertEvent) re;
        long seq = ce.getSequenceNumber();
        int val = ce.getValue();
        System.out.println("Convert #" + seq + ": " + val);
        numRecords++;
    
private voidprocessRecords()

        try {
            mailbox.enableDelivery(this);
        } catch (RemoteException re) {
            System.out.println("Can't enable mailbox delivery");
            System.exit(0);
        }
        doneProcessing = false;
        synchronized(this) {
            while (!doneProcessing) {
                // Set done to true; if its still true after 30 seconds then
                // we're done. The notify() method will set it to false, in
                // which case we'll try again.
                doneProcessing = true;
                try {
                    wait(30 * 1000);
                } catch (InterruptedException ie) {}
            }
        }
        System.out.println("Processed " + numRecords + " records");
        try {
            mailbox.disableDelivery();
        } catch (RemoteException re) {
        }