reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
PersistentMessageQueue.hxx
Go to the documentation of this file.
1#if !defined(RESIP_PERSISTENTMESSAGEQUEUE_HXX)
2#define RESIP_PERSISTENTMESSAGEQUEUE_HXX
3
4// Note: Using this define will remove the dependency on BerkeleyDB, however it will render the AccountingCollector useless
5#ifndef DISABLE_BERKELEYDB_USE
6
7#ifdef WIN32
8#include <db_cxx.h>
9#elif HAVE_CONFIG_H
10#include "config.h"
11#include DB_HEADER
12//#elif defined(__APPLE__)
13//#include <db42/db_cxx.h>
14#else
15#include <db_cxx.h>
16#endif
17
18#endif
19
20#include "rutil/Data.hxx"
21#include <vector>
22
23// This class implements a persistent message queue that utilizes a BerkeleyDb backing store.
24// Messages are persisted out to disk and can be consumed from another process using the
25// PeristentMessageDequeue class.
26//
27// If PersistentMessageEnqueue::push or
28// PersistentMessageDequeue::pop or
29// PersistentMessageDequeue::commit
30// even fail, then the isRecoveryNeeded should be called. If this returns true, then the
31// PersistentMessageQueue in use should be destroyed and a new one created in order to "recover"
32// the backing store.
33//
34// Warning: If autoCommit is not used on PersistentMessageDequeue::pop then there can only be 1
35// consumer.
36//
37// Producer example:
38// -----------------
39// PersistentMessageEnqueue* queue = new PersistentMessageEnqueue;
40// if(queue->init(true, "msgqueue"))
41// {
42// for(int i = 0; i < 1000; i++)
43// {
44// Data qstr("test string" + Data(i));
45// if(!queue->push(qstr))
46// {
47// if(queue->isRecoveryNeeded())
48// {
49// delete queue;
50// queue = new PersistentMessageEnqueue;
51// if(!queue->init(true, "msgqueue")) break;
52// if(queue->push(qstr))
53// {
54// cout << "Queued: " << qstr << endl;
55// }
56// else
57// {
58// cout << "Error queuing - exiting!" << endl;
59// break;
60// }
61// }
62// else
63// {
64// cout << "Error queuing - exiting!" << endl;
65// break;
66// }
67// }
68// else
69// {
70// cout << "Queued: " << qstr << endl;
71// }
72// }
73//}
74//delete queue;
75//
76//
77// Consumer example:
78// -----------------
79//PersistentMessageDequeue* queue = new PersistentMessageDequeue;
80//if(queue->init(true, "msgqueue"))
81//{
82// vector<resip::Data> recs;
83// while(true)
84// {
85// if(queue->pop(5, recs, false))
86// {
87// if(recs.size() > 0)
88// {
89// for(int i = 0; i < recs.size(); i++)
90// {
91// cout << "Popped(" << i << "): " << recs[i] << endl;
92// }
93// queue->commit();
94// }
95// else
96// {
97// cout << "No records to pop." << endl;
98// sleepMs(1000);
99// //break;
100// }
101// }
102// else
103// {
104// if(queue->isRecoveryNeeded())
105// {
106// delete queue;
107// queue = new PersistentMessageDequeue;
108// if(!queue->init(true, "msgqueue")) break;
109// }
110// else
111// {
112// cout << "Error Dequeuing!" << endl;
113// break;
114// }
115// }
116// }
117//}
118//delete queue;
119
120
121namespace repro
122{
123#ifndef DISABLE_BERKELEYDB_USE
124class PersistentMessageQueue : public DbEnv
125#else
127#endif
128{
129public:
131 virtual ~PersistentMessageQueue();
132
133 bool init(bool sync, const resip::Data& queueName);
134 bool isRecoveryNeeded();
135
136protected:
137#ifndef DISABLE_BERKELEYDB_USE
139#endif
142};
143
145{
146public:
150
151 // Note: this has a potential to block if the a consumer crashes and leaves a lock open on the database (deadlock)
152 // typically restarting the consumer will "recover" the "dead" lock and allow this call to unblock
153 bool push(const resip::Data& data);
154};
155
157{
158public:
163
164 // returns true for success, false for failure - can return true and 0 records if none available
165 // Note: if autoCommit is used then it is safe to allow multiple consumers
166 bool pop(size_t numRecords, std::vector<resip::Data>& records, bool autoCommit);
167 bool commit();
168 void abort();
169
170private:
171 size_t mNumRecords;
172 };
173
174}
175#endif
176
177/* ====================================================================
178 * The Vovida Software License, Version 1.0
179 *
180 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
181 *
182 * Redistribution and use in source and binary forms, with or without
183 * modification, are permitted provided that the following conditions
184 * are met:
185 *
186 * 1. Redistributions of source code must retain the above copyright
187 * notice, this list of conditions and the following disclaimer.
188 *
189 * 2. Redistributions in binary form must reproduce the above copyright
190 * notice, this list of conditions and the following disclaimer in
191 * the documentation and/or other materials provided with the
192 * distribution.
193 *
194 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
195 * and "Vovida Open Communication Application Library (VOCAL)" must
196 * not be used to endorse or promote products derived from this
197 * software without prior written permission. For written
198 * permission, please contact vocal@vovida.org.
199 *
200 * 4. Products derived from this software may not be called "VOCAL", nor
201 * may "VOCAL" appear in their name, without prior written
202 * permission of Vovida Networks, Inc.
203 *
204 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
205 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
206 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
207 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
208 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
209 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
210 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
211 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
212 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
213 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
214 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
215 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
216 * DAMAGE.
217 *
218 * ====================================================================
219 *
220 * This software consists of voluntary contributions made by Vovida
221 * Networks, Inc. and many individuals on behalf of Vovida Networks,
222 * Inc. For more information on Vovida Networks, Inc., please see
223 * <http://www.vovida.org/>.
224 *
225 */
bool pop(size_t numRecords, std::vector< resip::Data > &records, bool autoCommit)
PersistentMessageDequeue(const resip::Data &baseDir)
PersistentMessageEnqueue(const resip::Data &baseDir)
bool push(const resip::Data &data)
bool init(bool sync, const resip::Data &queueName)
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
This Class is used to implement a primitive form of RPC using loose XML formatting.
void list()
Definition userAdmin.cxx:66