Java org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler 代码实例

・11 分钟阅读

以下是展示如何使用org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * The default TaskScheduler to use if none is configured via
 * {@link SockJsServiceRegistration#setTaskScheduler(org.springframework.scheduling.TaskScheduler)}
 * , i.e.
 *
 * <pre class="code">
 * &#064;Configuration
 * public class WampConfig extends DefaultWampConfiguration {
 *
 *      public void registerWampEndpoints(WampEndpointRegistry registry) {
 *              registry.addEndpoint(&quot;/wamp&quot;).withSockJS().setTaskScheduler(myScheduler());
 *      }
 *
 *      // ...
 * }
 * </pre>
 */
@Bean
public ThreadPoolTaskScheduler messageBrokerSockJsTaskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("WampSockJS-");
        scheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
        scheduler.setRemoveOnCancelPolicy(true);
        return scheduler;
}
 

实例 2


public RabbitTestBinder(ConnectionFactory connectionFactory) {
        RabbitMessageChannelBinder binder = new RabbitMessageChannelBinder(connectionFactory);
        GenericApplicationContext context = new GenericApplicationContext();
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(1);
        scheduler.afterPropertiesSet();
        context.getBeanFactory().registerSingleton(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, scheduler);
        context.refresh();
        binder.setApplicationContext(context);
        binder.setCodec(new PojoCodec());
        this.setBinder(binder);
        this.rabbitAdmin = new RabbitAdmin(connectionFactory);
}
 

实例 3


@Bean(name = "paas-monitor-scheduler")
public Executor getPaaSScheduler() {
    ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(paasMonitorThreadPoolSize);
    threadPoolTaskScheduler.setThreadNamePrefix("paas-monitor-");
    return threadPoolTaskScheduler;
}
 

实例 4


public static ThreadPoolTaskScheduler createThreadPoolScheduler(int poolSz,
        String threadNamePrefix)
{
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(poolSz);
    if (threadNamePrefix != null)
        scheduler.setThreadNamePrefix(threadNamePrefix);
    return scheduler;
}
 

实例 5


public RabbitTestMessageBus(ConnectionFactory connectionFactory, Codec codec) {
        RabbitMessageBus messageBus = new RabbitMessageBus(connectionFactory, codec);
        GenericApplicationContext context = new GenericApplicationContext();
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(1);
        scheduler.afterPropertiesSet();
        context.getBeanFactory().registerSingleton(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, scheduler);
        context.refresh();
        messageBus.setApplicationContext(context);
        this.setMessageBus(messageBus);
        this.rabbitAdmin = new RabbitAdmin(connectionFactory);
}
 

实例 6


@Bean(
    destroyMethod = "shutdown" )
public ThreadPoolTaskScheduler rhizomeScheduler() {
    ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
    executor.setPoolSize( 8 );
    executor.setThreadNamePrefix( "rhizome-pulse-" );
    executor.initialize();
    return executor;
}
 

实例 7


private ScheduledTaskRegistrar getScheduledTaskRegistrar() {
    ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) getServletContext()
            .getAttribute("ScheduledTaskRegistrar");
    if (registrar == null) {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.setDaemon(true);
        executor.setPoolSize(10);
        executor.setThreadNamePrefix(getServletContext()
                .getServletContextName() + "-Thread-");
        executor.afterPropertiesSet();
        getServletContext().setAttribute("ThreadPoolTaskScheduler",
                executor);
        registrar = new ScheduledTaskRegistrar();
        registrar.setTaskScheduler(executor);
        getServletContext().setAttribute("ScheduledTaskRegistrar",
                registrar);
    }
    return registrar;
}
 

实例 8


@Before
public void setup() {
        this.connectionFactory = new CachingConnectionFactory();
        this.connectionFactory.setHost("localhost");
        this.connectionFactory.setPort(BrokerTestUtils.getPort());
        scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(1);
        scheduler.initialize();
}
 

实例 9


@PostConstruct
public void start() {
        Validate.notBlank(cronExpression);
        threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setThreadNamePrefix("SpringCronJob");
        threadPoolTaskScheduler.initialize();
        threadPoolTaskScheduler.schedule(this, new CronTrigger(cronExpression));
}
 

实例 10


@PostConstruct
public void start() {
        Assert.hasText(cronExpression);
        threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setThreadNamePrefix("SpringCronJob");
        threadPoolTaskScheduler.initialize();
        threadPoolTaskScheduler.schedule(this, new CronTrigger(cronExpression));
}
 

实例 11


@PostConstruct
public void start() {
        Validate.notBlank(cronExpression);
        threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setThreadNamePrefix("SpringCronJob");
        threadPoolTaskScheduler.initialize();
        threadPoolTaskScheduler.schedule(this, new CronTrigger(cronExpression));
        logger.info("[??????????]");
}
 

实例 12


@Test
public void test() throws InterruptedException {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.initialize();
        NativeSessionCloser closer = mock(NativeSessionCloser.class);
        PingWatchdogManager manager = new PingWatchdogManager(executor, closer);
        manager.setPingWatchdog(true);
        for (int i = 0; i < 10; i++) {
                manager.pingReceived("TransportID", 100);
                Thread.sleep(100);
        }
        Thread.sleep(500);
        verify(closer).closeSession("TransportID");
}
 

实例 13


public void afterPropertiesSet() throws Exception {
        try {
                pollInterval = Long.parseLong(pollIntervalString);
        }catch(NumberFormatException npe){
                throw new RuntimeException("Can not parse ENF{eu.cloud4soa.governance.slaenforcement.poll.interval.mseconds} value{"+pollIntervalString+"}. Is a number?");
        }
        scheduler = new ThreadPoolTaskScheduler();
        scheduler.initialize();
        logger.debug("SLAEnforcementWorker registered, cron{ " + cron + " }.");
}
 

实例 14


@Bean(destroyMethod = "shutdown")
public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setErrorHandler(new ErrorHandler() {
                @Override
                public void handleError(Throwable t) {
                        LogManager.getLogger().error("error while handling job", t);
                }
        });
        scheduler.initialize();
        return scheduler;
}
 

实例 15


public RedisTestBinder(RedisConnectionFactory connectionFactory) {
        RedisMessageChannelBinder binder = new RedisMessageChannelBinder(connectionFactory);
        GenericApplicationContext context = new GenericApplicationContext();
        context.getBeanFactory().registerSingleton(IntegrationUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME,
                        new DefaultMessageBuilderFactory());
        DefaultHeaderChannelRegistry channelRegistry = new DefaultHeaderChannelRegistry();
        channelRegistry.setReaperDelay(Long.MAX_VALUE);
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.afterPropertiesSet();
        channelRegistry.setTaskScheduler(taskScheduler);
        context.getBeanFactory().registerSingleton(
                        IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME,
                        channelRegistry);
        context.refresh();
        binder.setApplicationContext(context);
        binder.setCodec(new PojoCodec());
        setBinder(binder);
        template = new StringRedisTemplate(connectionFactory);
}
 
讨论
淘淘あ西西 profile image