use of org.springframework.jdbc.core.RowCallbackHandler in project opennms by OpenNMS.
the class AlarmdIT method testPersistManyAlarmsAtOnce.
@Test
public void testPersistManyAlarmsAtOnce() throws InterruptedException {
int numberOfAlarmsToReduce = 10;
//there should be no alarms in the alarms table
assertEmptyAlarmTable();
final String reductionKey = "countThese";
final MockNode node = m_mockNetwork.getNode(1);
final long millis = System.currentTimeMillis() + 2500;
final CountDownLatch signal = new CountDownLatch(numberOfAlarmsToReduce);
for (int i = 1; i <= numberOfAlarmsToReduce; i++) {
MockUtil.println("Creating Runnable: " + i + " of " + numberOfAlarmsToReduce + " events to reduce.");
class EventRunner implements Runnable {
@Override
public void run() {
try {
while (System.currentTimeMillis() < millis) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
MockUtil.println(e.getMessage());
}
}
sendNodeDownEvent(reductionKey, node);
} catch (Throwable t) {
t.printStackTrace();
} finally {
signal.countDown();
}
}
}
Runnable r = new EventRunner();
Thread p = new Thread(r);
p.start();
}
signal.await();
//this should be the first occurrence of this alarm
//there should be 1 alarm now
int rowCount = m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue();
Integer counterColumn = m_jdbcTemplate.queryForObject("select counter from alarms where reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
MockUtil.println("rowcCount is: " + rowCount + ", expected 1.");
MockUtil.println("counterColumn is: " + counterColumn + ", expected " + numberOfAlarmsToReduce);
assertEquals(1, rowCount);
if (numberOfAlarmsToReduce != counterColumn) {
final List<Integer> reducedEvents = new ArrayList<Integer>();
m_jdbcTemplate.query("select eventid from events where alarmID is not null", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
reducedEvents.add(rs.getInt(1));
}
});
Collections.sort(reducedEvents);
final List<Integer> nonReducedEvents = new ArrayList<Integer>();
m_jdbcTemplate.query("select eventid from events where alarmID is null", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
nonReducedEvents.add(rs.getInt(1));
}
});
Collections.sort(nonReducedEvents);
fail("number of alarms to reduce (" + numberOfAlarmsToReduce + ") were not reduced into a single alarm (instead the counter column reads " + counterColumn + "); " + "events that were reduced: " + StringUtils.collectionToCommaDelimitedString(reducedEvents) + "; events that were not reduced: " + StringUtils.collectionToCommaDelimitedString(nonReducedEvents));
}
Integer alarmId = m_jdbcTemplate.queryForObject("select alarmId from alarms where reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
rowCount = m_jdbcTemplate.queryForObject("select count(*) from events where alarmid = ?", new Object[] { alarmId }, Integer.class).intValue();
MockUtil.println(String.valueOf(rowCount) + " of events with alarmid: " + alarmId);
// assertEquals(numberOfAlarmsToReduce, rowCount);
rowCount = m_jdbcTemplate.queryForObject("select count(*) from events where alarmid is null", Integer.class).intValue();
MockUtil.println(String.valueOf(rowCount) + " of events with null alarmid");
assertEquals(10, rowCount);
}
use of org.springframework.jdbc.core.RowCallbackHandler in project opennms by OpenNMS.
the class AlarmdIT method testPersistAlarm.
@Test
public void testPersistAlarm() throws Exception {
final MockNode node = m_mockNetwork.getNode(1);
//there should be no alarms in the alarms table
assertEmptyAlarmTable();
//this should be the first occurrence of this alarm
//there should be 1 alarm now
sendNodeDownEvent("%nodeid%", node);
Thread.sleep(1000);
assertEquals(1, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());
//this should be the second occurrence and shouldn't create another row
//there should still be only 1 alarm
sendNodeDownEvent("%nodeid%", node);
Thread.sleep(1000);
assertEquals(1, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());
//this should be a new alarm because of the new key
//there should now be 2 alarms
sendNodeDownEvent("DontReduceThis", node);
Thread.sleep(1000);
assertEquals(2, m_jdbcTemplate.queryForObject("select count(*) from alarms", Integer.class).intValue());
MockUtil.println("Going for the print of the counter column");
m_jdbcTemplate.query("select reductionKey, sum(counter) from alarms group by reductionKey", new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
MockUtil.println("count for reductionKey: " + rs.getString(1) + " is: " + rs.getObject(2));
}
});
}
use of org.springframework.jdbc.core.RowCallbackHandler in project dal by ctripcorp.
the class DaoBySqlBuilder method updateAndGetAllTasks.
public List<GenTaskBySqlBuilder> updateAndGetAllTasks(int projectId) {
final List<GenTaskBySqlBuilder> tasks = new ArrayList<>();
this.jdbcTemplate.query("SELECT id, project_id, db_name,table_name,class_name,method_name,sql_style," + "crud_type,fields,where_condition,sql_content,`generated`,version,update_user_no," + "update_time,comment,scalarType,pagination,orderby,approved,approveMsg,hints " + " FROM task_auto WHERE project_id=?", new Object[] { projectId }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
GenTaskBySqlBuilder task = GenTaskBySqlBuilder.visitRow(rs);
task.setGenerated(true);
if (updateTask(task) > 0) {
tasks.add(task);
}
}
});
return tasks;
}
use of org.springframework.jdbc.core.RowCallbackHandler in project dal by ctripcorp.
the class DaoByTableViewSp method updateAndGetTasks.
public List<GenTaskByTableViewSp> updateAndGetTasks(int projectId) {
final List<GenTaskByTableViewSp> tasks = new ArrayList<>();
this.jdbcTemplate.query("SELECT id, project_id,db_name,table_names,view_names,sp_names,prefix,suffix," + "cud_by_sp,pagination,`generated`,version,update_user_no,update_time," + "comment,sql_style,api_list,approved,approveMsg FROM task_table " + "WHERE project_id=? AND `generated`=FALSE", new Object[] { projectId }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
GenTaskByTableViewSp task = GenTaskByTableViewSp.visitRow(rs);
task.setGenerated(true);
if (updateTask(task) > 0) {
tasks.add(task);
}
}
});
return tasks;
}
use of org.springframework.jdbc.core.RowCallbackHandler in project dal by ctripcorp.
the class DaoByFreeSql method updateAndGetAllTasks.
public List<GenTaskByFreeSql> updateAndGetAllTasks(int projectId) {
final List<GenTaskByFreeSql> tasks = new ArrayList<>();
this.jdbcTemplate.query("SELECT id, project_id,db_name,class_name,pojo_name,method_name,crud_type," + "sql_content,parameters,`generated`,version,update_user_no,update_time,comment," + "scalarType,pojoType,pagination,sql_style,approved,approveMsg,hints" + " FROM task_sql WHERE project_id=?", new Object[] { projectId }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
GenTaskByFreeSql task = GenTaskByFreeSql.visitRow(rs);
task.setGenerated(true);
if (updateTask(task) > 0) {
tasks.add(task);
}
}
});
return tasks;
}
Aggregations