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

Let @Scheduled work with a non-void method how is possible with <task:scheduled> [SPR-14175] #18746

Closed
spring-issuemaster opened this issue Apr 14, 2016 · 6 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Apr 14, 2016

Manuel Jordan opened SPR-14175 and commented

I am doing some experiments with Task Execution and Scheduling

I have two projects with this code:

public class ProcessServiceImpl implements ProcessService, Runnable, Callable<String> {

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

	@Override
	public String executeCallable() {
		String result = "";
		try {
			result = new MessageCallableWithSleep().call(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
...

With XML I have:

	<task:scheduler id="scheduler" pool-size="#{T(com.manuel.jordan.support.Constants).FIXED_THREAD_POOL}" />

	<task:scheduled-tasks scheduler="scheduler" >
	
		<task:scheduled ref="processServiceImpl" 
						method="executeRunnable" 
						initial-delay="#{T(com.manuel.jordan.support.Constants).INITIAL_DELAY}"						 
						fixed-rate="#{T(com.manuel.jordan.support.Constants).PERIOD}"   
						/>						
		<task:scheduled ref="processServiceImpl" 
						method="executeCallable" 
						initial-delay="#{T(com.manuel.jordan.support.Constants).INITIAL_DELAY}"						 
						fixed-rate="#{T(com.manuel.jordan.support.Constants).PERIOD}"   
						/>
						
	</task:scheduled-tasks>

The code works fine. Until here I can confirm <task:scheduled> works for a method with a return type, therefore I can use a method where it returns something with schedule in peace.

But if in the other project I use:

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

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

	@Override
	@Scheduled(initialDelay=Constants.INITIAL_DELAY, fixedRate=Constants.PERIOD)
	public String executeCallable() {
		String result = "";
		try {
			result = new MessageCallableWithSleep().call(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

When I execute the code I get:

Initialization of bean failed; nested exception is java.lang.IllegalStateException:
Encountered invalid @Scheduled method 'executeCallable':
Only void-returning methods may be annotated with @Scheduled

Therefore is not possible do a complete migration from XML to annotations.

The reason about have a scheduled method with a return value would be give the option to call the method manually (if need it) or leave in peace working within the scheduled process.

Let's consider the scenario about the sendNotificationEmail method with a Boolean how a return type. I am able to call the method manually and get the boolean value how a successful confirmation or put a schedule to work around that method.

Thanks


Affects: 4.2 GA, 4.2.5, 4.3 RC1

Issue Links:

  • #18768 Add note constrain/restriction for task:scheduled about method signature such as @Scheduled
@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 14, 2016

Juergen Hoeller commented

Good point! Relaxed as of 4.3 RC2 now, not rejecting non-void methods anymore but rather simply ignoring the return value.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 15, 2016

Manuel Jordan commented

Great!, good to know it has been enhanced... thanks so much!

BTW: about your comment, should be: not rejecting non-void methods anymore?
We are talking about non-void methods...

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 15, 2016

Juergen Hoeller commented

Indeed, comment fixed :-)

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Apr 15, 2016

Manuel Jordan commented

Thanks again. I am going to test this new feature soon as 4.3 RC2 be available. I only can see until 4.3 RC1

Thanks!

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Feb 6, 2017

Lennie Kid commented

Is this by any chance going to be available in any earlier version of Spring? Or asking the question in a different way, when will 4.3 RC2 be available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.