Search in sources :

Example 11 with ActiveMQInterruptedException

use of org.apache.activemq.artemis.api.core.ActiveMQInterruptedException in project activemq-artemis by apache.

the class ClientConsumerImpl method resetIfSlowConsumer.

private void resetIfSlowConsumer() {
    if (clientWindowSize == 0) {
        sendCredits(0);
        // If resetting a slow consumer, we need to wait the execution
        final CountDownLatch latch = new CountDownLatch(1);
        flowControlExecutor.execute(new Runnable() {

            @Override
            public void run() {
                latch.countDown();
            }
        });
        try {
            latch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new ActiveMQInterruptedException(e);
        }
    }
}
Also used : ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException)

Example 12 with ActiveMQInterruptedException

use of org.apache.activemq.artemis.api.core.ActiveMQInterruptedException in project activemq-artemis by apache.

the class ClientConsumerImpl method receive.

private ClientMessage receive(final long timeout, final boolean forcingDelivery) throws ActiveMQException {
    if (logger.isTraceEnabled()) {
        logger.trace(this + "::receive(" + timeout + ", " + forcingDelivery + ")");
    }
    checkClosed();
    if (largeMessageReceived != null) {
        if (logger.isTraceEnabled()) {
            logger.trace(this + "::receive(" + timeout + ", " + forcingDelivery + ") -> discard LargeMessage body for " + largeMessageReceived);
        }
        // Check if there are pending packets to be received
        largeMessageReceived.discardBody();
        largeMessageReceived = null;
    }
    if (rateLimiter != null) {
        rateLimiter.limit();
    }
    if (handler != null) {
        if (logger.isTraceEnabled()) {
            logger.trace(this + "::receive(" + timeout + ", " + forcingDelivery + ") -> throwing messageHandlerSet");
        }
        throw ActiveMQClientMessageBundle.BUNDLE.messageHandlerSet();
    }
    if (clientWindowSize == 0) {
        if (logger.isTraceEnabled()) {
            logger.trace(this + "::receive(" + timeout + ", " + forcingDelivery + ") -> start slowConsumer");
        }
        startSlowConsumer();
    }
    receiverThread = Thread.currentThread();
    // To verify if deliveryForced was already call
    boolean deliveryForced = false;
    // To control when to call deliveryForce
    boolean callForceDelivery = false;
    long start = -1;
    long toWait = timeout == 0 ? Long.MAX_VALUE : timeout;
    try {
        while (true) {
            ClientMessageInternal m = null;
            synchronized (this) {
                while ((stopped || (m = buffer.poll()) == null) && !closed && toWait > 0) {
                    if (start == -1) {
                        start = System.currentTimeMillis();
                    }
                    if (m == null && forcingDelivery) {
                        if (stopped) {
                            break;
                        }
                        // we only force delivery once per call to receive
                        if (!deliveryForced) {
                            callForceDelivery = true;
                            break;
                        }
                    }
                    try {
                        wait(toWait);
                    } catch (InterruptedException e) {
                        throw new ActiveMQInterruptedException(e);
                    }
                    if (m != null || closed) {
                        break;
                    }
                    long now = System.currentTimeMillis();
                    toWait -= now - start;
                    start = now;
                }
            }
            if (failedOver) {
                if (m == null) {
                    if (logger.isTraceEnabled()) {
                        logger.trace(this + "::receive(" + timeout + ", " + forcingDelivery + ") -> m == null and failover");
                    }
                    // if failed over and the buffer is null, we reset the state and try it again
                    failedOver = false;
                    deliveryForced = false;
                    toWait = timeout == 0 ? Long.MAX_VALUE : timeout;
                    continue;
                } else {
                    if (logger.isTraceEnabled()) {
                        logger.trace(this + "::receive(" + timeout + ", " + forcingDelivery + ") -> failedOver, but m != null, being " + m);
                    }
                    failedOver = false;
                }
            }
            if (callForceDelivery) {
                if (logger.isTraceEnabled()) {
                    logger.trace(this + "::Forcing delivery");
                }
                // JBPAPP-6030 - Calling forceDelivery outside of the lock to avoid distributed dead locks
                sessionContext.forceDelivery(this, forceDeliveryCount.getAndIncrement());
                callForceDelivery = false;
                deliveryForced = true;
                continue;
            }
            if (m != null) {
                session.workDone();
                if (m.containsProperty(ClientConsumerImpl.FORCED_DELIVERY_MESSAGE)) {
                    long seq = m.getLongProperty(ClientConsumerImpl.FORCED_DELIVERY_MESSAGE);
                    // As we could be receiving a message that came from a previous call
                    if (forcingDelivery && deliveryForced && seq == forceDeliveryCount.get() - 1) {
                        // forced delivery messages are discarded, nothing has been delivered by the queue
                        resetIfSlowConsumer();
                        if (logger.isTraceEnabled()) {
                            logger.trace(this + "::There was nothing on the queue, leaving it now:: returning null");
                        }
                        return null;
                    } else {
                        if (logger.isTraceEnabled()) {
                            logger.trace(this + "::Ignored force delivery answer as it belonged to another call");
                        }
                        // Ignore the message
                        continue;
                    }
                }
                // if we have already pre acked we can't expire
                boolean expired = m.isExpired();
                flowControlBeforeConsumption(m);
                if (expired) {
                    m.discardBody();
                    session.expire(this, m);
                    if (clientWindowSize == 0) {
                        startSlowConsumer();
                    }
                    if (toWait > 0) {
                        continue;
                    } else {
                        return null;
                    }
                }
                if (m.isLargeMessage()) {
                    largeMessageReceived = m;
                }
                if (logger.isTraceEnabled()) {
                    logger.trace(this + "::Returning " + m);
                }
                return m;
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace(this + "::Returning null");
                }
                resetIfSlowConsumer();
                return null;
            }
        }
    } finally {
        receiverThread = null;
    }
}
Also used : ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException)

