Java 如何定期轮询并检查数据库中的新传入数据
在这里,该方法读取具有唯一ID的数据库,其序列号不断增加,因为我是java初学者,我是否知道如何实现这种重复轮询并每次检查新传入的消息Java 如何定期轮询并检查数据库中的新传入数据,java,multithreading,object,methods,polling,Java,Multithreading,Object,Methods,Polling,在这里,该方法读取具有唯一ID的数据库,其序列号不断增加,因为我是java初学者,我是否知道如何实现这种重复轮询并每次检查新传入的消息 /** * Method which defines polling of the database and also count the number of Queries * @return pojo collection * @throws Exception */ public List<KAMessage> fullPoll() t
/**
* Method which defines polling of the database and also count the number of Queries
* @return pojo collection
* @throws Exception
*/
public List<KAMessage> fullPoll() throws Exception {
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 order by SEQ DESC");
List<KAMessage> pojoCol = new ArrayList<KAMessage>();
while (rs.next()) {
KAMessage filedClass = convertRecordsetToPojo(rs);
pojoCol.add(filedClass);
}
return pojoCol;
}
/**
* Converts a provided record-set to a {@link KAMessage}.
*
* The following attributes are copied from record-set to pojo:
*
* <ul>
* <li>SEQ</li>
* <li>TABLENAME</li>
* <li>ENTRYTIME</li>
* <li>STATUS</li>
* </ul>
*
* @param rs
* @return the converted pojo class object
* @throws SQLException
*
*/
private KAMessage convertRecordsetToPojo(ResultSet rs) throws SQLException {
KAMessage msg = new KAMessage();
int sequence = rs.getInt("SEQ");
msg.setSequence(sequence);
int action = rs.getInt("ACTION");
msg.setAction(action);
String tablename = rs.getString("TABLENAME");
msg.setTableName(tablename);
Timestamp entrytime = rs.getTimestamp("ENTRYTIME");
Date entryTime = new Date(entrytime.getTime());
msg.setEntryTime(entryTime);
Timestamp processingtime = rs.getTimestamp("PROCESSINGTIME");
if (processingtime != null) {
Date processingTime = new Date(processingtime.getTime());
msg.setProcessingTime(processingTime);
}
String keyInfo1 = rs.getString("KEYINFO1");
msg.setKeyInfo1(keyInfo1);
String keyInfo2 = rs.getString("KEYINFO2");
msg.setKeyInfo2(keyInfo2);
return msg;
}
}
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
如何使用准备好的语句将参数传递到该代码的查询中,我在这里遇到了难题
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
public List<KAMessage> fullPoll() throws Exception {
PreparedStatement oldSeq = null;
PreparedStatement newSeq = null;
Statement st = dbConnection.createStatement();
System.out.println("Polling");
String query = "select * from msg_new_to_bde where ACTION = 804 and SEQ between oldSeq and newSeq order by SEQ DESC";// insert in table
pstmt = conn.prepareStatement(query);
}
public List fullPoll()引发异常{
PreparedStatement oldSeq=null;
PreparedStatement newSeq=null;
语句st=dbConnection.createStatement();
System.out.println(“轮询”);
String query=“select*from msg_new_to_bde,其中ACTION=804,并按SEQ DESC在oldSeq和newSeq之间排序”;//插入表中
pstmt=conn.preparest陈述(查询);
}
您可以在while循环中调用方法fullPoll()
,并使用thread.sleep()
在调用之间等待
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
另一个解决方案是使用完整的调度程序框架,如quartz
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
希望这能回答您的问题。实现重复轮询可能有多种方法,我能想到的最简单的方法是将轮询方法放在一段时间内,例如使用thread.sleep:
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
fullPoll();
Thread.sleep(10000);
}
不要忘记使用try-catch,因为抛出了异常。
或者,您可以使用计时器任务或框架之类的东西作为石英
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
为了检查数据库中是否有新数据,我可以动态地想到以下方法(可以有更优化的方法):
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
您可以记录行数,以便了解是否添加了其他行,以便重新运行查询
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
这不包括删除并重新插入行的情况,为了涵盖这一点,您可以尝试将上次查询数据库时使用的max id存储在某个位置,并每次检查新的max id是否与上次存储的max id不同,如果是,则数据库已更改
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
例如,如果将旧索引保留在变量oldSeq中,将新索引保留在变量newSeq中,并且只希望获取新添加的消息,则可以使用以下查询:
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
select * from msg_new_to_bde where ACTION = 804 and SEQ between oldSeq and newSeq order by SEQ DESC
您应该检查数据库的between是否还包括测试值(oldSeq和newSeq)@frederikdebaker我更喜欢使用while loop和thread.sleep,这样我就可以学习编程,比如说如果我使用while loop和thread.sleep()6秒钟,我如何才能为下一次轮询只提取新的传入数据,在这里,我有一个唯一的序列号,它会随着每一条新的传入消息而增加……在这种情况下,您需要跟踪上一次轮询的最后一个序列号。您可以在db查询中使用此序列号仅检索新的序列号。请参考我在上面添加的代码,如何查找此轮询的新收入消息谢谢,我可以知道如何从我上面添加的代码中查找新的传入消息吗?谢谢,我可以知道如何在查询中获取此变量oldSeq和newSeq。。。请向我提供一个方法,我的意思是如何在查询中传递这个变量,并每次提取它,以便我可以在另一个类中处理新传入的消息。您可以使用一个查询作为select max(seq)from msg_new_to_bde,这会在每次查询db时为您提供最大值,将其保留在newSeq变量中,将其与oldSeq变量进行比较,id if newSeq>oldSeq继续查询,然后oldSeq=newSeq,如果不继续查询,则检查此语句是否已准备好,为了了解其用法,在传递上述链接中所述的参数之前,必须从数据库中获取我的答案中描述的参数,因此,一般的流程是,从db获取newSeq,与oldSeq比较(第一时间oldSeq将为0),如果它们不同,则执行传递oldSeq和newSeq的查询,如果不执行,则不执行查询
while(true){
try {
incomingMessages.addAll(fullPoll());
System.out.println("waiting 6 seconds");
//perform this operation in a loop
Thread.sleep(6000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}