Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling Threadpools & wait notifyALL()
    primarykey
    data
    text
    <p>How to handle the thread pool where one is polling while the other should update new incoming data after processing. The program execution beings in a controller class which has a main method and thread pool:</p> <p>The main class Controller</p> <pre><code> public static void main(String[] args) throws InterruptedException { RunnableController controller = new RunnableController(); Accumulator acque = new Accumulator(); controller.initializeDb(); controller.initialiseThreads(acque); controller.initialUpdate(acque); } </code></pre> <p>The Run method for Polling class:</p> <pre><code> public void run() { int seqId = 0; List&lt;KpiMessage&gt; list = null; while(true) { try{ list = fullPoll(seqId); if (!list.isEmpty()) { accumulator.manageIngoing(list); } } catch (Exception e){ e.printStackTrace(); } } } public List&lt;KpiMessage&gt; fullPoll(int lastSeq) throws Exception { Statement st = dbConnection.createStatement(); System.out.println("Polling"); ResultSet rs = st.executeQuery("Select * from msg_new_to_bde where ACTION = 804 and SEQ &gt;" + lastSeq + "order by SEQ DESC"); return pojoCol; } </code></pre> <p>Run method for processing:</p> <pre><code> public void run() { try { generate(accumulator.outgoingQueue); accumulator.manageOutgoing(accumulator.outgoingQueue, dbConnection); } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>Method for updating into Database</p> <pre><code> public void updateDb(Collection&lt;KpiMessage&gt; updatedQueue, Connection dbConnection) throws SQLException{ for(KpiMessage pojoClass : updatedQueue){ Statement stmtupd = dbConnection.createStatement(); System.out.println("Updating"); String query = "UPDATE msg_new_to_bde SET KEYINFO1= 'Processed', KEYINFO2 = 'Updated' WHERE ACTION = 804"; stmtupd.executeUpdate(query);**My Execution stops here** </code></pre> <p>Finally an accumulator class for maintaing all these queues:</p> <pre><code> public boolean isUsed = false; public synchronized void manageIngoing(List&lt;KpiMessage&gt; list){ if(this.isUsed){ try { wait(); System.out.println("first wait"); } catch (Exception e1) { e1.printStackTrace(); } } System.out.println("recived pass after update"); this.getIncomingQueue().addAll(list); //incoming queue copied to outgoing queue this.setOutgoingQueue(this.getIncomingQueue()); System.out.println("waiting"); System.out.println("new incoming message"); this.isUsed = false; notifyAll(); } /** * Method which handles synchronization using wait and notify for outgoing messages after polling * @param outgoingQueue * @param dbConnection */ public synchronized void manageOutgoing(Collection&lt;KpiMessage&gt; outgoingQueue, Connection dbConnection){ if(!this.isUsed) { try { System.out.println("second wait"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.isUsed = true; DBhandler dbhandler = new DBhandler(); try { dbhandler.updateDb(getOutgoingQueue(), dbConnection); } catch (SQLException e) { e.printStackTrace(); } notifyAll(); } } </code></pre> <p><strong>My task and Question is :</strong></p> <p>1.The controller should handle both the threads Poller &amp; processor and accumulator handles the incoming and outgoing queues, finally fed into to updated queue for updating DB after processing</p> <p>2.My class here just does polling once, is not able to update ,execution stops at </p> <p>3.Is my wait(), notifyALL() handle correct here. </p> <p><strong>How to achieve repeated polling and updation here?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload