
base.jee.api.sql.GetPendingEmail Maven / Gradle / Ivy
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base.jee.api.sql;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.sql.DataSource;
import base.Query;
import base.jee.api.model.Email;
/**
* Return a list of emails ready to be delivered. To prevent the scenario
* where an email is delivered but not marked as delivered in the database,
* email that is in the process of being delivered is marked as "in-progress".
* Email that is delivered but never unmarked as "in-progress" requires human
* interaction with the system to check what happened.
*/
public class GetPendingEmail extends Query {
private DataSource ds;
private int count;
public GetPendingEmail(DataSource ds, int count) {
this.ds = ds;
this.count = count;
}
@Override
public Query newWithParameters(Map parameters) {
return new GetPendingEmail(
((SqlAPI)parameters.get("api")).getDataSource(),
Integer.parseInt((String)parameters.get("count")));
}
public List execute() throws IOException {
List results = new LinkedList<>();
Connection c = null;
PreparedStatement s = null;
PreparedStatement t = null;
ResultSet r = null;
try {
c = ds.getConnection();
c.setAutoCommit(false);
t = c.prepareStatement("update email set retries=retries+1,in_progress=1,attempt_at=? where uuid=?");
s = c.prepareStatement(
"select uuid,email,retries from email " +
"where in_progress=0 and attempt_at<=? " +
"order by retries, attempt_at " +
"limit " + count);
s.setLong(1, new Date().getTime());
r = s.executeQuery();
while(r.next()) {
Email e = new Email(r.getString(2));
e.setUuid(UUID.fromString(r.getString(1)));
results.add(e);
t.setLong(1, (new Date().getTime()) + 60*1000 + r.getLong(3)*5*60*1000);
t.setString(2, r.getString(1));
t.execute(); // Set email as in progress, and prepare retry time in case delivery fails
}
t.close();
t = null;
c.commit();
c.close();
c = null;
} catch(SQLException e) {
throw new IOException(e);
} finally {
if(r != null) { try { r.close(); } catch (SQLException e) {} }
if(s != null) { try { s.close(); } catch (SQLException e) {} }
if(t != null) { try { t.close(); } catch (SQLException e) {} }
if(c != null) {
try { c.rollback(); } catch (SQLException e) {}
try { c.close(); } catch (SQLException e) {}
}
}
return results;
}
@Override
public String getJsonParameters() {
return "{" +
"\"count\":" + count +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy