On 06/27/2013 02:37 PM, Petr Parýzek wrote:
Hi, I was try to encapsulate qpid client into this simple classclass MySender { private: Connection *pConn; public: MySender() {pConn = NULL;} ~MySender() { if (pConn != NULL) delete pConn;} void Send(const char *Addr, const char *Msg) {if (pConn == NULL) pConn = new Connection(URL); try {if (!pConn->isOpen()) pConn->open(); Session session = pConn->createSession(); qpid::messaging::Sender sender = session.createSender(Addr); sender.send(Message(Msg)); } catch(const std::exception& error) {std::cerr << error.what() << std::endl; pConn->close(); } } }; In my program I have one global instance of this class and use its method Send(). It's simple and it works. However, it is not very effective. I would like to don't always create a new instance of Session and its Sender. But when I create a Session only once and save pointer to it in some member variable, it stop working. Second call of Send() method throws an exception. So I ask, how to keep one instance of a Session between calls?
How about something like the attached example? This holds onto both the connection and session and also keeps a map of senders for a given address (it doesn't have anyway to start removing these if not used which you might need for a 'real' application).
/* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ #include <qpid/messaging/Connection.h> #include <qpid/messaging/Message.h> #include <qpid/messaging/Sender.h> #include <qpid/messaging/Session.h> #include <iostream> #include <map> class MySender { private: std::string url; qpid::messaging::Connection connection; qpid::messaging::Session session; typedef std::map<std::string, qpid::messaging::Sender> Senders; Senders senders; public: MySender(const std::string u) : url(u) {} void send(const std::string& address, const std::string& message) { try { if (connection.isNull()) { connection = qpid::messaging::Connection(url); } if (!connection.isOpen()) { connection.open(); } if (session.isNull()) { session = connection.createSession(); } qpid::messaging::Sender sender; Senders::iterator i = senders.find(address); if (i == senders.end()) { sender = session.createSender(address); senders[address] = sender; } else { sender = i->second; } sender.send(qpid::messaging::Message(message)); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; reset(); } } void reset() { if (connection.isOpen()) connection.close(); connection = qpid::messaging::Connection(); session = qpid::messaging::Session(); } }; int main(int argc, char** argv) { MySender sender(argc > 1 ? argv[0] : "localhost"); bool done(false); while (!done) { std::string address; std::string content; std::cout << "Enter address:" << std::endl; if (getline(std::cin, address)) { std::cout << "Enter message:" << std::endl; if (getline(std::cin, content)) { sender.send(address, content); } else { done = true; } } else { done = true; } } sender.reset(); return 0; }
--------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
