Search in sources :

Example 6 with DataStreamOutputStream

use of com.swiftmq.tools.util.DataStreamOutputStream in project swiftmq-client by iitsoftware.

the class ConnectionImpl method handOver.

public void handOver(com.swiftmq.net.client.Connection connection) {
    this.connection = connection;
    if (connection != null) {
        connectionId++;
        myHostname = connection.getLocalHostname();
        connection.setInboundHandler(this);
        connection.setExceptionHandler(this);
        inputActiveIndicator = new AtomicBoolean(false);
        connection.setInputActiveIndicator(inputActiveIndicator);
        outStream = new DataStreamOutputStream(connection.getOutputStream());
        if (connectionQueue != null) {
            connectionQueue.clear();
            connectionQueue.startQueue();
        }
        // Blocks new XA Requests
        setSessionBlockState(true);
        if (requestRegistry != null) {
            requestRegistry.setPaused(false);
            Semaphore retrySem = new Semaphore();
            requestRegistry.retryAllRequests(retrySem);
            retrySem.waitHere();
        }
        resetSessions(false);
        notifyReconnectListener();
        setSessionBlockState(false);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataStreamOutputStream(com.swiftmq.tools.util.DataStreamOutputStream) Semaphore(com.swiftmq.tools.concurrent.Semaphore)

Example 7 with DataStreamOutputStream

use of com.swiftmq.tools.util.DataStreamOutputStream in project swiftmq-client by iitsoftware.

the class Connector method visit.

public void visit(POReconnect po) {
    if (debug)
        System.out.println(toString() + ", visit, po=" + po + " ... [recreateStarted=" + recreateStarted + ", reconnectInProgress=" + reconnectInProgress + "]");
    if (closed) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", closed, return");
        if (po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
        return;
    }
    if (recreateStarted) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", recreate in progress");
        return;
    }
    if (reconnectInProgress) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", reconnect already in progress");
        if (!po.isInternalRetry() && po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
    } else {
        if (po.isInternalRetry()) {
            if (debug)
                System.out.println(toString() + ", visit, po=" + po + ", internal retry, count=" + internalRetryCount);
            internalRetryCount--;
            if (internalRetryCount <= 0) {
                if (debug)
                    System.out.println(toString() + ", visit, po=" + po + ", internal retry cancel!");
                connection = null;
                dispatch(new POHandover());
                return;
            }
        }
        reconnectInProgress = true;
        recreateStarted = false;
        sem = po.getSemaphore();
        recreatableConnection = po.getRecreatableConnection();
        reconnector.setDebugString(recreatableConnection.toString());
        if (!po.isInternalRetry())
            recreatableConnection.prepareForReconnect();
        connection = reconnector.getConnection();
        if (connection != null) {
            if (debug)
                System.out.println(toString() + ", visit, po=" + po + ", connection=" + connection);
            connection.setInboundHandler(this);
            connection.setExceptionHandler(this);
            outStream = new DataStreamOutputStream(connection.getOutputStream());
            try {
                connection.start();
                dispatch(new POVersionRequest());
            } catch (Exception e) {
                if (debug)
                    System.out.println(toString() + ", visit, po=" + po + ", exception connection.start()=" + e);
                reconnector.invalidateConnection();
                connection = null;
                dispatch(new POHandover());
            }
        } else
            dispatch(new POHandover());
    }
    if (debug)
        System.out.println(toString() + ", visit, po=" + po + " done");
}
Also used : DataStreamOutputStream(com.swiftmq.tools.util.DataStreamOutputStream) InvalidVersionException(com.swiftmq.jms.InvalidVersionException) IOException(java.io.IOException) JMSSecurityException(javax.jms.JMSSecurityException)

Example 8 with DataStreamOutputStream

use of com.swiftmq.tools.util.DataStreamOutputStream in project swiftmq-client by iitsoftware.

the class Connector method visit.

