欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSM秒杀系统(十二)异步订单超时处理

程序员文章站 2022-04-22 08:18:31
...

对于用户提交订单后,没有进行支付,我们需要设置一个超时处理方法,比如:超过30分钟,订单自动取消。

采用定时任务来实现

实现方式:我们采用定时任务实现,每秒钟执行一次,判断是否超时,超时则将订单自动取消,也就是将订单的状态设置为2

1.超时任务controller层

@Controller
public class CheckordrertimeoutTask {

	@Autowired
	MsoderService msoderService;
	
	public void Checktimeoutorder(int miniutes) {
		List<Msorder> orderlist = msoderService.listorder();
		for(Msorder msorder:orderlist) {
			Date createtime = msorder.getCreatetime();
			Date paytime = msorder.getPaytime();
			if(paytime!=null) {
				continue;
			}
			long timedistance = System.currentTimeMillis()-createtime.getTime();
			long miniute = timedistance/60*1000;
			if(miniute>miniutes) {
				//订单取消,状态设置为2
				msoderService.updateflagbyid(2, msorder.getId());
			}
			System.out.println("Checktimeoutorder");
		}
	}
}

2.设置定时任务

2.1 执行定时任务的方法

  • 通过Autowired自动引入超时任务类CheckordrertimeoutTask
  • 调用超时任务方法clusterQuartz.Checktimeoutorder(20);
public class PrintCurrentTimeJobs2 extends QuartzJobBean
{
    private static final Log LOG_RECORD = LogFactory.getLog(PrintCurrentTimeJobs2.class);

    @Autowired
    private CheckordrertimeoutTask clusterQuartz;


    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException
    {
        LOG_RECORD.info("begin to execute task,clusterQuartz.Checktimeoutorder(20)" + DateUtils.dateToString(new Date()));

        clusterQuartz.Checktimeoutorder(20);

        LOG_RECORD.info("end to execute task,clusterQuartz.Checktimeoutorder(20)" + DateUtils.dateToString(new Date()));

    }
}

2.2 配置超时任务

  • 配置超时任务的数据源
  • 配置job详情,表明执行超时任务的类
  • 配置超时任务的触发时间
  • 配置任务调度:包括数据源、配置job、触发时间
   <!-- 订单超时任务 -->
	<!-- 配置数据源 -->
    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/test3"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
    </bean>

    <!-- 配置Job详情 -->
    <bean name="CheckordrertimeoutTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.youfan.task.PrintCurrentTimeJobs2"/>
        <!--<property name="jobDataAsMap">
            <map>
                <entry key="clusterQuartz" value="com.aaron.framework.clusterquartz.job.ClusterQuartz"/>
            </map>
        </property>-->
        <property name="durability" value="true"/>
        <property name="requestsRecovery" value="false"/>
    </bean>
   
       <!-- 配置触发时间 -->
    <bean name="printCurrentTimeScheduler1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="printCurrentTimeJobs"/>
        <property name="cronExpression">
            <value>0/1 * * * * ?</value>
        </property>
        <property name="timeZone">
            <value>GMT+8:00</value>
        </property>
    </bean>
    
    <!-- 配置调度任务-->
    <bean name="quartzScheduler1" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="configLocation" value="classpath:quartz.properties"/>
        <property name="dataSource" ref="dataSource2"/>
        <property name="transactionManager" ref="transactionManager"/>

        <!-- 任务唯一的名称,将会持久化到数据库-->
        <property name="schedulerName1" value="baseScheduler1"/>

        <!-- 每台集群机器部署应用的时候会更新触发器-->
        <property name="overwriteExistingJobs" value="true"/>
        <property name="applicationContextSchedulerContextKey" value="appli55"/>

        <property name="jobFactory">
            <bean class="com.youfan.task.AutowiringSpringBeanJobFactory"/>
        </property>

        <property name="triggers">
            <list>
                <ref bean="printCurrentTimeScheduler1"/>
            </list>
        </property>
        <property name="jobDetails">
            <list>
                <ref bean="CheckordrertimeoutTask"/>
            </list>
        </property>

        <property name="taskExecutor" ref="executor"/>

    </bean>
相关标签: SSM秒杀系统