我想有一個異步消息傳遞在我的Java程序,所以第一步就應該不斷地監視某些表在DB的變化。當有新的傳入消息時,它應該顯示它。這應該是重複的過程,只要應用程序正在運行。如何實現異步處理
可我知道如何繼續其執行以下代碼,它有它的輪詢方法,它必須保持對自稱無限每6秒,也應該在數據庫中找到新傳入的消息。
這裏是代碼片段:
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;
}
}
這裏序列ID在不斷爲新的傳入郵件到達時增加了表的唯一ID。
P.S:「各種要求:請給給了負分(反對)的理由讓我可以解釋我的問題清楚了。」
我 - 我使用Quartz來創建一個重複投票的任務,我會使用JMS(HornetQ的更具體)來處理消息的一部分。在已經有堅如磐石的車輪可用的時候,我不會重新推出車輪。 – Gimby
輪詢?你可以通過觸發器提升某種事件嗎? –
@thanks Gimby可我知道如何使用石英的,這也是初學者水平的程序員,還如何調用使用異步地這個話題像一個不斷輪詢等proccess消息和更新它.... – Babu