public void visit(POReconnect po) {
    if (debug)
        System.out.println(toString() + ", visit, po=" + po + " ...");
    if (closed) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", closed, return");
        if (po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
        return;
    }
    if (recreateStarted) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", recreate in progress");
        return;
    }
    if (reconnectInProgress) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", reconnect already in progress");
        if (!po.isInternalRetry() && po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
    } else {
        if (po.isInternalRetry()) {
            if (debug)
                System.out.println(toString() + ", visit, po=" + po + ", internal retry, count=" + internalRetryCount);
            internalRetryCount--;
            if (internalRetryCount <= 0) {
                if (debug)
                    System.out.println(toString() + ", visit, po=" + po + ", internal retry cancel!");
                connection = null;
                dispatch(new POHandover());
                return;
            }
        }
        reconnectInProgress = true;
        recreateStarted = false;
        sem = po.getSemaphore();
        recreatableConnection = po.getRecreatableConnection();
        reconnector.setDebugString(recreatableConnection.toString());
        if (!po.isInternalRetry())
            recreatableConnection.prepareForReconnect();
        connection = reconnector.getConnection();
        if (connection != null) {
            if (debug)
                System.out.println(toString() + ", visit, po=" + po + ", connection=" + connection);
            connection.setInboundHandler(this);
            connection.setExceptionHandler(this);
            outStream = new DataStreamOutputStream(connection.getOutputStream());
            try {
                connection.start();
                dispatch(new POVersionRequest());
            } catch (Exception e) {
                if (debug)
                    System.out.println(toString() + ", visit, po=" + po + ", exception connection.start()=" + e);
                reconnector.invalidateConnection();
                connection = null;
                dispatch(new POHandover());
            }
        } else
            dispatch(new POHandover());
    }
    if (debug)
        System.out.println(toString() + ", visit, po=" + po + " done");
}
Also used : DataStreamOutputStream(com.swiftmq.tools.util.DataStreamOutputStream) InvalidVersionException(com.swiftmq.jms.InvalidVersionException) IOException(java.io.IOException) JMSSecurityException(javax.jms.JMSSecurityException)

Example 9 with DataStreamOutputStream

use of com.swiftmq.tools.util.DataStreamOutputStream in project swiftmq-client by iitsoftware.

the class Connection method connect.

/**
 * Performs the actual connect to the remote host, negotiates the protocol and authenticates the user
 *
 * @throws IOException                         if an IOExcption occurs
 * @throws UnsupportedProtocolVersionException if the AMQP/SASL protocol version is not supported by the remote host
 * @throws AuthenticationException             if the user cannot be authenticated
 * @throws ConnectionClosedException           if the connection was closed
 */
public void connect() throws IOException, UnsupportedProtocolVersionException, AuthenticationException, ConnectionClosedException {
    verifyState();
    connectionDispatcher = new ConnectionDispatcher(ctx, hostname);
    if (socketFactory instanceof SocketFactory2)
        ((SocketFactory2) socketFactory).setReceiveBufferSize(inputBufferSize);
    networkConnection = new BlockingConnection(socketFactory.createSocket(hostname, port), connectionDispatcher, this) {

        protected ProtocolOutputHandler createOutputHandler(int outputBufferSize, int outputExtendSize) {
            return new RawOutputHandler(outputBufferSize, outputExtendSize) {

                public void flush() throws IOException {
                    super.flush();
                    invokeOutputListener();
                }
            };
        }

        protected ProtocolInputHandler createInputHandler() {
            return connectionDispatcher.getProtocolHandler();
        }
    };
    connectionDispatcher.setMyConnection(this);
    networkConnection.start();
    dos = new DataStreamOutputStream(networkConnection.getOutputStream());
    connectionPool = ctx.getConnectionPool();
    connectionTask = new ConnectionTask();
    connectionQueue = new ConnectionQueue();
    connectionDispatcher.setOutboundHandler(connectionQueue);
    connectionQueue.startQueue();
    // SASL
    if (doAuth) {
        connectionDispatcher.setSaslActive(true);
        Semaphore sem = new Semaphore();
        POObject po = new POProtocolRequest(sem, Util.SASL_INIT);
        connectionDispatcher.dispatch(po);
        sem.waitHere();
        sem.reset();
        if (!po.isSuccess()) {
            cancel();
            throw new UnsupportedProtocolVersionException(po.getException());
        }
        po = new POAuthenticate(sem, mechanism, userName, password);
        connectionDispatcher.dispatch(po);
        sem.waitHere();
        sem.reset();
        if (!po.isSuccess()) {
            cancel();
            throw new AuthenticationException(po.getException());
        }
    }
    // AMQP
    Semaphore sem = new Semaphore();
    POObject po = new POProtocolRequest(sem, Util.AMQP_INIT);
    connectionDispatcher.dispatch(po);
    sem.waitHere();
    sem.reset();
    if (!po.isSuccess()) {
        cancel();
        throw new UnsupportedProtocolVersionException(po.getException());
    }
    // Open
    po = new POOpen(sem, containerId, maxFrameSize, 255, idleTimeout);
    connectionDispatcher.dispatch(po);
    sem.waitHere();
    sem.reset();
    if (!po.isSuccess()) {
        cancel();
        throw new IOException(po.getException());
    }
    connected = true;
}
Also used : ProtocolInputHandler(com.swiftmq.net.protocol.ProtocolInputHandler) ProtocolOutputHandler(com.swiftmq.net.protocol.ProtocolOutputHandler) POProtocolRequest(com.swiftmq.amqp.v100.client.po.POProtocolRequest) SocketFactory2(com.swiftmq.net.SocketFactory2) DataStreamOutputStream(com.swiftmq.tools.util.DataStreamOutputStream) RawOutputHandler(com.swiftmq.net.protocol.raw.RawOutputHandler) IOException(java.io.IOException) Semaphore(com.swiftmq.tools.concurrent.Semaphore) POAuthenticate(com.swiftmq.amqp.v100.client.po.POAuthenticate) POOpen(com.swiftmq.amqp.v100.client.po.POOpen) BlockingConnection(com.swiftmq.net.client.BlockingConnection) POObject(com.swiftmq.tools.pipeline.POObject)

