joynr.PeriodicSubscriptionQos Maven / Gradle / Ivy
package joynr;
/*
* #%L
* %%
* Copyright (C) 2011 - 2016 BMW Car IT GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import io.joynr.pubsub.HeartbeatSubscriptionInformation;
import io.joynr.pubsub.SubscriptionQos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Class representing the quality of service settings for subscriptions based on
* time periods.
*
* This class stores quality of service settings used for subscriptions to
* attributes in generated proxy objects. Notifications will only be sent
* if the period has expired. The subscription will automatically expire after
* the expiry date is reached. If no publications were received for alertAfter
* interval, publicationMissed will be called.
*/
public class PeriodicSubscriptionQos extends SubscriptionQos implements HeartbeatSubscriptionInformation {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(PeriodicSubscriptionQos.class);
/**
* Value for alertAfterInterval to disable alert: 0
*/
private static final long NO_ALERT_AFTER_INTERVAL = 0L;
/**
* Minimum value for period in milliseconds: 50
*/
private static final long MIN_PERIOD_MS = 50L;
/**
* Maximum value for period in milliseconds: 2.592.000.000 (30 days)
*/
private static final long MAX_PERIOD_MS = 30L * 24L * 60L * 60L * 1000L; // 30 days
/**
* Default value for period in milliseconds: 60.000 (1 minute)
*/
private static final long DEFAULT_PERIOD_MS = 60L * 1000L;
/**
* Maximum value for alertAfterInterval in milliseconds: 2.592.000.000 (30 days)
*/
private static final long MAX_ALERT_AFTER_INTERVAL_MS = 30L * 24L * 60L * 60L * 1000L; // 30 days
/**
* Default value for alertAfterInterval in milliseconds: no alert
*/
private static final long DEFAULT_ALERT_AFTER_INTERVAL_MS = NO_ALERT_AFTER_INTERVAL;
private long periodMs = DEFAULT_PERIOD_MS;
private long alertAfterIntervalMs = DEFAULT_ALERT_AFTER_INTERVAL_MS;
/**
* Default Constructor
*/
public PeriodicSubscriptionQos() {
}
/**
* Constructor of PeriodicSubscriptionQos objects with full parameter set.
* @deprecated This constructor will be deleted by 2017-01-01.
* Use the fluent interface instead:
* new PeriodicSubscriptionQos().setPeriodMs(periodMs)
* .setValidityMs(validityMs)
* @param periodMs
* The provider will send notifications every period in milliseconds
* independently of value changes.
* @param expiryDateMs
* the end date of the subscription until which publications will
* be sent. This value is provided in milliseconds
* (since 1970-01-01T00:00:00.000).
* @param alertAfterIntervalMs
* defines how long to wait for an update before publicationMissed
* is called if no publications were received.
* @param publicationTtlMs
* time to live for publication messages
*
* @see #setPeriodMs(long)
* @see #setAlertAfterIntervalMs(long)
* @see SubscriptionQos#SubscriptionQos(long, long)
* SubscriptionQos.SubscriptionQos(long, long)
* for more information on expiryDate and publicationTtl
*/
@Deprecated
public PeriodicSubscriptionQos(long periodMs, long expiryDateMs, long alertAfterIntervalMs, long publicationTtlMs) {
super(expiryDateMs, publicationTtlMs);
setPeriodMs(periodMs);
setAlertAfterIntervalMs(alertAfterIntervalMs);
}
/**
* Constructor of PeriodicSubscriptionQos objects with specified period, expiry
* date, and publicationTtl.
* @deprecated This constructor will be deleted by 2017-01-01.
* Use the fluent interface instead:
* new PeriodicSubscriptionQos().setPeriodMs(periodMs)
* .setValidityMs(validityMs)
* @param periodMs
* The provider will send notifications every period in milliseconds
* independently of value changes.
* @param expiryDateMs
* the end date of the subscription until which publications will
* be sent. This value is provided in milliseconds
* (since 1970-01-01T00:00:00.000).
* @param publicationTtlMs
* time to live for publication messages
*
* @see #setPeriodMs(long)
* @see SubscriptionQos#SubscriptionQos(long, long)
* SubscriptionQos.SubscriptionQos(long, long)
* for more information on expiryDate and publicationTtl
* @see #setAlertAfterIntervalMs(long)
* (alertAfterInterval will be set to its default value)
*/
@Deprecated
public PeriodicSubscriptionQos(long periodMs, long expiryDateMs, long publicationTtlMs) {
this(periodMs, expiryDateMs, DEFAULT_ALERT_AFTER_INTERVAL_MS, publicationTtlMs);
}
/**
* @deprecated Use getAlertAfterIntervalMs instead
*
* Get the alertAfterInterval in milliseconds.
* If no notification was received within the last alert interval, a missed
* publication notification will be raised.
*
* @return The alertAfterInterval in milliseconds. If more than
* alertAfterInterval milliseconds pass without receiving a message,
* the subscriptionManager will issue a publicationMissed. If set
* to 0, never alert.
*/
@Override
@Deprecated
public long getAlertAfterInterval() {
return getAlertAfterIntervalMs();
}
/**
* Get the alertAfterInterval in milliseconds.
* If no notification was received within the last alert interval, a missed
* publication notification will be raised.
*
* @return The alertAfterInterval in milliseconds. If more than
* alertAfterInterval milliseconds pass without receiving a message,
* the subscriptionManager will issue a publicationMissed. If set
* to 0, never alert.
*/
@Override
public long getAlertAfterIntervalMs() {
return alertAfterIntervalMs;
}
/**
* @deprecated Use setAlertAfterIntervalMs instead
* Set the alertAfterInterval in milliseconds.
* If no notification was received within the last alert interval, a missed
* publication notification will be raised.
*
* Minimum, Maximum, and Default Values:
*
* - The absolute minimum setting is the period value.
* Any value less than period will be replaced by the period setting.
* - The absolute maximum setting is 2.592.000.000 milliseconds (30 days).
* Any value bigger than this maximum will be treated at the absolute maximum setting of
* 2.592.000.000 milliseconds.
* - Default setting: 0 milliseconds (no alert).
* (no alert).
*
*
* Use {@link #clearAlertAfterInterval()} to remove missed publication notifications.
*
* @param alertAfterIntervalMs
* If more than alertInterval_ms pass without receiving a message,
* subscriptionManager will issue a publication missed.
* @return the subscriptionQos (fluent interface)
*
* @see #clearAlertAfterInterval()
*/
@Deprecated
public PeriodicSubscriptionQos setAlertAfterInterval(final long alertAfterIntervalMs) {
return setAlertAfterIntervalMs(alertAfterIntervalMs);
}
/**
* Set the alertAfterInterval in milliseconds.
* If no notification was received within the last alert interval, a missed
* publication notification will be raised.
*
* Minimum, Maximum, and Default Values:
*
* - The absolute minimum setting is the period value.
* Any value less than period will be replaced by the period setting.
* - The absolute maximum setting is 2.592.000.000 milliseconds (30 days).
* Any value bigger than this maximum will be treated at the absolute maximum setting of
* 2.592.000.000 milliseconds.
* - Default setting: 0 milliseconds (no alert).
* (no alert).
*
*
* Use {@link #clearAlertAfterInterval()} to remove missed publication notifications.
*
* @param alertAfterIntervalMs
* If more than alertInterval_ms pass without receiving a message,
* subscriptionManager will issue a publication missed.
*
* @see #clearAlertAfterInterval()
* @return this (fluent interface).
*/
public PeriodicSubscriptionQos setAlertAfterIntervalMs(final long alertAfterIntervalMs) {
if (alertAfterIntervalMs > MAX_ALERT_AFTER_INTERVAL_MS) {
this.alertAfterIntervalMs = MAX_ALERT_AFTER_INTERVAL_MS;
logger.warn("alertAfterInterval_ms > MAX_ALERT_AFTER_INTERVAL_MS. Using MAX_ALERT_AFTER_INTERVAL_MS: {}",
MAX_ALERT_AFTER_INTERVAL_MS);
} else {
this.alertAfterIntervalMs = alertAfterIntervalMs;
}
if (this.alertAfterIntervalMs != NO_ALERT_AFTER_INTERVAL && this.alertAfterIntervalMs < periodMs) {
this.alertAfterIntervalMs = periodMs;
logger.warn("alertAfterInterval_ms < MIN_ALERT_AFTER_INTERVAL and will therefore be set to the period: {}",
periodMs);
}
return this;
}
/**
* Resets the alert interval to 0 milliseconds (no alert), which disables
* alerts. No missed publication notifications will be raised.
*/
public void clearAlertAfterInterval() {
alertAfterIntervalMs = NO_ALERT_AFTER_INTERVAL;
}
/**
* @deprecated Use getPeriodMs instead
*
* Get the period in milliseconds.
* The provider will periodically send notifications every period milliseconds.
* The period can thus be seen as a sort of heart beat.
*
* @return The period value of the subscription in milliseconds.
*/
@Deprecated
public long getPeriod() {
return getPeriodMs();
}
/**
* Get the period in milliseconds.
* The provider will periodically send notifications every period milliseconds.
* The period can thus be seen as a sort of heart beat.
*
* @return The period value of the subscription in milliseconds.
*/
public long getPeriodMs() {
return periodMs;
}
/**
* @deprecated use setPeriodMs instead
*
* Set the period in milliseconds.
*
* The provider will periodically send notifications every period milliseconds.
* The period can thus be seen as a sort of heart beat.
*
* Minimum, Maximum, and Default Values:
*
* - The absolute minimum setting is 50 milliseconds.
* Any value less than this minimum will be treated at the absolute minimum
* setting of 50 milliseconds.
* - The absolute maximum setting is 2.592.000.000 milliseconds (30 days).
* Any value bigger than this maximum will be treated at the absolute maximum
* setting of 2.592.000.000 milliseconds (30 days).
* - The default setting is 50 milliseconds (MIN_PERIOD).
*
*
* @param periodMs
* The publisher will send a notification at least every period_ms.
* @return the subscriptionQos (fluent interface)
*/
@Deprecated
public PeriodicSubscriptionQos setPeriod(long periodMs) {
return setPeriodMs(periodMs);
}
/**
* Set the period in milliseconds.
*
* The provider will periodically send notifications every period milliseconds.
* The period can thus be seen as a sort of heart beat.
*
* Minimum, Maximum, and Default Values:
*
* - The absolute minimum setting is 50 milliseconds.
* Any value less than this minimum will be treated at the absolute minimum
* setting of 50 milliseconds.
* - The absolute maximum setting is 2.592.000.000 milliseconds (30 days).
* Any value bigger than this maximum will be treated at the absolute maximum
* setting of 2.592.000.000 milliseconds (30 days).
* - The default setting is 50 milliseconds (MIN_PERIOD).
*
*
* @param periodMs
* The publisher will send a notification at least every period_ms.
* @return this (fluent interface).
*/
public PeriodicSubscriptionQos setPeriodMs(long periodMs) {
if (periodMs < MIN_PERIOD_MS) {
this.periodMs = MIN_PERIOD_MS;
logger.warn("periodMs < MIN_PERIOD_MS. Using MIN_PERIOD_MS: {}", MIN_PERIOD_MS);
} else if (periodMs > MAX_PERIOD_MS) {
this.periodMs = MAX_PERIOD_MS;
logger.warn("periodMs > MAX_PERIOD_MS. Using MAX_PERIOD_MS: {}", MAX_PERIOD_MS);
} else {
this.periodMs = periodMs;
}
if (this.alertAfterIntervalMs != NO_ALERT_AFTER_INTERVAL && this.alertAfterIntervalMs < this.periodMs) {
this.alertAfterIntervalMs = this.periodMs;
logger.warn("alertAfterIntervalMs < periodMs. Setting alertAfterIntervalMs = periodMs: {}", this.periodMs);
}
return this;
}
@Override
public PeriodicSubscriptionQos setExpiryDateMs(long expiryDateMs) {
return (PeriodicSubscriptionQos) super.setExpiryDateMs(expiryDateMs);
}
@Override
public PeriodicSubscriptionQos setPublicationTtlMs(long publicationTtlMs) {
return (PeriodicSubscriptionQos) super.setPublicationTtlMs(publicationTtlMs);
}
@Override
public PeriodicSubscriptionQos setValidityMs(long validityMs) {
return (PeriodicSubscriptionQos) super.setValidityMs(validityMs);
}
@Override
@JsonIgnore
@Deprecated
/**
* @deprecated this method will be removed by 2017-01-01.
* Use getPeriodMs() instead.
*/
public long getHeartbeat() {
return periodMs;
}
/**
* Calculate code for hashing based on member contents
*
* @return The calculated hash code
*/
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (int) (alertAfterIntervalMs ^ (alertAfterIntervalMs >>> 32));
result = prime * result + (int) (periodMs ^ (periodMs >>> 32));
return result;
}
/**
* Check for equality
*
* @param obj Reference to the object to compare to
* @return true, if objects are equal, false otherwise
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PeriodicSubscriptionQos other = (PeriodicSubscriptionQos) obj;
if (alertAfterIntervalMs != other.alertAfterIntervalMs) {
return false;
}
if (periodMs != other.periodMs) {
return false;
}
return true;
}
}