On 12/15/2009 04:08 PM, Acácio Centeno wrote:
Hello Mr. Sim,

        Could you give me an example on using flow control? I tried this,
expecting to limit the amount of messages to be consumed to five, but almost
all the messages on the queue were consumed (I'm also fuzzy about why not
all of them were consumed):

#include<iostream>

#include<qpid/client/Connection.h>
#include<qpid/client/Session.h>
#include<qpid/client/Message.h>
#include<qpid/client/SubscriptionManager.h>

using namespace std;
using namespace qpid::client;
using namespace qpid::framing;

int main(int argc, char *argv[]) {
    Connection           connection;
    ConnectionSettings   settings;
    Session              session;
    SubscriptionManager  *mngr;
    LocalQueue           incoming;
    Message              m;

    pid_t                pid = getpid();

    try {
       settings.host = "localhost";
       settings.port = 5672;
       settings.virtualhost = "bridge";
       settings.mechanism = "ANONYMOUS";
       settings.tcpNoDelay = false;

       connection.open(settings);
       session = connection.newSession();

       mngr = new SubscriptionManager( session );

One easy way is to change:

       mngr->subscribe(incoming, "teste_flow");

to:
mngr->subscribe(incoming, "teste_flow", SubscriptionSettings(FlowControl::messageWindow(5)));

       // O esperado eh que apenas 5 elementos sejam consumidos.

You don't need the following line if you do the above; if you do want to set the default instead of the specific approach above, this line needs to happen *before* you call SubscriptionManager::subscribe().

       mngr->setFlowControl( "teste_flow", 5, 0, false );

       while ( incoming.get(m, 1000000L) ) {
          printf("[%d] [%s]\n", pid, m.getData().c_str());
       }

       printf("[%d] finalizado.\n", pid);
    } catch(const std::exception&  error) {
       fprintf(stderr, "Erro: %s\n", error.what());
       return 1;
    }

    return 0;
}

Thanks,

Hope this helps!

---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to