Example usage for com.rabbitmq.client ConfirmListener ConfirmListener

List of usage examples for com.rabbitmq.client ConfirmListener ConfirmListener

Introduction

In this page you can find the example usage for com.rabbitmq.client ConfirmListener ConfirmListener.

Prototype

ConfirmListener

Source Link

Usage

From source file:mobisocial.musubi.service.AMQPService.java

License:Apache License

void initiateConnection() {
    if (mConnection != null) {
        //just for information sake, this is a legitimate event
        Log.i(TAG, "Already connected when triggered to initiate connection");
        return;/*from w w w .j  av a 2  s  .  c o  m*/
    }
    Log.i(TAG, "Network is up connection is being attempted");
    try {
        mConnection = mConnectionFactory.newConnection();
        mConnection.addShutdownListener(new ShutdownListener() {
            @Override
            public void shutdownCompleted(ShutdownSignalException cause) {
                if (!cause.isInitiatedByApplication()) {
                    //start the connection
                    mAMQPHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            closeConnection(FailedOperationType.FailedConnect);
                        }
                    });
                }
            }
        });

        mDeclaredGroups = new HashSet<String>();
        mMessageWaitingForAck = new TLongHashSet();
        mMessageWaitingForAckByTag = new TLongLongHashMap();

        mIncomingChannel = mConnection.createChannel();
        mIncomingChannel.basicQos(10);
        attachToQueues();

        mOutgoingChannel = mConnection.createChannel();
        //TODO: these callbacks run in another thread, so this is not correct
        //we need some synchronized or a customized ExecutorService that
        //posts to the handler (though, that may not be possible if they demand
        //n threads to run on
        mOutgoingChannel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                //don't immediately try to resend, just flag it, it will be rescanned later
                //this probably only happens if the server is temporarily out of space
                long encoded_id = mMessageWaitingForAckByTag.get(deliveryTag);
                mMessageWaitingForAckByTag.remove(deliveryTag);
                mMessageWaitingForAck.remove(encoded_id);
            }

            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                //delivered!
                long encoded_id = mMessageWaitingForAckByTag.get(deliveryTag);

                //mark the db entry as processed
                MEncodedMessage encoded = mEncodedMessageManager.lookupMetadataById(encoded_id);
                assert (encoded.outbound_);
                encoded.processed_ = true;
                encoded.processedTime_ = new Date().getTime();
                mEncodedMessageManager.updateEncodedMetadata(encoded);

                mMessageWaitingForAckByTag.remove(deliveryTag);
                mMessageWaitingForAck.remove(encoded_id);

                long feedId = mEncodedMessageManager.getFeedIdForEncoded(encoded_id);
                if (feedId != -1) {
                    Uri feedUri = MusubiContentProvider.uriForItem(Provided.FEEDS_ID, feedId);
                    getContentResolver().notifyChange(feedUri, null);
                }
            }
        });
        mOutgoingChannel.confirmSelect();
        mConnectionReady = true;

        //once we have successfully done our work, we can
        //reset the failure delay, FYI, internal exceptions in the
        //message sender will cause backoff to MAX_DELAY
        if (mFailedOperation == FailedOperationType.FailedConnect) {
            mFailureDelay = MIN_DELAY;
            mFailedOperation = FailedOperationType.FailedNone;
        }
    } catch (Throwable e) {
        closeConnection(FailedOperationType.FailedConnect);
        Log.e(TAG, "Failed to connect to AMQP", e);
    }
    //slight downside here is that if publish a message causes the fault,
    //then we will always reconnect and disconnect
    sendMessages();
}

From source file:mx.bigdata.utils.amqp.HaPublisher.java

License:Apache License

private void initConfirms() throws Exception {
    getChannel().addConfirmListener(new ConfirmListener() {
        public void handleAck(long seqNo, boolean multiple) {
            if (multiple) {
                Map<Long, PublishWrapper> headMap = unconfirmedMap.headMap(seqNo + 1);
                logger.trace(//from  ww  w.j  a  v a 2s . co m
                        tag + " confirmed " + headMap.size() + " messages up to " + seqNo + " sequence number");
                headMap.clear();
            } else {
                logger.trace(tag + " confirmed 1 messages with " + seqNo + " sequence number");
                unconfirmedMap.remove(seqNo);
            }
        }

        public void handleNack(long seqNo, boolean multiple) {
            if (multiple) {
                Iterator<Map.Entry<Long, PublishWrapper>> entries = unconfirmedMap.headMap(seqNo + 1).entrySet()
                        .iterator();
                while (entries.hasNext()) {
                    Map.Entry<Long, PublishWrapper> entry = entries.next();
                    try {
                        entry.getValue().publish();
                    } catch (Exception ex) {
                        logger.error(tag + " unable to republish unconfirmed message: " + entry.getKey());
                    }
                    entries.remove();
                }
            } else {
                try {
                    unconfirmedMap.remove(seqNo).publish();
                } catch (Exception ex) {
                    logger.error(tag + " unable to republish unconfirmed message: " + seqNo);
                }
            }
        }
    });
    getChannel().confirmSelect();
}

From source file:org.mule.transport.amqp.DefaultAmqpConfirmsManager.java

License:Open Source License

public void requestConfirm(Channel channel, MuleEvent event) throws Exception {
    if (!handlesConfirms()) {
        return;/*from  www .j  av a  2  s .co m*/
    }

    channel.addConfirmListener(new ConfirmListener() {
        public void handleAck(long deliveryTag, boolean multiple) throws IOException {
            confirm(deliveryTag, true);
        }

        public void handleNack(long deliveryTag, boolean multiple) throws IOException {
            confirm(deliveryTag, false);
        }
    });

    channel.confirmSelect();
    long nextSequence = channel.getNextPublishSeqNo();
    pendingConfirms.put(nextSequence, new ConfirmHandler());
    event.setFlowVariable(AmqpConstants.NEXT_PUBLISH_SEQ_NO, nextSequence);

}

From source file:org.mule.transport.amqp.internal.confirm.DefaultConfirmsManager.java

License:Open Source License

public void requestConfirm(Channel channel, MuleEvent event) throws Exception {
    if (!handlesConfirms()) {
        return;/*ww w. j  a  v  a 2s. c  o  m*/
    }

    channel.addConfirmListener(new ConfirmListener() {
        public void handleAck(long deliveryTag, boolean multiple) throws IOException {
            confirm(deliveryTag, true);
        }

        public void handleNack(long deliveryTag, boolean multiple) throws IOException {
            confirm(deliveryTag, false);
        }
    });

    channel.confirmSelect();
    long nextSequence = channel.getNextPublishSeqNo();
    pendingConfirms.put(nextSequence, new ConfirmHandler());
    event.setFlowVariable(AmqpConnector.MESSAGE_PROPERTY_NEXT_PUBLISH_SEQ_NO, nextSequence);

}