Quartz在weblogic下初始化两次问题

09-09-11 xysniper

我的quartz在TOMCAT下正常初始化一次,为什么在weblogic下初始化了两次,大家谁有这方面的经验?

日志如下:

090911101853 INFO  util.QuartzLoadServlet-javaee scheduler Initializing...
090911101853 INFO  util.QuartzLoadServlet-javaee scheduler Initializing...
090911101854 INFO  util.QuartzLoadServlet-Schduler Name : JavaEEScheduler
090911101854 INFO  util.QuartzLoadServlet-Schduler Name : JavaEEScheduler
090911101854 INFO  util.QuartzLoadServlet-Schduler ID :NON_CLUSTERED
090911101854 INFO  util.QuartzLoadServlet-Schduler ID :NON_CLUSTERED
090911101854 INFO  util.QuartzLoadServlet-javaee Start Scheduler OK
090911101854 INFO  util.QuartzLoadServlet-javaee Start Scheduler OK

<p>

我的JAVA代码如下:

package com.javaee.util;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import com.javaee.common.Constants;

public class QuartzLoadServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(QuartzLoadServlet.class);

	private static String configPath = null;

	Scheduler sched = null;

	// private ServletContext context;

	public void init(ServletConfig config) throws ServletException {
		try {
			configPath = ConfigManager.getInstance().getConfigPath();
			String schedulerConfigFile = configPath + "/"
					+ Constants.SCHEDULER_CONFIGFILE;
			log.info("get javaee Scheduler config file : "
					+ schedulerConfigFile);

			// initial the scheduler
			SchedulerFactory sf = new org.quartz.impl.StdSchedulerFactory(
					schedulerConfigFile);
			log.info("javaee scheduler Initializing...");

			sched = sf.getScheduler();
			log.info("Schduler Name : " + sched.getSchedulerName());
			log.info("Schduler ID :" + sched.getSchedulerInstanceId());
			sched.start();
			log.info("javaee Start Scheduler OK");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			log.error("javaee initiation error...");
			e.printStackTrace();
		}

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException {

	}

	/**
	 * shutdown
	 */
	public void destroy() {
		try {
			Scheduler defaultsched = StdSchedulerFactory.getDefaultScheduler();
			if (defaultsched != null) {
				defaultsched.shutdown();
			}
			log
					.info("===========Default Quartz Scheduler successful shutdown=======start");
			if (sched != null) {
				sched.shutdown();
			}
			log
					.info("============javaee Scheduler successful shutdown.=======end");
		} catch (Exception e) {
			log.error("Default Quartz Scheduler failed to shutdown cleanly: "
					+ e.toString());
			e.printStackTrace();
		}
	}
}
 
<p>

[该贴被thinkjava于2009-09-11 12:14修改过]

banq
2009-09-14 16:20

init里面启动Quartz一段加锁,确保单线程运行

xysniper
2009-09-21 15:07

怎么加锁?请banq大哥明言!是不是通过一个什么变量来控制初始化一次?

wildfox
2009-10-28 18:13

在listener里面初始化也行。扩展weblogic的 ApplicationLifecycleListener. 不过如果你还要在别的app server里面跑的话就不行。