gpt4 book ai didi

java - 如何通过轮询和反馈来检索新传入的数据

转载 作者:行者123 更新时间:2023-12-04 05:16:10 26 4
gpt4 key购买 nike

这里的方法读取具有唯一ID且序列号不断增加的数据库,因为我是java初学者,我是否知道如何实现这种重复轮询并每次检查新传入的消息。

public void run() {
int seqId = 0;
while(true) {
List<KpiMessage> list = null;
try {
list = fullPoll(seqId);
if (!list.isEmpty()) {
seqId = list.get(0).getSequence();
incomingMessages.addAll(list);
System.out.println("waiting 3 seconds");
System.out.println("new incoming message");
Thread.sleep(3000);

}
} catch (Exception e1) {
e1.printStackTrace();
}


}
}

//Method which defines polling of the database and also count the number of Queries
public List<KpiMessage> fullPoll(int lastSeq) throws Exception {
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 and SEQ >" + lastSeq + "order by SEQ DESC");
List<KpiMessage> pojoCol = new ArrayList<KpiMessage>();
while (rs.next()) {
KpiMessage filedClass = convertRecordsetToPojo(rs);
pojoCol.add(filedClass);
}
for (KpiMessage pojoClass : pojoCol) {
System.out.print(" " + pojoClass.getSequence());
System.out.print(" " + pojoClass.getTableName());
System.out.print(" " + pojoClass.getAction());
System.out.print(" " + pojoClass.getKeyInfo1());
System.out.print(" " + pojoClass.getKeyInfo2());
System.out.println(" " + pojoClass.getEntryTime());
}
// return seqId;
return pojoCol;
}

我的目标是从数据库中轮询表,并检查新的传入消息,我可以从表中的 Header 字段 SequenceID 中找到它,该表是唯一的,并且不断增加新条目。现在我的问题是

1.假设我第一次轮询后,它读取所有条目并使线程 hibernate 6 秒,同时我如何获取新传入的数据并再次轮询?

2.还有如何添加新数据,当它第二次进行轮询并将新数据传递给另一个类时。

最佳答案

轮询器每 6 秒调用一次 fullPoll 并将 lastSeq 参数传递给它。最初 lastSeq = 0。当 Poller 获取结果列表时,它用最大 SEQ 值替换 lastSeq。 fullPoll 只检索 SEQ > lastSeq 的记录。

void run() throws Exception {
int seqId = 0;
while(true) {
List<KpiMessage> list = fullPoll(seqId);
if (!list.isEmpty()) {
seqId = list.get(0).getSequene();
}
Thread.sleep(6000);
}
}

public List<KAMessage> fullPoll(int lastSeq) throws Exception {
...
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 and SEQ > " + lastSeq + " order by SEQ
DESC");
..
}

关于java - 如何通过轮询和反馈来检索新传入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com