summaryrefslogtreecommitdiff
path: root/java/client/src/main/java/org/apache/qpid/jms/failover/FailoverSingleServer.java
blob: d033a49f5cad04e3ee66fbb87c329ea6a9fd3b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */
package org.apache.qpid.jms.failover;

import org.apache.qpid.jms.BrokerDetails;
import org.apache.qpid.jms.ConnectionURL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FailoverSingleServer implements FailoverMethod
{
    private static final Logger _logger = LoggerFactory.getLogger(FailoverSingleServer.class);

    /** The default number of times to rety a conection to this server */
    public static final int DEFAULT_SERVER_RETRIES = 0;

    /** The details of the Single Server */
    private BrokerDetails _brokerDetail;

    /** The number of times to retry connecting to the sever */
    protected int _retries;

    /** The current number of attempts made to the server */
    protected int _currentRetries = 0;


    public FailoverSingleServer(ConnectionURL connectionDetails)
    {
        if (connectionDetails.getBrokerCount() > 0)
        {
            setBroker(connectionDetails.getBrokerDetails(0));
        }
        else
        {
            throw new IllegalArgumentException("BrokerDetails details required for connection.");
        }
    }

    public FailoverSingleServer(BrokerDetails brokerDetail)
    {
        setBroker(brokerDetail);
    }

    public void reset()
    {
        _currentRetries = 0;
    }

    public boolean failoverAllowed()
    {
        return _currentRetries < _retries;
    }

    public void attainedConnection()
    {
        reset();
    }

    public BrokerDetails getCurrentBrokerDetails()
    {
        return _brokerDetail;
    }

    public BrokerDetails getNextBrokerDetails()
    {
        if (_currentRetries == _retries)
        {
            return null;
        }
        else
        {
            if (_currentRetries < _retries)
            {
                _currentRetries++;
            }
        }


        String delayStr = _brokerDetail.getProperty(BrokerDetails.OPTIONS_CONNECT_DELAY);
        if (delayStr != null && _currentRetries > 0)
        {
            Long delay = Long.parseLong(delayStr);
            _logger.info("Delay between connect retries:" + delay);
            try
            {

                Thread.sleep(delay);
            }
            catch (InterruptedException ie)
            {
                return null;
            }
        }
        else
        {
            _logger.info("No delay between connect retries, use tcp://host:port?connectdelay='value' to enable.");
        }

        return _brokerDetail;
    }

    public void setBroker(BrokerDetails broker)
    {
        if (broker == null)
        {
            throw new IllegalArgumentException("BrokerDetails details cannot be null");
        }
        _brokerDetail = broker;

        String retries = broker.getProperty(BrokerDetails.OPTIONS_RETRY);
        if (retries != null)
        {
            try
            {
                _retries = Integer.parseInt(retries);
            }
            catch (NumberFormatException nfe)
            {
                _retries = DEFAULT_SERVER_RETRIES;
            }
        }
        else
        {
            _retries = DEFAULT_SERVER_RETRIES;
        }

        reset();
    }

    public void setRetries(int retries)
    {
        _retries = retries;
    }

    public String methodName()
    {
        return "Single Server";
    }

    public String toString()
    {
        return methodName()+":\n" +
               "Max Retries:" + _retries +
               "\nCurrent Retry:" + _currentRetries +
               "\n" + _brokerDetail + "\n";
    }

}