Example 13 with ActiveMQInterruptedException

use of org.apache.activemq.artemis.api.core.ActiveMQInterruptedException in project activemq-artemis by apache.

the class BridgeImpl method connect.

/* This is called only when the bridge is activated */
protected void connect() {
    if (stopping)
        return;
    synchronized (connectionGuard) {
        if (!keepConnecting)
            return;
        logger.debug("Connecting  " + this + " to its destination [" + nodeUUID.toString() + "], csf=" + this.csf);
        retryCount++;
        try {
            if (csf == null || csf.isClosed()) {
                if (stopping)
                    return;
                csf = createSessionFactory();
                if (csf == null) {
                    // Retrying. This probably means the node is not available (for the cluster connection case)
                    scheduleRetryConnect();
                    return;
                }
                // Session is pre-acknowledge
                session = (ClientSessionInternal) csf.createSession(user, password, false, true, true, true, 1);
                session.getProducerCreditManager().setCallback(this);
                sessionConsumer = (ClientSessionInternal) csf.createSession(user, password, false, true, true, true, 1);
            }
            if (forwardingAddress != null) {
                ClientSession.AddressQuery query = null;
                try {
                    query = session.addressQuery(forwardingAddress);
                } catch (Throwable e) {
                    ActiveMQServerLogger.LOGGER.errorQueryingBridge(e, name);
                    // This was an issue during startup, we will not count this retry
                    retryCount--;
                    scheduleRetryConnectFixedTimeout(100);
                    return;
                }
                if (!query.isExists()) {
                    ActiveMQServerLogger.LOGGER.errorQueryingBridge(forwardingAddress, retryCount);
                    scheduleRetryConnect();
                    return;
                }
            }
            producer = session.createProducer();
            session.addFailureListener(BridgeImpl.this);
            session.setSendAcknowledgementHandler(BridgeImpl.this);
            afterConnect();
            active = true;
            queue.addConsumer(BridgeImpl.this);
            queue.deliverAsync();
            ActiveMQServerLogger.LOGGER.bridgeConnected(this);
            serverLocator.addClusterTopologyListener(new TopologyListener());
            keepConnecting = false;
            return;
        } catch (ActiveMQException e) {
            // the session was created while its server was starting, retry it:
            if (e.getType() == ActiveMQExceptionType.SESSION_CREATION_REJECTED) {
                ActiveMQServerLogger.LOGGER.errorStartingBridge(name);
                // We are not going to count this one as a retry
                retryCount--;
                scheduleRetryConnectFixedTimeout(this.retryInterval);
                return;
            } else {
                ActiveMQServerLogger.LOGGER.errorConnectingBridgeRetry(this);
                scheduleRetryConnect();
            }
        } catch (ActiveMQInterruptedException | InterruptedException e) {
            ActiveMQServerLogger.LOGGER.errorConnectingBridge(e, this);
        } catch (Exception e) {
            ActiveMQServerLogger.LOGGER.errorConnectingBridge(e, this);
            if (csf != null) {
                try {
                    csf.close();
                    csf = null;
                } catch (Throwable ignored) {
                }
            }
            fail(false);
            scheduleRetryConnect();
        }
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ClusterTopologyListener(org.apache.activemq.artemis.api.core.client.ClusterTopologyListener) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException)

Example 14 with ActiveMQInterruptedException

use of org.apache.activemq.artemis.api.core.ActiveMQInterruptedException in project activemq-artemis by apache.

the class InVMConnection method write.

@Override
public void write(final ActiveMQBuffer buffer, final boolean flush, final boolean batch, final ChannelFutureListener futureListener) {
    try {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    if (!closed) {
                        // read and discard
                        buffer.readInt();
                        if (logger.isTraceEnabled()) {
                            logger.trace(InVMConnection.this + "::Sending inVM packet");
                        }
                        handler.bufferReceived(id, buffer);
                        if (futureListener != null) {
                            futureListener.operationComplete(null);
                        }
                    }
                } catch (Exception e) {
                    final String msg = "Failed to write to handler on connector " + this;
                    ActiveMQServerLogger.LOGGER.errorWritingToInvmConnector(e, this);
                    throw new IllegalStateException(msg, e);
                } finally {
                    buffer.release();
                    if (logger.isTraceEnabled()) {
                        logger.trace(InVMConnection.this + "::packet sent done");
                    }
                }
            }
        });
        if (flush && flushEnabled) {
            final CountDownLatch latch = new CountDownLatch(1);
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    latch.countDown();
                }
            });
            try {
                if (!latch.await(10, TimeUnit.SECONDS)) {
                    ActiveMQServerLogger.LOGGER.timedOutFlushingInvmChannel();
                }
            } catch (InterruptedException e) {
                throw new ActiveMQInterruptedException(e);
            }
        }
    } catch (RejectedExecutionException e) {
    // Ignore - this can happen if server/client is shutdown and another request comes in
    }
}
Also used : ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 15 with ActiveMQInterruptedException