Example 10 with DataStreamOutputStream

use of com.swiftmq.tools.util.DataStreamOutputStream in project swiftmq-client by iitsoftware.

the class Connector method visit.

public void visit(POReconnect po) {
    if (debug)
        System.out.println(toString() + ", visit, po=" + po + " ...");
    if (closed) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", closed, return");
        if (po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
        return;
    }
    if (reconnectInProgress && !po.isInternalRetry()) {
        if (debug)
            System.out.println(toString() + ", visit, po=" + po + ", reconnect already in progress");
        if (po.getSemaphore() != null)
            po.getSemaphore().notifySingleWaiter();
    } else {
        reconnectInProgress = true;
        recreateStarted = false;
        sem = po.getSemaphore();
        recreatableConnection = po.getRecreatableConnection();
        if (!po.isInternalRetry())
            recreatableConnection.prepareForReconnect();
        connection = reconnector.getConnection();
        if (connection != null) {
            if (debug)
                System.out.println(toString() + ", visit, po=" + po + ", connection=" + connection);
            connection.setInboundHandler(this);
            connection.setExceptionHandler(this);
            outStream = new DataStreamOutputStream(connection.getOutputStream());
            try {
                connection.start();
                dispatch(new POVersionRequest());
            } catch (Exception e) {
                if (debug)
                    System.out.println(toString() + ", visit, po=" + po + ", exception connection.start()=" + e);
                reconnector.invalidateConnection();
                connection = null;
                dispatch(new POHandover());
            }
        } else
            dispatch(new POHandover());
    }
    if (debug)
        System.out.println(toString() + ", visit, po=" + po + " done");
}
Also used : DataStreamOutputStream(com.swiftmq.tools.util.DataStreamOutputStream) IOException(java.io.IOException) JMSSecurityException(javax.jms.JMSSecurityException)

Aggregations

DataStreamOutputStream (com.swiftmq.tools.util.DataStreamOutputStream)12 Semaphore (com.swiftmq.tools.concurrent.Semaphore)5 IOException (java.io.IOException)5 JMSSecurityException (javax.jms.JMSSecurityException)3 InvalidVersionException (com.swiftmq.jms.InvalidVersionException)2 TimerListener (com.swiftmq.swiftlet.timer.event.TimerListener)2 POAuthenticate (com.swiftmq.amqp.v100.client.po.POAuthenticate)1 POOpen (com.swiftmq.amqp.v100.client.po.POOpen)1 POProtocolRequest (com.swiftmq.amqp.v100.client.po.POProtocolRequest)1 SocketFactory2 (com.swiftmq.net.SocketFactory2)1 BlockingConnection (com.swiftmq.net.client.BlockingConnection)1 ProtocolInputHandler (com.swiftmq.net.protocol.ProtocolInputHandler)1 ProtocolOutputHandler (com.swiftmq.net.protocol.ProtocolOutputHandler)1 RawOutputHandler (com.swiftmq.net.protocol.raw.RawOutputHandler)1 POObject (com.swiftmq.tools.pipeline.POObject)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1