001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.jms.failover;
022
023 import org.apache.qpid.jms.BrokerDetails;
024 import org.apache.qpid.jms.ConnectionURL;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027
028 public class FailoverSingleServer implements FailoverMethod
029 {
030 private static final Logger _logger = LoggerFactory.getLogger(FailoverSingleServer.class);
031
032 /** The default number of times to rety a conection to this server */
033 public static final int DEFAULT_SERVER_RETRIES = 1;
034
035 /** The details of the Single Server */
036 private BrokerDetails _brokerDetail;
037
038 /** The number of times to retry connecting to the sever */
039 private int _retries;
040
041 /** The current number of attempts made to the server */
042 private int _currentRetries;
043
044
045 public FailoverSingleServer(ConnectionURL connectionDetails)
046 {
047 if (connectionDetails.getBrokerCount() > 0)
048 {
049 setBroker(connectionDetails.getBrokerDetails(0));
050 }
051 else
052 {
053 throw new IllegalArgumentException("BrokerDetails details required for connection.");
054 }
055 }
056
057 public FailoverSingleServer(BrokerDetails brokerDetail)
058 {
059 setBroker(brokerDetail);
060 }
061
062 public void reset()
063 {
064 _currentRetries = -1;
065 }
066
067 public boolean failoverAllowed()
068 {
069 return _currentRetries < _retries;
070 }
071
072 public void attainedConnection()
073 {
074 reset();
075 }
076
077 public BrokerDetails getCurrentBrokerDetails()
078 {
079 return _brokerDetail;
080 }
081
082 public BrokerDetails getNextBrokerDetails()
083 {
084 if (_currentRetries == _retries)
085 {
086 return null;
087 }
088 else
089 {
090 if (_currentRetries < _retries)
091 {
092 _currentRetries++;
093 }
094 }
095
096
097 String delayStr = _brokerDetail.getProperty(BrokerDetails.OPTIONS_CONNECT_DELAY);
098 if (delayStr != null && _currentRetries > 0)
099 {
100 Long delay = Long.parseLong(delayStr);
101 _logger.info("Delay between connect retries:" + delay);
102 try
103 {
104
105 Thread.sleep(delay);
106 }
107 catch (InterruptedException ie)
108 {
109 return null;
110 }
111 }
112 else
113 {
114 _logger.info("No delay between connect retries, use tcp://host:port?connectdelay='value' to enable.");
115 }
116
117 return _brokerDetail;
118 }
119
120 public void setBroker(BrokerDetails broker)
121 {
122 if (broker == null)
123 {
124 throw new IllegalArgumentException("BrokerDetails details cannot be null");
125 }
126 _brokerDetail = broker;
127
128 String retries = broker.getProperty(BrokerDetails.OPTIONS_RETRY);
129 if (retries != null)
130 {
131 try
132 {
133 _retries = Integer.parseInt(retries);
134 }
135 catch (NumberFormatException nfe)
136 {
137 _retries = DEFAULT_SERVER_RETRIES;
138 }
139 }
140 else
141 {
142 _retries = DEFAULT_SERVER_RETRIES;
143 }
144
145 reset();
146 }
147
148 public void setRetries(int retries)
149 {
150 _retries = retries;
151 }
152
153 public String methodName()
154 {
155 return "Single Server";
156 }
157
158 public String toString()
159 {
160 return "SingleServer:\n" +
161 "Max Retries:" + _retries +
162 "\nCurrent Retry:" + _currentRetries +
163 "\n" + _brokerDetail + "\n";
164 }
165
166 }
|