Package edu.emory.mathcs.backport.java.util.concurrent

Examples of edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch


      fillQueue(queueA);

      // Test sending to Queue B it should block.
      // Since even though  the it's queue limits have not been reached, the connection
      // is blocked.
      CountDownLatch pubishDoneToQeueuB = asyncSendTo(queueB, "Message 1");
      assertFalse( pubishDoneToQeueuB.await(2, TimeUnit.SECONDS) );     
    }
View Full Code Here


    }   
    keepGoing.set(false);
  }

  private CountDownLatch asyncSendTo(final ActiveMQQueue queue, final String message) throws JMSException {
    final CountDownLatch done = new CountDownLatch(1);
    new Thread("Send thread.") {
      public void run() {
        Session session=null;
          try {
          session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
          MessageProducer producer = session.createProducer(queue);
          producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
          producer.send(session.createTextMessage(message));
          done.countDown();
        } catch (JMSException e) {
        } finally {
          safeClose(session);
        }
      }
View Full Code Here

        consumerConnection= getConsumerConnection(factory);
        //create durable subs
        consumer = getConsumer(consumerConnection);
        final List consumerList = new ArrayList();
       
        final CountDownLatch latch = new CountDownLatch(1);
        consumer.setMessageListener(new MessageListener() {

            public void onMessage(Message msg){
                try{
                    //sleep to act as a slow consumer
                    //which will force a mix of direct and polled dispatching
                    //using the cursor on the broker
                    Thread.sleep(50);
                }catch(Exception e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                consumerList.add(msg);
                if (consumerList.size()==MESSAGE_COUNT) {
                    latch.countDown();
                }
               
            }
           
        });
        for (int i =MESSAGE_COUNT/10; i < MESSAGE_COUNT; i++) {
            TextMessage msg=session.createTextMessage("test"+i);
            senderList.add(msg);
          
            producer.send(msg);
           
          
        }  
       
       
        latch.await(300000,TimeUnit.MILLISECONDS);
        assertEquals("Still dipatching - count down latch not sprung" , latch.getCount(),0);
        assertEquals("cosumerList - expected: " + MESSAGE_COUNT + " but was: " + consumerList.size(),consumerList.size(),senderList.size());
        assertEquals(senderList,consumerList);
        producerConnection.close();
        consumerConnection.close();      
    }
View Full Code Here

     */
    public void doTestAsynchRecover() throws JMSException, InterruptedException {
       
        final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        final String errorMessage[] = new String[]{null};
        final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
       
        MessageConsumer consumer = session.createConsumer(dest);       
       
        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        producer.send(session.createTextMessage("First"));
        producer.send(session.createTextMessage("Second"));
       
        consumer.setMessageListener(new MessageListener(){
            int counter;
            public void onMessage(Message msg) {
                counter++;
                try {
                    TextMessage message = (TextMessage)msg;
                    switch( counter ) {
                      case 1:
                            assertEquals("First", message.getText());
                            assertFalse(message.getJMSRedelivered());
                            message.acknowledge();
                         
                            break;                       
                      case 2:
                            assertEquals("Second", message.getText());
                            assertFalse(message.getJMSRedelivered());
                            session.recover();
                            break;
                           
                      case 3:
                            assertEquals("Second", message.getText());
                            assertTrue(message.getJMSRedelivered());                           
                            message.acknowledge();
                          doneCountDownLatch.countDown();
                          break;
                         
                      default:
                          errorMessage[0]="Got too many messages: "+counter;
                          doneCountDownLatch.countDown();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                  errorMessage[0]="Got exception: "+e;
                  doneCountDownLatch.countDown();
                }
            }
        });
        connection.start();
       
        if( doneCountDownLatch.await(5, TimeUnit.SECONDS) ) {
            if( errorMessage[0]!=null ) {
                fail(errorMessage[0]);
            }           
        } else {
            fail("Timeout waiting for async message delivery to complete.");
View Full Code Here

     */
    public void doTestAsynchRecoverWithAutoAck() throws JMSException, InterruptedException {
       
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final String errorMessage[] = new String[]{null};
        final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
       
        MessageConsumer consumer = session.createConsumer(dest);       
       
        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        producer.send(session.createTextMessage("First"));
        producer.send(session.createTextMessage("Second"));
       
        consumer.setMessageListener(new MessageListener(){
            int counter;
            public void onMessage(Message msg) {
                counter++;
                try {
                    TextMessage message = (TextMessage)msg;
                    switch( counter ) {
                      case 1:
                            assertEquals("First", message.getText());
                            assertFalse(message.getJMSRedelivered());
                            break;                       
                      case 2:
                          // This should rollback the delivery of this message.. and re-deliver.
                            assertEquals("Second", message.getText());
                            assertFalse(message.getJMSRedelivered());
                            session.recover();
                            break;
                           
                      case 3:
                            assertEquals("Second", message.getText());
                            assertTrue(message.getJMSRedelivered());                           
                          doneCountDownLatch.countDown();
                          break;
                         
                      default:
                          errorMessage[0]="Got too many messages: "+counter;
                          doneCountDownLatch.countDown();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                  errorMessage[0]="Got exception: "+e;
                  doneCountDownLatch.countDown();
                }
            }
        });
        connection.start();
       
        if( doneCountDownLatch.await(5000, TimeUnit.SECONDS) ) {
            if( errorMessage[0]!=null ) {
                fail(errorMessage[0]);
            }           
        } else {
            fail("Timeout waiting for async message delivery to complete.");
View Full Code Here

        try {
            if (journal == null )
                throw new IllegalStateException("Journal is closed.");
           
            long now = System.currentTimeMillis();
            CountDownLatch latch = null;
            synchronized(this) {
                latch = nextCheckpointCountDownLatch;
                lastCheckpointRequest = now;
                if( fullCheckpoint ) {
                    this.fullCheckPoint = true;
                }
            }
           
            checkpointTask.wakeup();
           
            if (sync) {
                log.debug("Waking for checkpoint to complete.");
                latch.await();
            }
        }
        catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.warn("Request to start checkpoint failed: " + e, e);
View Full Code Here

    /**
     * This does the actual checkpoint.
     * @return
     */
    public boolean doCheckpoint() {
        CountDownLatch latch = null;
        boolean fullCheckpoint;
        synchronized(this) {                      
            latch = nextCheckpointCountDownLatch;
            nextCheckpointCountDownLatch = new CountDownLatch(1);
            fullCheckpoint = this.fullCheckPoint;
            this.fullCheckPoint=false;           
        }       
        try {

            log.debug("Checkpoint started.");
            RecordLocation newMark = null;

            ArrayList futureTasks = new ArrayList(queues.size()+topics.size());
           
            //
            // We do many partial checkpoints (fullCheckpoint==false) to move topic messages
            // to long term store as soon as possible. 
            //
            // We want to avoid doing that for queue messages since removes the come in the same
            // checkpoint cycle will nullify the previous message add.  Therefore, we only
            // checkpoint queues on the fullCheckpoint cycles.
            //
            if( fullCheckpoint ) {               
                Iterator iterator = queues.values().iterator();
                while (iterator.hasNext()) {
                    try {
                        final JournalMessageStore ms = (JournalMessageStore) iterator.next();
                        FutureTask task = new FutureTask(new Callable() {
                            public Object call() throws Exception {
                                return ms.checkpoint();
                            }});
                        futureTasks.add(task);
                        checkpointExecutor.execute(task);                       
                    }
                    catch (Exception e) {
                        log.error("Failed to checkpoint a message store: " + e, e);
                    }
                }
            }

            Iterator iterator = topics.values().iterator();
            while (iterator.hasNext()) {
                try {
                    final JournalTopicMessageStore ms = (JournalTopicMessageStore) iterator.next();
                    FutureTask task = new FutureTask(new Callable() {
                        public Object call() throws Exception {
                            return ms.checkpoint();
                        }});
                    futureTasks.add(task);
                    checkpointExecutor.execute(task);                       
                }
                catch (Exception e) {
                    log.error("Failed to checkpoint a message store: " + e, e);
                }
            }

            try {
                for (Iterator iter = futureTasks.iterator(); iter.hasNext();) {
                    FutureTask ft = (FutureTask) iter.next();
                    RecordLocation mark = (RecordLocation) ft.get();
                    // We only set a newMark on full checkpoints.
                    if( fullCheckpoint ) {
                        if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
                            newMark = mark;
                        }
                    }
                }
            } catch (Throwable e) {
                log.error("Failed to checkpoint a message store: " + e, e);
            }
           

            if( fullCheckpoint ) {
                try {
                    if (newMark != null) {
                        log.debug("Marking journal at: " + newMark);
                        journal.setMark(newMark, true);
                    }
                }
                catch (Exception e) {
                    log.error("Failed to mark the Journal: " + e, e);
                }
   
                if (longTermPersistence instanceof JDBCPersistenceAdapter) {
                    // We may be check pointing more often than the checkpointInterval if under high use
                    // But we don't want to clean up the db that often.
                    long now = System.currentTimeMillis();
                    if( now > lastCleanup+checkpointInterval ) {
                        lastCleanup = now;
                        ((JDBCPersistenceAdapter) longTermPersistence).cleanup();
                    }
                }
            }

            log.debug("Checkpoint done.");
        }
        finally {
            latch.countDown();
        }
        synchronized(this) {
            return this.fullCheckPoint;
        }       

View Full Code Here

    /*
     * Test method for 'org.apache.activemq.kaha.Store.close()'
     */
    public void testLoad() throws Exception{
        CountDownLatch start=new CountDownLatch(NUM_LOADERS);
        CountDownLatch stop=new CountDownLatch(NUM_LOADERS);
        for(int i=0;i<NUM_LOADERS;i++){
            Loader loader=new Loader("loader:"+i,store,COUNT,start,stop);
            loader.start();
        }
        stop.await();
    }
View Full Code Here

        try {
            if (journal == null )
                throw new IllegalStateException("Journal is closed.");
           
            long now = System.currentTimeMillis();
            CountDownLatch latch = null;
            synchronized(this) {
                latch = nextCheckpointCountDownLatch;
                lastCheckpointRequest = now;
                if( fullCheckpoint ) {
                    this.fullCheckPoint = true;
                }
            }
           
            checkpointTask.wakeup();
           
            if (sync) {
                log.debug("Waking for checkpoint to complete.");
                latch.await();
            }
        }
        catch (InterruptedException e) {
            log.warn("Request to start checkpoint failed: " + e, e);
        }
View Full Code Here

    /**
     * This does the actual checkpoint.
     * @return
     */
    public boolean doCheckpoint() {
        CountDownLatch latch = null;
        boolean fullCheckpoint;
        synchronized(this) {                      
            latch = nextCheckpointCountDownLatch;
            nextCheckpointCountDownLatch = new CountDownLatch(1);
            fullCheckpoint = this.fullCheckPoint;
            this.fullCheckPoint=false;           
        }       
        try {

            log.debug("Checkpoint started.");
            RecordLocation newMark = null;

            ArrayList futureTasks = new ArrayList(queues.size()+topics.size());
           
            //
            // We do many partial checkpoints (fullCheckpoint==false) to move topic messages
            // to long term store as soon as possible. 
            //
            // We want to avoid doing that for queue messages since removes the come in the same
            // checkpoint cycle will nullify the previous message add.  Therefore, we only
            // checkpoint queues on the fullCheckpoint cycles.
            //
            if( fullCheckpoint ) {               
                Iterator iterator = queues.values().iterator();
                while (iterator.hasNext()) {
                    try {
                        final RapidMessageStore ms = (RapidMessageStore) iterator.next();
                        FutureTask task = new FutureTask(new Callable() {
                            public Object call() throws Exception {
                                return ms.checkpoint();
                            }});
                        futureTasks.add(task);
                        checkpointExecutor.execute(task);                       
                    }
                    catch (Exception e) {
                        log.error("Failed to checkpoint a message store: " + e, e);
                    }
                }
            }

            Iterator iterator = topics.values().iterator();
            while (iterator.hasNext()) {
                try {
                    final RapidTopicMessageStore ms = (RapidTopicMessageStore) iterator.next();
                    FutureTask task = new FutureTask(new Callable() {
                        public Object call() throws Exception {
                            return ms.checkpoint();
                        }});
                    futureTasks.add(task);
                    checkpointExecutor.execute(task);                       
                }
                catch (Exception e) {
                    log.error("Failed to checkpoint a message store: " + e, e);
                }
            }

            try {
                for (Iterator iter = futureTasks.iterator(); iter.hasNext();) {
                    FutureTask ft = (FutureTask) iter.next();
                    RecordLocation mark = (RecordLocation) ft.get();
                    // We only set a newMark on full checkpoints.
                    if( fullCheckpoint ) {
                        if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
                            newMark = mark;
                        }
                    }
                }
            } catch (Throwable e) {
                log.error("Failed to checkpoint a message store: " + e, e);
            }
           

            if( fullCheckpoint ) {
                try {
                    if (newMark != null) {
                        log.debug("Marking journal at: " + newMark);
                        journal.setMark(newMark, true);
                    }
                }
                catch (Exception e) {
                    log.error("Failed to mark the Journal: " + e, e);
                }
               
// TODO: do we need to implement a periodic clean up?
              
//                if (longTermPersistence instanceof JDBCPersistenceAdapter) {
//                    // We may be check pointing more often than the checkpointInterval if under high use
//                    // But we don't want to clean up the db that often.
//                    long now = System.currentTimeMillis();
//                    if( now > lastCleanup+checkpointInterval ) {
//                        lastCleanup = now;
//                        ((JDBCPersistenceAdapter) longTermPersistence).cleanup();
//                    }
//                }
            }

            log.debug("Checkpoint done.");
        }
        finally {
            latch.countDown();
        }
        synchronized(this) {
            return this.fullCheckPoint;
        }       

View Full Code Here

TOP

Related Classes of edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.