Example usage for org.springframework.scheduling.concurrent ThreadPoolTaskExecutor getActiveCount

List of usage examples for org.springframework.scheduling.concurrent ThreadPoolTaskExecutor getActiveCount

Introduction

In this page you can find the example usage for org.springframework.scheduling.concurrent ThreadPoolTaskExecutor getActiveCount.

Prototype

public int getActiveCount() 

Source Link

Document

Return the number of currently active threads.

Usage

From source file:com.github.liyp.rabbitmq.demo.Main.java

public static void main(String[] args) throws IOException, InterruptedException {
    // start spring context
    @SuppressWarnings({ "resource" })
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "com/github/liyp/rabbitmq/demo/applicationContext.xml");

    RabbitTemplate rabbitTemplate = (RabbitTemplate) context.getBean("rabbitTemplate");
    for (int i = 0; i < 200; i++) {
        rabbitTemplate.convertAndSend("queue_one", "test queue 1 " + i);
        rabbitTemplate.convertAndSend("queue_two", new MsgBean("test queue 2 " + i));
    }//from   w  w  w .j a v  a 2 s  .  c om
    Thread.sleep(5000);
    ThreadPoolTaskExecutor threadExe = (ThreadPoolTaskExecutor) context.getBean("taskExecutor");
    System.out.println(threadExe.getActiveCount());
    System.out.println(threadExe.getCorePoolSize());
    System.out.println(threadExe.getMaxPoolSize());
    System.out.println(threadExe.getPoolSize());
    System.out.println(threadExe.getThreadPoolExecutor().getCorePoolSize());

    // System.out.println("rsv: " +
    // rabbitTemplate.receiveAndConvert("queue_two"));
}

From source file:com.googlecode.msidor.springframework.integration.system.ShutdownHandler.java

/**
 * Handles application shutdown//from   w w  w  .  j a va2 s  .c o m
 */
public void shutdownGently() {

    long startTimestamp = System.currentTimeMillis();

    log.info("Shuting down the application...");

    if (componentsToShutDown != null) {

        Assert.notNull(operationChannel, "Operation channel must be set");

        for (String component : componentsToShutDown) {
            log.info("Sending shutdown command to component: ".concat(component));

            Message<String> operation = MessageBuilder.withPayload("@" + component + ".stop()").build();
            operationChannel.send(operation);

        }
    }

    if (executorsToWatch != null) {
        log.info("Checking if all executor threads have been accomplished...");

        boolean allDone = true;
        do {
            allDone = true;
            for (ThreadPoolTaskExecutor executor : executorsToWatch) {
                allDone = allDone && executor.getActiveCount() == 0;
            }
        } while (!allDone && !Thread.interrupted()
                && ((timeout > 0 && System.currentTimeMillis() - startTimestamp < timeout) || timeout <= 0));

        if (allDone)
            log.info("No more active threads");
        else
            log.warn("Some threads are still working");
    }

    okToShutdown = true;

    if (shutodwnApplication) {
        System.exit(systemExitCode);
    }

}