Java 更好的方法是避免这样的投票。当数据更新时,是否可以将消息放入JMS队列中,以便在JMS队列中存在此类消息时(通过消息驱动bean)调用您的逻辑?(只有一种可能的方法,有很多类似的方法)Me-我会使用Quartz创建重复轮询任务,并使用JMS(更具体地说是H
Java 更好的方法是避免这样的投票。当数据更新时,是否可以将消息放入JMS队列中,以便在JMS队列中存在此类消息时(通过消息驱动bean)调用您的逻辑?(只有一种可能的方法,有很多类似的方法)Me-我会使用Quartz创建重复轮询任务,并使用JMS(更具体地说是H,java,multithreading,asynchronous,parallel-processing,polling,Java,Multithreading,Asynchronous,Parallel Processing,Polling,更好的方法是避免这样的投票。当数据更新时,是否可以将消息放入JMS队列中,以便在JMS队列中存在此类消息时(通过消息驱动bean)调用您的逻辑?(只有一种可能的方法,有很多类似的方法)Me-我会使用Quartz创建重复轮询任务,并使用JMS(更具体地说是HornetQ)处理消息传递部分。我不太喜欢在已经有坚固的轮子可用的情况下重新发明轮子。投票?你能通过触发器引发某种事件吗?@谢谢Gimby,我可以知道如何使用quartz吗?因为我也是一个初级程序员,也可以使用非同步调用此轮询,就像一个保持轮询
更好的方法是避免这样的投票。当数据更新时,是否可以将消息放入JMS队列中,以便在JMS队列中存在此类消息时(通过消息驱动bean)调用您的逻辑?(只有一种可能的方法,有很多类似的方法)Me-我会使用Quartz创建重复轮询任务,并使用JMS(更具体地说是HornetQ)处理消息传递部分。我不太喜欢在已经有坚固的轮子可用的情况下重新发明轮子。投票?你能通过触发器引发某种事件吗?@谢谢Gimby,我可以知道如何使用quartz吗?因为我也是一个初级程序员,也可以使用非同步调用此轮询,就像一个保持轮询和其他进程处理消息并更新它一样…@MartinJames我不知道,我只是被困在这里……当你没有活动时,投票是另一种方式。您可以通过阅读quartz的手册来学习如何使用它,这一点在一篇文章中很难回答。我-我会使用quartz来创建一个重复的轮询任务,并使用JMS(更具体地说是HornetQ)来处理消息传递部分。我不太喜欢在已经有坚固的轮子可用的情况下重新发明轮子。投票?你能通过触发器引发某种事件吗?@谢谢Gimby,我可以知道如何使用quartz吗?因为我也是一个初级程序员,也可以使用非同步调用此轮询,就像一个保持轮询和其他进程处理消息并更新它一样…@MartinJames我不知道,我只是被困在这里……当你没有活动时,投票是另一种方式。您可以通过阅读石英的使用手册来学习使用石英,这太多了,一篇帖子回答不了。@AdrianThat的工作原理是反复调用轮询,但如何同时检查新消息和更新的新消息…?@AdrianThat的工作原理是反复调用轮询,但如何同时检查新消息和更新的新消息…?我认为使用类似Quartz在很大程度上取决于应用程序的范围和大小。如果这是应用程序需要的唯一“作业”,那么创建线程和轮询是完全有效的。一些搜索还显示了Javas定时器和TimerTask。也许这比石英更“纤细”:阿德里安诚实地使用石英已经是一个非常纤细的解决方案。考虑到OP实际上正在做DB访问,我认为我不能把他的应用程序看作那些超级简单的应用程序。而且,使用Quartz(或其他种类的调度器)并不是什么困难或庞大的事情。我找不到任何理由来避免使用那些可靠的、使我们的应用程序更容易开发的东西。我认为使用像Quartz这样的框架在很大程度上取决于应用程序的范围和大小。如果这是应用程序需要的唯一“作业”,那么创建线程和轮询是完全有效的。一些搜索还显示了Javas定时器和TimerTask。也许这比石英更“纤细”:阿德里安诚实地使用石英已经是一个非常纤细的解决方案。考虑到OP实际上正在做DB访问,我认为我不能把他的应用程序看作那些超级简单的应用程序。而且,使用Quartz(或其他种类的调度器)并不是什么困难或庞大的事情。我找不到任何理由来避免使用可靠的软件,使我们的应用程序更容易开发。
public class PollingSynchronizer implements Runnable {
private Collection<KPIMessage> incomingMessages;
private Connection dbConnection;
/**
* Constructor. Requires to provide a reference to the KA message queue
*
* @param incomingMessages reference to message queue
*
*/
public PollingSynchronizer(Collection<KpiMessage> incomingMessages, Connection dbConnection) {
super();
this.incomingMessages = incomingMessages;
this.dbConnection = dbConnection;
}
private int sequenceId;
public int getSequenceId() {
return sequenceId;
}
public void setSequenceId(int sequenceId) {
this.sequenceId = sequenceId;
}
@Override
/**
* The method which runs Polling action and record the time at which it is done
*
*/
public void run() {
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();
}
Date currentDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
// System.out.println(sdf.format(currentDate) + " " + msg);
}
/**
* Method which defines polling of the database and also count the number of Queries
* @return
* @throws Exception
*/
public List<KpiMessage> fullPoll() throws Exception {
// int sequenceID = 0;
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select * from msg_new_to_bde where ACTION = 804 order by SEQ DESC");
List<KpiMessage> pojoCol = new ArrayList<KpiMessage>();
while (rs.next()) {
KpiMessage filedClass = convertRecordsetToPojo(rs);
pojoCol.add(filedClass);
}
return pojoCol;
}
/**
* Converts a provided record-set to a {@link KpiMessage}.
*
* 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
* the recordset to convert
* @return the converted pojo class object
* @throws SQLException
* if an sql error occurrs during processing of recordset
*/
private KpiMessage convertRecordsetToPojo(ResultSet rs) throws SQLException {
KpiMessage msg = new KpiMessage();
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;
}
}
public void run() {
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();
}
Date currentDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
// System.out.println(sdf.format(currentDate) + " " + msg);
}
}