NutzCN Logo
问答 无法正常停止quartz的线程
发布于 3045天前 作者 qq_411db427 9093 次浏览 复制 上一个帖子 下一个帖子
标签:

JOB是

@IocBean
public class StockJob implements Job{

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		System.out.println("任务正在执行,执行时间: " + Calendar.getInstance().getTime());
	}
	
}

Schedule是

    private void stockSchedule() throws Throwable{
    	sf = new StdSchedulerFactory();  
        // 从工厂里面拿到一个scheduler实例  
        sched = sf.getScheduler();  
        // 计算任务的开始时间,DateBuilder.evenMinuteDate方法是取下一个整数分钟  
//        Date runTime = DateBuilder.evenMinuteDate(new Date());  
        // 真正执行的任务并不是Job接口的实例,而是用反射的方式实例化的一个JobDetail实例  
        job = JobBuilder.newJob(StockJob.class).withIdentity("job1", "group1").build();  
        // 定义一个触发器,startAt方法定义了任务应当开始的时间  
//        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();  
        // 定义触发时间
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")  
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build(); 
        // 将任务和Trigger放入scheduler  
        sched.scheduleJob(job, trigger);  
        sched.start();  
    }

在Nutz SetUp里面

	@Override
	public void destroy(NutConfig config) {
		// TODO Auto-generated method stub
		System.out.println("关闭服务");
////		//定时任务
//		MainSchedule mainSchedule=new MainSchedule();
//		mainSchedule.shutdownSchedule();
		try {
			config.getIoc().get(Scheduler.class).shutdown(true);
		} catch (Exception e) {
		}
	}

用的是tomcat8.0.9,启动服务的时候正常,也能正常运行线程,但是在关闭服务的时候,线程无法关闭
控制台报错
Nutz.Mvc[nutz] is deposing ...
关闭服务
[DEBUG] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:149)
Get 'scheduler'<interface org.quartz.Scheduler>
[DEBUG] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:169)
>> Load definition
[INFO ] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:241)
Closing class org.nutz.ioc.impl.NutIoc@729029038 startup date [Fri Jul 15 10:33:57 CST 2016] [DEBUG] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:91)
Depose object 'dao' ...
[DEBUG] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:91)
Depose object 'tmpFilePool' ...
[DEBUG] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:91)
Depose object 'conf' ...
[DEBUG] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:91)
Depose object 'dataSource' ...
[INFO ] 2016-07-15 10:34:10,388 method:com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1436)
{dataSource-1} closed
[INFO ] 2016-07-15 10:34:10,388 method:org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:245)
class org.nutz.ioc.impl.NutIoc@729029038 is deposed.
[INFO ] 2016-07-15 10:34:10,388 method:org.nutz.mvc.impl.NutLoading.depose(NutLoading.java:434)
Nutz.Mvc[nutz] is down in 0ms
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
警告: The web application [/KMIA_ABCM] registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
警告: The web application [/KMIA_ABCM] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-3] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-5] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-6] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-7] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-8] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-9] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_Worker-10] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
七月 15, 2016 10:34:10 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
警告: The web application [/KMIA_ABCM] appears to have started a thread named [DefaultQuartzScheduler_QuartzSchedulerThread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:311)
七月 15, 2016 10:34:10 上午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["http-nio-8088"]
七月 15, 2016 10:34:10 上午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["ajp-nio-8009"]
七月 15, 2016 10:34:10 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["http-nio-8088"]
七月 15, 2016 10:34:10 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["ajp-nio-8009"]

请问是为什么会有11个线程?然后为什么会关不掉会有内存溢出?
谢谢了

7 回复

你自己new的啊, 自行关掉啊

private void stockSchedule() throws Throwable{
    	sf = new StdSchedulerFactory();  
        // 从工厂里面拿到一个scheduler实例  
        sched = sf.getScheduler();  
		//.................
		
// 在MainSetup里面

public void destroy(NutConfig config) {
        sched.shutdown(true); // 这里的sched指向stockSchedule方法那个sched
}

@wendal 我在我的MainSchedule类里面有一个

    public void shutdownSchedule() {
        if (null != sched) {  
            try {  
                sched.shutdown();  
                try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            } catch (SchedulerException e) {  
                e.printStackTrace();  
            }  
        }  
    }

这样的方法,我在destroy()里面直接调用这个方法也不行

	@Override
	public void destroy(NutConfig config) {
		// TODO Auto-generated method stub
		System.out.println("关闭服务");
////		//定时任务
		MainSchedule mainSchedule=new MainSchedule();
		mainSchedule.shutdownSchedule();
		try {
//			config.getIoc().get(Scheduler.class).shutdown(true);
		} catch (Exception e) {
		}
	}

@wendal 依然是无法关闭线程

我晕啊, new个新的然后关,那老的关吗??!!!!

		MainSchedule mainSchedule=new MainSchedule();
		mainSchedule.shutdownSchedule();

起码改成单例

public class MainSchedule {
      public static MainSchedule me; // 放在一个静态变量里面
	  public MainSchedule() {
	       me = this; // 或者用其他单例方式也行.
	  }
	  // 其他方法
}

// 然后在MainSetup里面取这个实例来调用
@Override
	public void destroy(NutConfig config) {
		MainSchedule.me.shutdownSchedule();
	}

@wendal 哦哦哦。我懂了。。谢谢大神。。我new了一个新的来管。。老的还在继续运行。。谢谢大神。。

@wendal 大神,我还有一个问题,我在job里面的execute方法里面使用dao,然后用您之前教我的

	@Inject
	protected Dao dao;
	public Dao dao() {
	    if (dao == null)
		    dao = Mvcs.getIoc().get(Dao.class);
	    return dao;
	}

程序里面用this.dao()来调用
但是在运行的时候还是会报空指针异常,请问这个需要怎么解决呢?

 dao = Mvcs.ctx().getDefaultIoc().get(Dao.class);

添加回复
请先登陆
回到顶部