}
}
public static List<Msg> getMessages(String node, int max) {
DBPool pool = DBPool.getPool();
DBConnection c = pool.getConnection();
List<Msg> msgs = new ArrayList<Msg>();
try {
String sql ="";
sql += "select msg_id, nodeid, status, ts from dbmq_queue_t ";
sql += "where nodeid=? AND status=? ORDER BY ts LIMIT ?";
c.prepare(sql);
c.param(node);
c.param(NEW);
c.param(max);
List<Long> ids = new ArrayList<Long>();
for (List row : c.select()) {
long msg_id = (Integer)row.get(0);
ids.add(msg_id);
}
if (ids.size() == 0) return msgs;
// get messages
sql = "select msg, nodeid, id_f, ts from dbmq_messages_t ";
sql += "where id_f IN ("+qmarks(ids.size())+")";
c.prepare(sql);
for (Long id : ids) { c.param(id); }
for (List<Object> row : c.select()) {
String msg = (String)row.get(0);
String nodeid = (String)row.get(1);
long id = (Integer)row.get(2);
Time ts = new Time((java.util.Date)row.get(3));
Msg m = new Msg(msg);
m.id = id;
m.ts = ts;
m.nodeid = nodeid;
msgs.add(m);
}
c.rollback();
// mark all status in progress
sql = "update dbmq_queue_t set status=? ";
sql += "where nodeid=? AND status=? AND msg_id IN ("+qmarks(ids.size())+")";
c.prepare(sql);
c.param(INPROGRESS);
c.param(node);
c.param(NEW);
for (Long id : ids) { c.param(id); }
int count = c.execute();
if (count != ids.size()) fail("inconsistent status write: " + count + "/"+ ids.size());
c.commit();
log.debug("read messages from :" + node + " size: " + count);
return msgs;
} catch (java.sql.SQLNonTransientConnectionException se) {
log.info("connection error in read. retrying in 30 sec: " + node);
// sleep 30 sec and retry
try {
Thread.sleep(30000);
} catch (InterruptedException ie) {
fail(ie);
}
log.info("retrying read now: " + node);
return getMessages(node,max);
} catch (Exception e) {
try {
c.rollback();
} catch (Exception e2) {
fail(e2);
}
fail(e);
} finally {
try {
c.free();
} catch (Exception e3) {
//fail(e3);
}
}
return msgs;