2

ここでメソッドは、増加し続けるシーケンス番号を持つ一意の 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;
}  

私の目標は、データベースからテーブルをポーリングし、新しい受信メッセージをチェックすることです。これは、一意であり、新しいエントリに対して増加し続けるテーブルのヘッダー フィールド SequenceID から見つけることができます。今私の問題は

1.初めてポーリングした後、すべてのエントリを読み取り、スレッドを 6 秒間スリープ状態にするとします。その間に、新しい着信データを取得して再度ポーリングするにはどうすればよいですか?

2.また、2回目のポーリングを行い、新しいデータを別のクラスに渡すときに、新しいデータを追加する方法。

4

2 に答える 2

2

ポーラーは6秒ごとにfullPollを呼び出し、lastSeqパラメーターを渡します。最初はlastSeq=0です。ポーラーが結果リストを取得すると、lastSeqが最大SEQ値に置き換えられます。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");     
      .. 
}
于 2013-01-09T15:46:21.117 に答える
1

作業を開始するために使用できるコードを次に示します。Observerパターンを使用してかなり柔軟にしようとしました。このようにして、複数の「メッセージプロセッサ」を同じポーラーに接続できます。

public class MessageRetriever implements Runnable {
    private int lastID;
    private List<MessageListener> listeners;

   ...

    public void addMessageListener(MessageListener listener) {
        this.listeners.add(listener)
    }

    public void removeMessageListener(MessageListener listener) {
        this.listeners.remove(listener)
    }

    public void run() {
        //code to repeat the polling process given some time interval    
    }  

    private void pollMessages() {
        if (this.lastID == 0)
            this.fullPoll()
        else
            this.partialPoll()           
    }

    private void fullPoll() {
        //your full poll code

        //assuming they are ordered by ID and it haves the ID field - you should
        //replace this code according to your structure
        this.lastID = pojoCol.get(pojoCol.length() - 1).getID() 

        this.fireInitialMessagesSignal(pojoCol)
    }

    private void fireInitialMessagesSignal(List<KAMessage> messages) {
        for (MessageListener listener : this.listeners)
            listener.initialMessages(messages)
    }

    private void partialPoll() {
        //code to retrieve messages *past* the lastID. You could do this by 
        //adding an extra condition on your where sentence e.g 
        //select * from msg_new_to_bde where ACTION = 804 AND SEQ > lastID order by SEQ DESC
        //the same as before
        this.lastID = pojoCol.get(pojoCol.length() - 1).getID() 

        this.fireNewMessagesListener(pojoCol)
    }

    private void fireNewMessagesListener(List<KAMessage> messages) {
        for (MessageListener listener : this.listeners)
            listener.newMessages(messages)
    }

}

そしてインターフェース

public interface MessageListener {
    public void initialMessages(List<KAMessage> messages);
    public void newMessages(List<KAMessage> messages)
}

基本的に、このアプローチを使用すると、レトリバーは実行可能であり(独自のスレッドで実行できます)、プロセス全体を処理します。最初のポーリングを実行し、指定された間隔で「部分的な」ポーリングを実行し続けます。

さまざまなイベントがさまざまなシグナルを発し、影響を受けたメッセージを登録済みのリスナーに送信し、それらは必要に応じてメッセージを処理します。

于 2013-01-09T15:59:36.413 に答える