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

Examples of edu.emory.mathcs.backport.java.util.concurrent.Semaphore$NonfairSync


    public void testPerformance() throws Exception {
       
        System.out.println("Running Benchmark for destination="+destination+", producers="+PRODUCER_COUNT+", consumers="+CONSUMER_COUNT+", deliveryMode="+deliveryMode);
        final int CONSUME_COUNT = destination.isTopic() ? CONSUMER_COUNT*PRODUCE_COUNT : PRODUCE_COUNT;

        final Semaphore consumersStarted = new Semaphore(1-(CONSUMER_COUNT));        
        final Semaphore producersFinished = new Semaphore(1-(PRODUCER_COUNT));
        final Semaphore consumersFinished = new Semaphore(1-(CONSUMER_COUNT));        
        final ProgressPrinter printer = new ProgressPrinter(PRODUCE_COUNT+CONSUME_COUNT, 10);
       
        // Start a producer and consumer
   
        profilerPause("Benchmark ready.  Start profiler ");
       
        long start = System.currentTimeMillis();
       
       
        final AtomicInteger receiveCounter = new AtomicInteger(0);
        for( int i=0; i < CONSUMER_COUNT; i++) {
            new Thread() {
                public void run() {
                    try {
                       
                        // Consume the messages    
                        StubConnection connection = new StubConnection(broker);
                        ConnectionInfo connectionInfo = createConnectionInfo();
                        connection.send(connectionInfo);

                        SessionInfo sessionInfo = createSessionInfo(connectionInfo);
                        ConsumerInfo consumerInfo = createConsumerInfo(sessionInfo, destination);
                        consumerInfo.setPrefetchSize(1000);
                        connection.send(sessionInfo);
                        connection.send(consumerInfo);
                       
                        consumersStarted.release();
                       
                        while( receiveCounter.get() < CONSUME_COUNT ) {
               
                            int counter=0;
                            // Get a least 1 message.
                            Message msg = receiveMessage(connection, 2000);
                            if( msg!=null ) {
                                printer.increment();
                                receiveCounter.incrementAndGet();
                               
                                counter++;
                               
                                // Try to piggy back a few extra message acks if they are ready.
                                Message extra=null;
                                while( (extra = receiveMessage(connection,0))!=null ) {
                                    msg=extra;
                                    printer.increment();
                                    receiveCounter.incrementAndGet();
                                    counter++;
                                }
                            }
                           
                           
                            if(msg!=null) {
                                connection.send(createAck(consumerInfo, msg, counter, MessageAck.STANDARD_ACK_TYPE));
                            } else if ( receiveCounter.get() < CONSUME_COUNT )  {
                                System.out.println("Consumer stall, waiting for message #"+receiveCounter.get()+1);
                            }
                        }
                       
                        connection.send(closeConsumerInfo(consumerInfo));                       
                    } catch (Throwable e) {
                        e.printStackTrace();
                    } finally {
                        consumersFinished.release();               
                    }
                }

            }.start();
        }
       
        // Make sure that the consumers are started first to avoid sending messages
        // before a topic is subscribed so that those messages are not missed.
        consumersStarted.acquire();
       
        // Send the messages in an async thread.
        for( int i=0; i < PRODUCER_COUNT; i++) {
            new Thread() {
                public void run() {
                    try {
                        StubConnection connection = new StubConnection(broker);
                        ConnectionInfo connectionInfo = createConnectionInfo();
                        connection.send(connectionInfo);
                       
                        SessionInfo sessionInfo = createSessionInfo(connectionInfo);
                        ProducerInfo producerInfo = createProducerInfo(sessionInfo);
                        connection.send(sessionInfo);
                        connection.send(producerInfo);
   
                        for(int i=0; i < PRODUCE_COUNT/PRODUCER_COUNT; i++) {
                            Message message = createMessage(producerInfo, destination);
                            message.setPersistent(deliveryMode);
                            message.setResponseRequired(false);
                            connection.send(message);
                            printer.increment();
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    } finally {
                        producersFinished.release();               
                    }
                };
            }.start();
        }
       
        producersFinished.acquire();
        long end1 = System.currentTimeMillis();
        consumersFinished.acquire();
        long end2 = System.currentTimeMillis();
       
        System.out.println("Results for destination="+destination+", producers="+PRODUCER_COUNT+", consumers="+CONSUMER_COUNT+", deliveryMode="+deliveryMode);
        System.out.println("Produced at messages/sec: "+ (PRODUCE_COUNT*1000.0/(end1-start)));
        System.out.println("Consumed at messages/sec: "+ (CONSUME_COUNT*1000.0/(end2-start)));       
View Full Code Here


    /**
     * @throws Throwable
     */
    public void testConcurrentSendReceive() throws Throwable {

        final Semaphore connectionsEstablished = new Semaphore(1 - (CONSUMER_COUNT + PRODUCER_COUNT));
        final Semaphore workerDone = new Semaphore(1 - (CONSUMER_COUNT + PRODUCER_COUNT));
        final CountDownLatch sampleTimeDone = new CountDownLatch(1);

        final AtomicInteger producedMessages = new AtomicInteger(0);
        final AtomicInteger receivedMessages = new AtomicInteger(0);

        final Callable producer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                Connection connection = factory.createConnection();
                connections.add(connection);
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(destination);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                BytesMessage message = session.createBytesMessage();
                message.writeBytes(new byte[1024]);
                connection.start();
                connectionsEstablished.release();

                while (!sampleTimeDone.await(0, TimeUnit.MILLISECONDS)) {
                    producer.send(message);
                    producedMessages.incrementAndGet();
                }

                connection.close();
                workerDone.release();
                return null;
            }
        };

        final Callable consumer = new Callable() {
            public Object call() throws JMSException, InterruptedException {
                Connection connection = factory.createConnection();
                connections.add(connection);
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageConsumer consumer = session.createConsumer(destination);

                consumer.setMessageListener(new MessageListener() {
                    public void onMessage(Message msg) {
                        receivedMessages.incrementAndGet();
                    }
                });
                connection.start();

                connectionsEstablished.release();
                sampleTimeDone.await();

                connection.close();
                workerDone.release();
                return null;
            }
        };

        final Throwable workerError[] = new Throwable[1];
        for (int i = 0; i < PRODUCER_COUNT; i++) {
            new Thread("Producer:" + i) {
                public void run() {
                    try {
                        producer.call();
                    } catch (Throwable e) {
                        e.printStackTrace();
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        for (int i = 0; i < CONSUMER_COUNT; i++) {
            new Thread("Consumer:" + i) {
                public void run() {
                    try {
                        consumer.call();
                    } catch (Throwable e) {
                        e.printStackTrace();
                        workerError[0] = e;
                    }
                }
            }.start();
        }

        System.out.println(getName() + ": Waiting for Producers and Consumers to startup.");
        connectionsEstablished.acquire();
        System.out.println("Producers and Consumers are now running.  Waiting for system to reach steady state: "
                + (SAMPLE_DELAY / 1000.0f) + " seconds");
        Thread.sleep(1000 * 10);

        System.out.println("Starting sample: "+SAMPLES+" each lasting "+ (SAMPLE_DURATION / 1000.0f) + " seconds");


        long now = System.currentTimeMillis();
        for( int i=0; i < SAMPLES; i ++) {
           
            long start = System.currentTimeMillis();
            producedMessages.set(0);
            receivedMessages.set(0);
           
            Thread.sleep(SAMPLE_DURATION);
           
            long end = System.currentTimeMillis();
            int r = receivedMessages.get();
            int p = producedMessages.get();
           
            System.out.println("published: " + p + " msgs at "+ (p * 1000f / (end - start)) + " msgs/sec, "+
                    "consumed: " + r + " msgs at "+ (r * 1000f / (end - start)) + " msgs/sec");
        }

        System.out.println("Sample done.");
        sampleTimeDone.countDown();

        workerDone.acquire();
        if (workerError[0] != null) {
            throw workerError[0];
        }

    }
View Full Code Here

        this.next = next;
        this.maxSize = maxSize;
        this.minSize = minSize;
        this.blockingTimeoutMilliseconds = blockingTimeoutMilliseconds;
        setIdleTimeoutMinutes(idleTimeoutMinutes);
        permits = new Semaphore(maxSize, true);
    }
View Full Code Here

            resizeLock.writeLock().lock();
            try {
                ResizeInfo resizeInfo = new ResizeInfo(this.minSize, permits.availablePermits(), connectionCount, newMaxSize);
                this.shrinkLater = resizeInfo.getShrinkLater();

                permits = new Semaphore(newMaxSize, true);
                //pre-acquire permits for the existing checked out connections that will not be closed when they are returned.
                for (int i = 0; i < resizeInfo.getTransferCheckedOut(); i++) {
                    permits.acquire();
                }
                //transfer connections we are going to keep
View Full Code Here

        this.next = next;
        this.maxSize = maxSize;
        this.minSize = minSize;
        this.blockingTimeoutMilliseconds = blockingTimeoutMilliseconds;
        setIdleTimeoutMinutes(idleTimeoutMinutes);
        permits = new Semaphore(maxSize, true);
    }
View Full Code Here

            resizeLock.writeLock().lock();
            try {
                ResizeInfo resizeInfo = new ResizeInfo(this.minSize, permits.availablePermits(), connectionCount, newMaxSize);
                this.shrinkLater = resizeInfo.getShrinkLater();

                permits = new Semaphore(newMaxSize, true);
                //pre-acquire permits for the existing checked out connections that will not be closed when they are returned.
                for (int i = 0; i < resizeInfo.getTransferCheckedOut(); i++) {
                    permits.acquire();
                }
                //transfer connections we are going to keep
View Full Code Here

TOP

Related Classes of edu.emory.mathcs.backport.java.util.concurrent.Semaphore$NonfairSync

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.