Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve reference documentation for Task Execution and Scheduling about bean scopes [SPR-14167] #18739

Closed
spring-projects-issues opened this issue Apr 14, 2016 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 14, 2016

Manuel Jordan opened SPR-14167 and commented

I am doing some experiments about Task Execution and Scheduling

In XML I have the following:

<bean id="processServiceImpl"
           scope="prototype"
           class="com.manuel.jordan.service.impl.ProcessServiceImpl" />

<task:scheduler id="scheduler" pool-size="4" />

<task:scheduled-tasks scheduler="scheduler" >
	
                <task:scheduled ref="processServiceImpl" 
					   method="executeRunnable" 
                                           initial-delay=3000"						 
                                           fixed-rate="10000"   
						/>						
                <task:scheduled ref="processServiceImpl" 
                                           method="executeCallable" 
                                           initial-delay="30000"						 
                                           fixed-rate="10000"   
						/>
						
</task:scheduled-tasks>

Until here my processServiceImpl bean is prototype

My code works well how is expected, I can see two instances working well, it about the executeRunnable and executeCallable methods... I can confirm this through the terminal and with visualVM.

Until here, I can assume I can work in peace with a prototype object with scheduling

But in JavaConfig I have a situation:

@Configuration
@EnableScheduling
public class SchedulingConfig implements SchedulingConfigurer {

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.setScheduler(taskScheduler());
	}
	
	@Bean(destroyMethod="shutdown")
    public TaskScheduler taskScheduler() {
		
		ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
		threadPoolTaskScheduler.setPoolSize(4);
		
		return threadPoolTaskScheduler;
    }

}

And with:

@Service
//@Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ProcessServiceImpl implements ProcessService, Runnable, Callable<String> {

	private static final Logger logger = LoggerFactory.getLogger(ProcessServiceImpl.class.getSimpleName());
	
	@Override
	@Scheduled(initialDelay=3000, fixedRate=10000)
	public void executeRunnable() {
		new MessageRunnableWithSleep().run();
	}
...

When I execute the code

public static void main(String[] args) throws Exception {
				
		ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(CentralConfig.class);
		
		MainScheduleSupport.sleep();
		
		applicationContext.close();
		
}

I did realise only works on Singleton, but not Prototype.

(1) I am confused, why prototype did work on XML and not in JavaConfig + Annotations. Did I forget something?
(2) On 33. Task Execution and Scheduling, there is no mention neither about Singleton nor Prototype

From (1) If scheduling is only valid on Singleton, I am assuming that on XML even when the bean is prototype the two <task:scheduled declarations work with one instance...

The point, the explanation should be added in the reference documentation and javadoc to avoid confusions.


Affects: 4.2 GA, 4.2.4

Issue Links:

Referenced from: commits d628420

@spring-projects-issues
Copy link
Collaborator Author

Manuel Jordan commented

Just to confirm

I have re-tested my two projects, based on:

  • XML
  • JavaConfig

All works fine with singleton

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

As of 4.3 RC2, ScheduledAnnotationBeanPostProcessor features a revised registration mechanism capable of tracking individual bean instances, both on creation for beans of any scope and on destruction of each individual bean. In other words, @Scheduled works on any Spring-managed bean now, not just on non-lazy singleton beans anymore.

So I suppose that there aren't any restrictions left to document. If there is nevertheless something you'd like to see improved in the wording or the examples, let me know.

@spring-projects-issues
Copy link
Collaborator Author

Manuel Jordan commented

Hi Juergen

In other words, @Scheduled works on any Spring-managed bean now, not just on non-lazy singleton beans anymore.

If that means now that @Scheduled (from 4.3 RC2) can be applied to prototypes beans. Has sense to left the documentation without changes, but perhaps add a note for XML and annotations sections indicating it has been improved would be nice, it of course because it is a new feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

2 participants