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

com.swiftmq.net.client.Reconnector Maven / Gradle / Ivy

Go to download

Client for SwiftMQ Messaging System with JMS, AMQP 1.0 and file transfer over JMS.

The newest version!
/*
 * Copyright 2019 IIT Software GmbH
 *
 * IIT Software GmbH 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 com.swiftmq.net.client;

import com.swiftmq.tools.concurrent.Semaphore;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public abstract class Reconnector {
    List servers = null;
    Map parameters = null;
    boolean enabled = false;
    int maxRetries = 0;
    long retryDelay = 0;
    boolean debug = false;
    Connection active = null;
    int currentPos = 0;
    boolean closed = false;
    boolean firstConnectAttempt = true;
    String debugString = null;
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    Semaphore waitSem = new Semaphore();

    protected Reconnector(List servers, Map parameters, boolean enabled, int maxRetries, long retryDelay, boolean debug) {
        this.servers = servers;
        this.parameters = parameters;
        this.enabled = enabled;
        this.maxRetries = maxRetries;
        this.retryDelay = retryDelay;
        this.debug = debug;
        if (debug)
            System.out.println(dbg() + " created, enabled=" + enabled + ", maxRetries=" + maxRetries + ", retryDelay=" + retryDelay + ", servers=" + servers + ", parameters=" + parameters);
    }

    private String dbg() {
        return new Date() + " " + (debugString == null ? toString() : debugString);
    }

    public void setDebugString(String debugString) {
        this.debugString = "[Reconnector, " + debugString + "]";
    }

    public List getServers() {
        return servers;
    }

    public boolean isDebug() {
        return debug;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public boolean isIntraVM() {
        return false;
    }

    protected abstract Connection createConnection(ServerEntry entry, Map parameters);

    public Connection getConnection() {
        lock.writeLock().lock();
        try {
            if (debug) System.out.println(dbg() + ", getConnection ...");
            int nRetries = -1;
            while (!closed && active == null && nRetries < maxRetries) {
                if (retryDelay > 0 && !firstConnectAttempt) {
                    if (debug)
                        System.out.println(dbg() + ", nRetries=" + nRetries + ", waiting " + retryDelay + " ms ...");
                    waitSem.waitHere(retryDelay);
                    waitSem.reset();
                }
                if (currentPos == servers.size())
                    currentPos = 0;
                ServerEntry entry = (ServerEntry) servers.get(currentPos++);
                if (debug)
                    System.out.println(dbg() + ", nRetries=" + nRetries + ", attempt to create connection to: " + entry);
                active = createConnection(entry, parameters);
                if (debug)
                    System.out.println(dbg() + ", nRetries=" + nRetries + ", createConnection returns " + active);
                if (active == null) {
                    if (!enabled)
                        break;
                    nRetries++;
                }
                firstConnectAttempt = false;
            }
            if (debug) System.out.println(dbg() + ", getConnection returns " + active);
            return active;
        } finally {
            lock.writeLock().unlock();
        }

    }

    public void invalidateConnection() {
        lock.writeLock().lock();
        try {
            if (debug) System.out.println(dbg() + ", invalidateConnection, active=" + active);
            if (active != null) {
                active.close();
                active = null;
            }
        } finally {
            lock.writeLock().unlock();
        }

    }

    public void close() {
        lock.writeLock().lock();
        try {
            if (debug) System.out.println(dbg() + ", close, active=" + active);
            closed = true;
            if (active != null) {
                active.close();
                active = null;
            }
        } finally {
            lock.writeLock().unlock();
        }
        waitSem.notifySingleWaiter();
    }

    public String toString() {
        return "Reconnector";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy