Java Code Examples for org.springframework.scheduling.concurrent.ConcurrentTaskScheduler

The following examples show how to use org.springframework.scheduling.concurrent.ConcurrentTaskScheduler. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-boot-websocket-client   Source File: ServiceClient.java    License: MIT License 6 votes vote down vote up
public static void main(String... argv) {
    WebSocketClient webSocketClient = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

    String url = "ws://127.0.0.1:8080/hello";
    StompSessionHandler sessionHandler = new MySessionHandler();
    stompClient.connect(url, sessionHandler);

    new Scanner(System.in).nextLine(); //Don't close immediately.
}
 
Example 2
@Override
public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry registry) throws BeansException {
    ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory;
    if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory, TaskScheduler.class).length == 0) {
        String[] scheduledExecutorsBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory, ScheduledExecutorService.class);
        if (scheduledExecutorsBeanNames.length != 1) {
            logger.debug("Registering default TaskScheduler");
            registry.registerBeanDefinition(DEFAULT_TASK_SCHEDULER_BEAN_NAME, rootBeanDefinition(ConcurrentTaskScheduler.class).getBeanDefinition());
            if (scheduledExecutorsBeanNames.length != 0) {
                logger.warn("Multiple ScheduledExecutorService found, do not know which one to use.");
            }
        } else {
            logger.debug("Registering default TaskScheduler with existing ScheduledExecutorService {}", scheduledExecutorsBeanNames[0]);
            registry.registerBeanDefinition(DEFAULT_TASK_SCHEDULER_BEAN_NAME,
                rootBeanDefinition(ConcurrentTaskScheduler.class)
                    .addPropertyReference("scheduledExecutor", scheduledExecutorsBeanNames[0])
                    .getBeanDefinition()
            );
        }
    }
}
 
Example 3
Source Project: booties   Source File: CustomSchedulingConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    if (properties.isEnabled()) {
        TaskScheduler taskScheduler = null;
        try {
            taskScheduler = this.beanFactory.getBean(TaskScheduler.class);
        } catch (NoUniqueBeanDefinitionException e) {
            taskScheduler = this.beanFactory.getBean("taskScheduler", TaskScheduler.class);
        } catch (NoSuchBeanDefinitionException ex) {
            log.warn("'useExistingScheduler' was configured to 'true', but we did not find any bean.");
        }
        if (taskScheduler != null) {
            log.info("register existing TaskScheduler");
            taskRegistrar.setScheduler(taskScheduler);
        } else {
            throw new BeanCreationException("Expecting a 'ConcurrentTaskScheduler' injected, but was 'null'");
        }
    } else {
        log.info("'CustomSchedulingConfiguration' is disabled, create a default - 'ConcurrentTaskScheduler'");
        this.localExecutor = Executors.newSingleThreadScheduledExecutor();
        taskRegistrar.setScheduler(new ConcurrentTaskScheduler(localExecutor));
    }
}
 
Example 4
@BeforeEach
void setUp() {
    this.meterRegistry = new SimpleMeterRegistry();
    this.executorService = Executors.newCachedThreadPool();
    final ScriptManager scriptManager = new ScriptManager(
        new ScriptManagerProperties(),
        new ConcurrentTaskScheduler(),
        this.executorService,
        new ScriptEngineManager(),
        new DefaultResourceLoader(),
        this.meterRegistry
    );
    this.scriptProperties = new CommandSelectorManagedScriptProperties();
    this.commandSelectorManagedScript = new CommandSelectorManagedScript(
        scriptManager,
        this.scriptProperties,
        this.meterRegistry
    );
}
 
Example 5
@BeforeEach
void setUp() {
    final MeterRegistry meterRegistry = new SimpleMeterRegistry();
    final ScriptManagerProperties scriptManagerProperties = new ScriptManagerProperties();
    final TaskScheduler taskScheduler = new ConcurrentTaskScheduler();
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    final ResourceLoader resourceLoader = new DefaultResourceLoader();
    final ObjectMapper objectMapper = GenieObjectMapper.getMapper();
    final ScriptManager scriptManager = new ScriptManager(
        scriptManagerProperties,
        taskScheduler,
        executorService,
        scriptEngineManager,
        resourceLoader,
        meterRegistry
    );
    this.scriptProperties = new ExecutionModeFilterScriptProperties();
    this.executionModeFilterScript = new ExecutionModeFilterScript(
        scriptManager,
        scriptProperties,
        objectMapper,
        meterRegistry
    );
}
 