use of org.apache.activemq.artemis.api.core.ActiveMQInterruptedException in project activemq-artemis by apache.

the class AIOSequentialFileFactory method stop.

@Override
public void stop() {
    if (this.running.compareAndSet(true, false)) {
        buffersControl.stop();
        libaioContext.close();
        libaioContext = null;
        if (pollerThread != null) {
            try {
                pollerThread.join(AbstractSequentialFileFactory.EXECUTOR_TIMEOUT * 1000);
                if (pollerThread.isAlive()) {
                    ActiveMQJournalLogger.LOGGER.timeoutOnPollerShutdown(new Exception("trace"));
                }
            } catch (InterruptedException e) {
                throw new ActiveMQInterruptedException(e);
            }
        }
        super.stop();
    }
}
Also used : ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException) IOException(java.io.IOException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQInterruptedException(org.apache.activemq.artemis.api.core.ActiveMQInterruptedException)

Aggregations

ActiveMQInterruptedException (org.apache.activemq.artemis.api.core.ActiveMQInterruptedException)17 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)7 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)2 IOException (java.io.IOException)1 ObjectStreamException (java.io.ObjectStreamException)1 Lock (java.util.concurrent.locks.Lock)1 BytesMessage (javax.jms.BytesMessage)1 IllegalStateException (javax.jms.IllegalStateException)1 InvalidDestinationException (javax.jms.InvalidDestinationException)1 JMSException (javax.jms.JMSException)1 MapMessage (javax.jms.MapMessage)1 ObjectMessage (javax.jms.ObjectMessage)1 StreamMessage (javax.jms.StreamMessage)1 TextMessage (javax.jms.TextMessage)1 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)1