关闭

spring quartz集群

标签: springquartz
153人阅读 评论(0) 收藏 举报
分类:

1.applicationContext.xml

<bean id="custTicketJobDetail" class="org.springframework.scheduling.<span style="color:#ff0000;">quartz.JobDetailBean</span>">
      <property name="jobClass">
         <value><span style="color:#ff0000;">com.555.common.MyDetailQuartzJobBean</span></value>
      </property>
      <property name="jobDataAsMap">
        <map>
            <entry key="targetObject" value="customerTicketService" />
          	<entry key="targetMethod" value="updateCustTicketStatus" />
	   	</map>
	 </property>
	 <property name="durability" value="true" />   
	</bean>

	<bean id="custTicketTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail"><!-- jobDetail是Spring定时器的特殊属性targetObject -->
			<ref bean="custTicketJobDetail" />
		</property>
		<property name="cronExpression">
			<!-- 每天的凌晨10分时触发 -->
			<value>0/5 * * * * ?</value><!-- cronExpression是Spring定时器的特殊属性 -->
		</property>
		<!-- <property name="startDelay">延时1分钟启动定时任务
			<value>60000</value>
		</property> -->
	</bean>

<bean autowire="no"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource">
            <ref bean="DataSource"/>
        </property>
		<property name="applicationContextSchedulerContextKey" value="applicationContext" />  
		<property name="configLocation" value="classpath:quartz.properties" />
		<property name="triggers">    <!-- triggers是Spring定时器的特殊属性 -->
			<list>
				<ref bean="custTicketTrigger" />
			</list>
		</property>
	</bean>

2.MyDetailQuartzJobBean.java     用反射机制动态调用job方法

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

public class MyDetailQuartzJobBean extends QuartzJobBean implements
		ApplicationContextAware {
	protected final Log logger = LogFactory.getLog(getClass());

	private String targetObject;
	private String targetMethod;
	private ApplicationContext ctx;

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		try {

			logger.info("execute [" + targetObject + "] at once>>>>>>");

			WebApplicationContext webContext = ContextLoaderListener
					.getCurrentWebApplicationContext();

			// ApplicationContext ctx =ContextUtil.getContext();
			/*
			 * WebApplicationContext ctx = (WebApplicationContext) context
			 * .getScheduler().getContext().get("applicationContext");
			 */
			Object otargetObject = webContext.getBean(targetObject);
			Method m = null;
			try {
				m = otargetObject.getClass().getMethod(targetMethod,
						new Class[] {});

				m.invoke(otargetObject, new Object[] {});
			} catch (SecurityException e) {
				logger.error(e);
			} catch (NoSuchMethodException e) {
				logger.error(e);
			}

		} catch (Exception e) {
			throw new JobExecutionException(e);
		}

	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.ctx = applicationContext;
	}

	public void setTargetObject(String targetObject) {
		this.targetObject = targetObject;
	}

	public void setTargetMethod(String targetMethod) {
		this.targetMethod = targetMethod;
	}
}


3.quartz.properties

org.quartz.scheduler.instanceName = TestScheduler1   
org.quartz.scheduler.instanceId = AUTO  

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 2
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true  
org.quartz.jobStore.clusterCheckinInterval = 20000


遇到问题1.customerTicketService要实现序列化

2.使用的jar包:quartz-1.6.1.jar,commons-dbcp-1.2.jar,commons-collections-3.2.1.jar,commons-pool-1.6.jar,commons-logging-1.1.3.jar,spring3.x.jar

3.上下文的问题

4.用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean指定类和方法,但是直接使用会报Java.io.NotSerializableException异常

5.添加quartz的12个表   quartz-1.8.6\docs\dbTables  mysql或者oracle表


0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26800次
    • 积分:437
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:22篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论