Example 6
Source Project: genie   Source File: ManagedScriptIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeEach
void setUp() {
    final MeterRegistry meterRegistry = new SimpleMeterRegistry();
    final ScriptManagerProperties scriptManagerProperties = new ScriptManagerProperties();
    final TaskScheduler taskScheduler = new ConcurrentTaskScheduler();
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    final ResourceLoader resourceLoader = new DefaultResourceLoader();
    this.objectMapper = GenieObjectMapper.getMapper();
    final ScriptManager scriptManager = new ScriptManager(
        scriptManagerProperties,
        taskScheduler,
        executorService,
        scriptEngineManager,
        resourceLoader,
        meterRegistry
    );
    this.scriptProperties = new TestScriptProperties();
    this.script = new TestScript(
        scriptManager,
        this.scriptProperties,
        meterRegistry
    );
}
 
Example 7
@BeforeEach
void setUp() {
    final MeterRegistry meterRegistry = new SimpleMeterRegistry();
    final ScriptManagerProperties scriptManagerProperties = new ScriptManagerProperties();
    final TaskScheduler taskScheduler = new ConcurrentTaskScheduler();
    final ExecutorService executorService = Executors.newCachedThreadPool();
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    final ResourceLoader resourceLoader = new DefaultResourceLoader();
    final ScriptManager scriptManager = new ScriptManager(
        scriptManagerProperties,
        taskScheduler,
        executorService,
        scriptEngineManager,
        resourceLoader,
        meterRegistry
    );
    this.scriptProperties = new ClusterSelectorScriptProperties();
    this.clusterSelectorScript = new ClusterSelectorManagedScript(
        scriptManager,
        this.scriptProperties,
        meterRegistry
    );
}
 
Example 8
Source Project: spring-analysis-note   Source File: ScheduledTaskRegistrar.java    License: MIT License 5 votes vote down vote up
/**
 * Set the {@link TaskScheduler} to register scheduled tasks with, or a
 * {@link java.util.concurrent.ScheduledExecutorService} to be wrapped as a
 * {@code TaskScheduler}.
 */
public void setScheduler(@Nullable Object scheduler) {
	if (scheduler == null) {
		this.taskScheduler = null;
	}
	else if (scheduler instanceof TaskScheduler) {
		this.taskScheduler = (TaskScheduler) scheduler;
	}
	else if (scheduler instanceof ScheduledExecutorService) {
		this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
	}
	else {
		throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
	}
}
 
Example 9
Source Project: java-technology-stack   Source File: ScheduledTaskRegistrar.java    License: MIT License 5 votes vote down vote up
/**
 * Set the {@link TaskScheduler} to register scheduled tasks with, or a
 * {@link java.util.concurrent.ScheduledExecutorService} to be wrapped as a
 * {@code TaskScheduler}.
 */
public void setScheduler(@Nullable Object scheduler) {
	if (scheduler == null) {
		this.taskScheduler = null;
	}
	else if (scheduler instanceof TaskScheduler) {
		this.taskScheduler = (TaskScheduler) scheduler;
	}
	else if (scheduler instanceof ScheduledExecutorService) {
		this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
	}
	else {
		throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
	}
}
 
Example 10
Source Project: lams   Source File: ScheduledTaskRegistrar.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the {@link TaskScheduler} to register scheduled tasks with, or a
 * {@link java.util.concurrent.ScheduledExecutorService} to be wrapped as a
 * {@code TaskScheduler}.
 */
public void setScheduler(Object scheduler) {
	Assert.notNull(scheduler, "Scheduler object must not be null");
	if (scheduler instanceof TaskScheduler) {
		this.taskScheduler = (TaskScheduler) scheduler;
	}
	else if (scheduler instanceof ScheduledExecutorService) {
		this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
	}
	else {
		throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
	}
}
 
