001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jms.support;
018
019import javax.jms.Message;
020
021import org.springframework.lang.Nullable;
022
023/**
024 * Gather the Quality-of-Service settings that can be used when sending a message.
025 *
026 * @author Stephane Nicoll
027 * @since 5.0
028 */
029public class QosSettings {
030
031        private int deliveryMode;
032
033        private int priority;
034
035        private long timeToLive;
036
037
038        /**
039         * Create a new instance with the default settings.
040         * @see Message#DEFAULT_DELIVERY_MODE
041         * @see Message#DEFAULT_PRIORITY
042         * @see Message#DEFAULT_TIME_TO_LIVE
043         */
044        public QosSettings() {
045                this(Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
046        }
047
048        /**
049         * Create a new instance with the specified settings.
050         */
051        public QosSettings(int deliveryMode, int priority, long timeToLive) {
052                this.deliveryMode = deliveryMode;
053                this.priority = priority;
054                this.timeToLive = timeToLive;
055        }
056
057
058        /**
059         * Set the delivery mode to use when sending a message.
060         * Default is the JMS Message default: "PERSISTENT".
061         * @param deliveryMode the delivery mode to use
062         * @see javax.jms.DeliveryMode#PERSISTENT
063         * @see javax.jms.DeliveryMode#NON_PERSISTENT
064         * @see javax.jms.Message#DEFAULT_DELIVERY_MODE
065         * @see javax.jms.MessageProducer#send(javax.jms.Message, int, int, long)
066         */
067        public void setDeliveryMode(int deliveryMode) {
068                this.deliveryMode = deliveryMode;
069        }
070
071        /**
072         * Return the delivery mode to use when sending a message.
073         */
074        public int getDeliveryMode() {
075                return this.deliveryMode;
076        }
077
078        /**
079         * Set the priority of a message when sending.
080         * @see javax.jms.Message#DEFAULT_PRIORITY
081         * @see javax.jms.MessageProducer#send(javax.jms.Message, int, int, long)
082         */
083        public void setPriority(int priority) {
084                this.priority = priority;
085        }
086
087        /**
088         * Return the priority of a message when sending.
089         */
090        public int getPriority() {
091                return this.priority;
092        }
093
094        /**
095         * Set the time-to-live of the message when sending.
096         * @param timeToLive the message's lifetime (in milliseconds)
097         * @see javax.jms.Message#DEFAULT_TIME_TO_LIVE
098         * @see javax.jms.MessageProducer#send(javax.jms.Message, int, int, long)
099         */
100        public void setTimeToLive(long timeToLive) {
101                this.timeToLive = timeToLive;
102        }
103
104        /**
105         * Return the time-to-live of the message when sending.
106         */
107        public long getTimeToLive() {
108                return this.timeToLive;
109        }
110
111
112        @Override
113        public boolean equals(@Nullable Object other) {
114                if (this == other) {
115                        return true;
116                }
117                if (!(other instanceof QosSettings)) {
118                        return false;
119                }
120
121                QosSettings otherSettings = (QosSettings) other;
122                return (this.deliveryMode == otherSettings.deliveryMode &&
123                                this.priority == otherSettings.priority &&
124                                this.timeToLive == otherSettings.timeToLive);
125        }
126
127        @Override
128        public int hashCode() {
129                return (this.deliveryMode * 31 + this.priority);
130        }
131
132        @Override
133        public String toString() {
134                return "QosSettings{" + "deliveryMode=" + this.deliveryMode +
135                                ", priority=" + this.priority + ", timeToLive=" + this.timeToLive + '}';
136        }
137}