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

Job 模块每次都开启一个线程处理任务的疑惑 #31

Closed
mjd507 opened this issue Nov 22, 2018 · 1 comment
Closed

Job 模块每次都开启一个线程处理任务的疑惑 #31

mjd507 opened this issue Nov 22, 2018 · 1 comment

Comments

@mjd507
Copy link

mjd507 commented Nov 22, 2018

下面代码 copy 自 ScheduleJob,删除了不相干的代码。
这里每次执行 job 的时候,都会单独开个线程,有什么说法吗?

public class ScheduleJob extends QuartzJobBean {
	private ExecutorService service = Executors.newSingleThreadExecutor(); 
	
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //....
        try {
            ScheduleRunnable task = new ScheduleRunnable(scheduleJob.getBeanName(),
            		scheduleJob.getMethodName(), scheduleJob.getParams());
                 Future<?> future = service.submit(task);
		future.get();
			
			// ....
		} catch (Exception e) {
			// ...
		}finally {
			// ...
		}
    }
}

我的理解 quartz 框架在处理这些 job 的时候已经是单独的线程了,没有必要在开一个线程。
伪代码如下

public class ScheduleJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //....
        try {
                 Object target = SpringContextUtils.getBean(context.getBeanName());
                 Method method = context.getBeanName().getClass().getDeclaredMethod(scheduleJob.getMethodName());
                 method.invoke(target);
		} catch (Exception e) {
			// ...
		}finally {
			// ...
		}
    }
}
@sunlightcs
Copy link
Member

确实是多余的,后面会优化这部分代码

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

No branches or pull requests

2 participants