Example 11
/**
 * Set the {@link TaskScheduler} to register scheduled tasks with, or a
 * {@link java.util.concurrent.ScheduledExecutorService} to be wrapped as a
 * {@code TaskScheduler}.
 */
public void setScheduler(Object scheduler) {
	Assert.notNull(scheduler, "Scheduler object must not be null");
	if (scheduler instanceof TaskScheduler) {
		this.taskScheduler = (TaskScheduler) scheduler;
	}
	else if (scheduler instanceof ScheduledExecutorService) {
		this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));
	}
	else {
		throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());
	}
}
 
Example 12
Source Project: booties   Source File: CustomSchedulingConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TaskScheduler taskScheduler() {
    if (taskExecutor != null) {
        log.debug("create task-scheduler with pre-configured executor ...");
        return new ConcurrentTaskScheduler(taskExecutor, scheduledExecutorService());
    } else {
        log.debug("create task-scheduler without pre-configured executor ...");
        return new ConcurrentTaskScheduler(scheduledExecutorService());
    }
}
 
Example 13
Source Project: bugsnag-java   Source File: ScheduledTaskConfigurationTest.java    License: MIT License 5 votes vote down vote up
@Test
public void findExecutorByType() throws NoSuchFieldException, IllegalAccessException {
    ScheduledExecutorService expected = Executors.newScheduledThreadPool(1);
    when(context.getBean(ScheduledExecutorService.class)).thenReturn(expected);

    configuration.configureTasks(registrar);
    TaskScheduler scheduler = registrar.getScheduler();
    assertTrue(scheduler instanceof ConcurrentTaskScheduler);
    assertEquals(expected, accessField(scheduler, "scheduledExecutor"));
}
 
Example 14
Source Project: bugsnag-java   Source File: ScheduledTaskConfigurationTest.java    License: MIT License 5 votes vote down vote up
@Test
public void findExecutorByName() throws NoSuchFieldException, IllegalAccessException {
    ScheduledExecutorService expected = Executors.newScheduledThreadPool(4);
    Throwable exc = new NoUniqueBeanDefinitionException(ScheduledExecutorService.class);
    when(context.getBean(ScheduledExecutorService.class)).thenThrow(exc);
    when(context.getBean("taskScheduler", ScheduledExecutorService.class))
            .thenReturn(expected);

    configuration.configureTasks(registrar);
    TaskScheduler scheduler = registrar.getScheduler();
    assertTrue(scheduler instanceof ConcurrentTaskScheduler);
    assertEquals(expected, accessField(scheduler, "scheduledExecutor"));
}
 
Example 15
Source Project: data-highway   Source File: WebSocketConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public TaskScheduler taskScheduler() {
    return new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor());
}
 
Example 16
@Bean
public TaskScheduler taskScheduler() {
    return new ConcurrentTaskScheduler();
}
 
Example 17
Source Project: halyard   Source File: ResourceConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
TaskScheduler taskScheduler() {
  return new ConcurrentTaskScheduler(Executors.newSingleThreadScheduledExecutor());
}
 
Example 18
Source Project: hawkbit-examples   Source File: DeviceSimulator.java    License: Eclipse Public License 1.0 4 votes vote down vote up
@Bean
TaskScheduler taskScheduler() {
    return new ConcurrentTaskScheduler(threadPool());
}
 
Example 19
Source Project: hawkbit   Source File: ExecutorAutoConfiguration.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @return {@link TaskScheduler} for task execution
 */
@Bean
@ConditionalOnMissingBean
public TaskScheduler taskScheduler() {
    return new ConcurrentTaskScheduler(scheduledExecutorService());
}
 
Example 20
public LightminClientApplicationRegistrationService(final LightminClientRegistratorService lightminClientRegistrator,
                                                    final ScheduledExecutorService scheduler) {
    this(lightminClientRegistrator, new ConcurrentTaskScheduler(scheduler));
}
 
Example 21
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
	taskRegistrar.setTaskScheduler(new ConcurrentTaskScheduler(
			Executors.newSingleThreadScheduledExecutor()));
}