All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBusx.util.TimerRequest Maven / Gradle / Ivy

//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General
// Public License along with this library; if not, write to the
//
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA
// 02111-1307 USA
//
// The Initial Developer of the Original Code is Charles W. Rapp.
// Portions created by Charles W. Rapp are
// Copyright 2012, 2013, 2015, 2016, 2019, 2020. Charles W. Rapp
// All Rights Reserved.
//

package net.sf.eBusx.util;

import com.google.common.base.Strings;
import java.io.Serializable;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Formatter;
import net.sf.eBus.messages.ELocalOnly;
import net.sf.eBus.messages.EReplyInfo;
import net.sf.eBus.messages.ERequestMessage;

/**
 * Send this request to schedule a timer task. Each timer request
 * must have a unique name, that is, no two timer requests
 * currently running must have the same name. Timer
 * requests come in four kinds:
 * 
    *
  1. *
    TimerRequest.builder()
     *            .timerName("name")
     *            .time(Instant)
     *            .build()
    * a one-shot timer scheduled to expire on a given date and * time. *
  2. *
  3. *
    TimerRequest.builder()
     *            .timerName("name")
     *            .delay(long)
     *            .build()
    * a one-shot timer scheduled to expire after the specified * millisecond delay. *
  4. *
  5. *
    TimerRequest.builder()
     *            .timerName("name")
     *            .time(Instant)
     *            .period(long)
     *            .fixedRate(boolean)
     *            .build()
    * a repeating timer which first expires on a given date and * time and after that on a periodic millisecond rate. If the * {@code fixedRate} parameter is {@code true}, then the * timer is scheduled using * {@link java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, Date, long)}. * Otherwise, the timer is scheduled using * {@link java.util.Timer#schedule(java.util.TimerTask, Date, long)}. *
  6. *
  7. *
    TimerRequest.builder()
     *            .timerName("name")
     *            .delay(long)
     *            .period(long)
     *            .fixedRate(boolean)
     *            .build()
    * a repeating timer which first expires after a specified * millisecond delay and then on a periodic millisecond rate. * if {@code fixedRate} parameter is {@code true}, then the * timer is scheduled using * {@link java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, Date, long)}. * Otherwise, the timer is scheduled using * {@link java.util.Timer#schedule(java.util.TimerTask, Date, long)}. *
  8. *
* * @author Charles Rapp */ @EReplyInfo (replyMessageClasses = {TimerReply.class}) @ELocalOnly public final class TimerRequest extends ERequestMessage implements Serializable { //--------------------------------------------------------------- // Member data. // //----------------------------------------------------------- // Constants. // /** * Serialization version identifier. */ private static final long serialVersionUID = 0x050200L; //----------------------------------------------------------- // Locals. // /** * The requestor-assigned name for this timer request. */ public final String timerName; /** * If this timer is to expire at a given time, then set this * value. Otherwise, set to {@code null} and specify a * {@link #delay}. */ public final Instant time; /** * If this timer is to expire after a millisecond delay, then * set this value. Otherwise, set to zero and specify a * {@link #time}. */ public final long delay; /** * If this timer is repeating, then set this millisecond * period. Otherwise, set to zero for a one shot timer. */ public final long period; /** * If {@code true}, then repeating timers are run using fixed * rate scheduling. * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, Date, long) * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long) */ public final boolean fixedRate; //--------------------------------------------------------------- // Member methods. // //----------------------------------------------------------- // Constructors. // /** * Creates a new timer request instance with the given timer * name and expiration time. * @param timerName the timer name which serves as the * request message subject. * @param time expiration time. * @exception IllegalArgumentException * if {@code timerName} is either {@code null} or empty or if * {@code time} is {@code null}. * @see java.util.Timer#schedule(java.util.TimerTask, Date) */ @Deprecated public TimerRequest(final String timerName, final Instant time) throws IllegalArgumentException { super (ETimer.TIMER_SUBJECT, System.currentTimeMillis()); if (timerName == null || timerName.isEmpty() == true) { throw ( new IllegalArgumentException( "timerName is null or empty")); } else if (time == null) { throw (new IllegalArgumentException("null time")); } this.timerName = timerName; this.time = time; this.delay = 0L; this.period = 0L; this.fixedRate = false; } // end of TimerRequest(String, Instant) /** * Creates a new timer request instance with the given timer * name, expiration time and repeating millisecond period. * If {@code fixedRate} is {@code true}, then the timer is * scheduled using a fixed-rate interval. * @param timerName the timer name which serves as the * request message subject. * @param time expiration time. * @param period repeating millisecond period. * @param fixedRate if {@code true}, timer is run using * fixed-rate execution. * @throws IllegalArgumentException * if {@code timerName} is either {@code null} or empty or if * either {@code time} is {@code null} or {@code period} is * ≤ zero. * @see java.util.Timer#schedule(java.util.TimerTask, Date, long) * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, Date, long) */ @Deprecated public TimerRequest(final String timerName, final Instant time, final long period, final boolean fixedRate) throws IllegalArgumentException { super (ETimer.TIMER_SUBJECT, System.currentTimeMillis()); if (timerName == null || timerName.isEmpty() == true) { throw ( new IllegalArgumentException( "timerName is null or empty")); } else if (time == null) { throw (new IllegalArgumentException("null time")); } else if (period < 1L) { throw (new IllegalArgumentException("period < 1")); } this.timerName = timerName; this.time = time; this.delay = 0L; this.period = period; this.fixedRate = fixedRate; } // end of TimerRequest(String, Instant, long, boolean) /** * Creates a new timer request instance with the given timer * name and millisecond expiration delay. * @param timerName the timer name which serves as the * request message subject. * @param delay the millisecond expiration delay. * @throws IllegalArgumentException * if {@code timerName} is either {@code null} or empty or if * {@code delay} is ≤ zero. * @see java.util.Timer#schedule(java.util.TimerTask, long) */ @Deprecated public TimerRequest(final String timerName, final long delay) throws IllegalArgumentException { super (ETimer.TIMER_SUBJECT, System.currentTimeMillis()); if (timerName == null || timerName.isEmpty() == true) { throw ( new IllegalArgumentException( "timerName is null or empty")); } else if (delay < 1L) { throw (new IllegalArgumentException("delay < 1")); } this.timerName = timerName; this.time = null; this.delay = delay; this.period = 0L; this.fixedRate = false; } // end fo TimerRequest(String, long) /** * Creates a new timer request instance with the given timer * name, millisecond expiration delay and repeating * millisecond period. If {@code fixedRate} is * {@code true}, then the timer is scheduled using a * fixed-rate interval. * @param timerName the timer name which serves as the * request message subject. * @param delay the millisecond expiration delay. * @param period repeating millisecond period. * @param fixedRate if {@code true}, timer is run using * fixed-rate execution. * @throws IllegalArgumentException * if {@code timerName} is either {@code null} or empty or if * either {@code delay} or {@code period} is ≤ zero. * @see java.util.Timer#schedule(java.util.TimerTask, long, long) * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long) */ @Deprecated public TimerRequest(final String timerName, final long delay, final long period, final boolean fixedRate) throws IllegalArgumentException { super (ETimer.TIMER_SUBJECT, System.currentTimeMillis()); if (timerName == null || timerName.isEmpty() == true) { throw ( new IllegalArgumentException( "timerName is null or empty")); } else if (delay < 1L) { throw (new IllegalArgumentException("delay < 1")); } else if (period < 1L) { throw (new IllegalArgumentException("period < 1")); } this.timerName = timerName; this.time = null; this.delay = delay; this.period = period; this.fixedRate = fixedRate; } // end of TimerRequest(String, long, long, boolean) /** * De-serialization constructor. * @param subject the message subject. * @param timestamp the message timestamp. * @param timerName the user-assigned timer name. * @param time the expiration time. * @param delay the millisecond expiration delay. * @param period the repeating millisecond period. * @param fixedRate if {@code true}, timer is run using * fixed-rate execution. */ @Deprecated public TimerRequest(final String subject, final long timestamp, final String timerName, final Instant time, final long delay, final long period, final boolean fixedRate) { super (subject, timestamp); this.timerName = timerName; this.time = time; this.delay = delay; this.period = period; this.fixedRate = fixedRate; } // end of TimerRequest(...) private TimerRequest(final Builder builder) { super (builder); this.timerName = builder.mTimerName; this.time = builder.mTime; this.delay = builder.mDelay; this.period = builder.mPeriod; this.fixedRate = builder.mFixedRate; } // end of TimerRequest(Builder) // // end of Constructors. //----------------------------------------------------------- //----------------------------------------------------------- // Object Method Overrides. // /** * Returns a textual representation of this timer request * message. * @return the request message as text. */ @Override public String toString() { final Formatter retval = new Formatter(); retval.format("%s%n timer expires ", super.toString()); // Is this a time-based expiration? if (time != null) { // Yes. final DateTimeFormatter format = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM); retval.format("%s", format.format(time)); } // No, this is a millisecond delay expiration. else { retval.format("in %,d milliseconds", delay); } // Does this timer have a repeating period? if (period > 0L) { // Yes. retval.format( ", repeating every %,d milliseconds.", period); } // No. else { retval.format("."); } return (retval.toString()); } // end of toString() // // end of Object Method Overrides. //----------------------------------------------------------- /** * Returns a timer request builder instance. * @return message builder instance. */ public static Builder builder() { return (new Builder()); } // end of builder() //--------------------------------------------------------------- // Inner classes. // public static final class Builder extends ERequestMessage.Builder { //----------------------------------------------------------- // Member data. // //------------------------------------------------------- // Locals. // private String mTimerName; private Instant mTime; private long mDelay; private long mPeriod; private boolean mFixedRate; //----------------------------------------------------------- // Member methods. // //------------------------------------------------------- // Constructors. // private Builder() { super (TimerRequest.class); mDelay = Long.MIN_VALUE; mPeriod = 0L; this.subject(ETimer.TIMER_SUBJECT); } // end of Builder() // // end of Constructors. //------------------------------------------------------- //------------------------------------------------------- // Builder Method Overrides. // @Override protected TimerRequest buildImpl() { return (new TimerRequest(this)); } // end of buildImpl() @Override protected Validator validate(final Validator problems) { // The following parameter combinations are allowed: // + Time only, // + Time and period, // + Delay only, and // + Delay and period. // This means that either the time or the delay must // be set. return (super.validate(problems) .requireNotNull(mTimerName, "timerName") .requireTrue( (v1, v2) -> (v1 != null || v2 >= 0L), mTime, mDelay, "time", "delay", "either time or delay must be set") .requireTrue( (v1, v2) -> (v1 == null || v2 < 0L), mTime, mDelay, "time", "delay", "time and delay parameters cannot both be set")); } // end of validate(Validator) // // end of Builder Method Overrides. //------------------------------------------------------- //------------------------------------------------------- // Set Methods. // /** * Sets the timer name. * @param name timer name. * @return {@code this TimerReply} builder. * @throws IllegalArgumentException * if {@code name} is {@code null} or empty. */ public Builder timerName(final String name) { if (Strings.isNullOrEmpty(name)) { throw ( new IllegalArgumentException( "name is null or empty")); } mTimerName = name; return (this); } // end of timerName(String) public Builder time(final Instant time) { if (time == null) { throw (new NullPointerException("time is null")); } if ((Instant.now()).compareTo(time) > 0) { throw ( new IllegalArgumentException( "time in the past")); } mTime = time; return (this); } // end of time(Instant) public Builder delay(final long delay) { if (delay < 0L) { throw ( new IllegalArgumentException( "delay < zero")); } mDelay = delay; return (this); } // end of delay(long) public Builder period(final long period) { if (period <= 0L) { throw ( new IllegalArgumentException( "period <= zero")); } mPeriod = period; return (this); } // end of period(long) public Builder fixedRate(final boolean flag) { mFixedRate = flag; return (this); } // end of fixedRate(boolean) // // end of Set Methods. //------------------------------------------------------- } // end of class Builder } // end of class TimerRequest




© 2015 - 2024 Weber Informatics LLC | Privacy Policy