5.1. Creating and Closing Sessions

5.1. Creating and Closing Sessions

All of the examples in this section have been written using the Apache Qpid C++ API, which is the C++ API for MRG Messaging. The examples use the same skeleton code to initialize the program, create a session, and clean up before exiting:

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

#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <sstream>

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


int main() { 
    char * host = "127.0.0.1";
    int port = 5672;
    Connection connection;
    Message msg;
    try { 
        connection.open(host, port);
        Session session =  connection.newSession();

        //--------- Main body of program --------------------------------------------

        //---------------------------------------------------------------------------

        connection.close();
        return 0;
    } catch(const std::exception& error) { 
        std::cout << error.what() << std::endl;
    }
    return 1;
}