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

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

Introduction

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

Prototype

@Override
    public void execute(Runnable task) 

Source Link

Usage

From source file:org.ng200.openolympus.Application.java

public static void main(final String[] args) {
    final ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
    final UserRepository userRepository = context.getBean(UserRepository.class);
    final RoleService roleService = context.getBean(RoleService.class);
    if (userRepository.findByUsername("admin") == null) {
        Application.logger.info("Creating administrator account");
        final User admin = new User("admin", new BCryptPasswordEncoder().encode("admin"), "", "", "", "", "",
                "", "", "", "", "", "", "", "", "", "", "", "", null);
        final Set<Role> roles = new HashSet<Role>();
        roles.add(roleService.getRoleByName(Role.USER));
        roles.add(roleService.getRoleByName(Role.SUPERUSER));
        admin.setRoles(roles);//  w  w  w .j  a va 2  s.c  o  m
        userRepository.save(admin);
    }
    final ThreadPoolTaskExecutor taskExecutor = context.getBean(ThreadPoolTaskExecutor.class);
    taskExecutor.execute(context.getBean(TestingService.class));
}

From source file:org.apache.camel.component.xquery.XQueryConcurrencyTest.java

@Test
public void testConcurrency() throws Exception {
    int total = 1000;

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(total);//  w  w  w.  j a va2  s. c  o  m

    // setup a task executor to be able send the messages in parallel
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.afterPropertiesSet();
    for (int i = 0; i < 5; i++) {
        final int threadCount = i;
        executor.execute(new Runnable() {
            public void run() {
                int start = threadCount * 200;
                for (int i = 0; i < 200; i++) {
                    try {
                        // do some random sleep to simulate spread in user activity
                        Thread.sleep(new Random().nextInt(10));
                    } catch (InterruptedException e) {
                        // ignore
                    }
                    template.sendBody(uri,
                            "<person><id>" + (start + i + 1) + "</id><name>James</name></person>");
                }
            }
        });
    }

    mock.assertNoDuplicates(body());

    assertMockEndpointsSatisfied();
    executor.shutdown();
}

From source file:org.apache.camel.component.xquery.XQueryURLBasedConcurrencyTest.java

@Test
public void testConcurrency() throws Exception {
    int total = 1000;

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(total);//from   w  ww  .j a  v  a  2s.co m

    // setup a task executor to be able send the messages in parallel
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.afterPropertiesSet();
    for (int i = 0; i < 5; i++) {
        final int threadCount = i;
        executor.execute(new Runnable() {
            public void run() {
                int start = threadCount * 200;
                for (int i = 0; i < 200; i++) {
                    try {
                        // do some random sleep to simulate spread in user activity
                        Thread.sleep(new Random().nextInt(10));
                    } catch (InterruptedException e) {
                        // ignore
                    }
                    template.sendBody("direct:start",
                            "<mail><subject>" + (start + i) + "</subject><body>Hello world!</body></mail>");
                }
            }
        });
    }

    mock.assertIsSatisfied();
    // must use bodyAs(String.class) to force DOM to be converted to String XML
    // for duplication detection
    mock.assertNoDuplicates(bodyAs(String.class));
    executor.shutdown();
}

From source file:zipkin.autoconfigure.storage.mysql.brave.TraceZipkinMySQLStorageAutoConfiguration.java

@Bean
@ConditionalOnMissingBean(Executor.class)
public Executor executor(ServerSpanState serverState) {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setThreadNamePrefix("MySQLStorage-");
    executor.initialize();/*from w ww.  j a  v a2  s  .  c om*/
    return command -> {
        ServerSpan currentSpan = serverState.getCurrentServerSpan();
        executor.execute(() -> {
            serverState.setCurrentServerSpan(currentSpan);
            command.run();
        });
    };
}

From source file:com.ethlo.geodata.GeodataServiceImpl.java

public void load() {
    ensureBaseDirectory();/*w w w.  j  av a 2 s  .  c o m*/

    final SourceDataInfoSet sourceInfo = geoMetaService.getSourceDataInfo();
    if (sourceInfo.isEmpty()) {
        logger.error(
                "Cannot start geodata server as there is no data. Please run with 'update' parameter to import data");
        System.exit(1);
    }

    loadHierarchy();

    final ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(3);
    taskExecutor.setThreadNamePrefix("data-loading-");
    taskExecutor.initialize();

    taskExecutor.execute(this::loadLocations);
    taskExecutor.execute(this::loadMbr);
    taskExecutor.execute(this::loadIps);

    taskExecutor.setAwaitTerminationSeconds(Integer.MAX_VALUE);
    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
    taskExecutor.shutdown();

    //final ResultSet<GeoLocation> result = geoNamesRepository.retrieve(QueryFactory.equal(CqGeonamesRepository.ATTRIBUTE_FEATURE_CODE, "ADM1"));
    //result.forEach(this::connectAdm1WithCountry);

    publisher.publishEvent(new DataLoadedEvent(this, DataType.ALL, Operation.LOAD, 1, 1));
}