ssm框架整合quartz实现定时任务

ssm框架搭建在此不做说明
新增一个applicationContext-quartz.xml配置文件,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd ">

    <context:component-scan base-package="com.lyt.timer.job" /> 

    <!-- 注入任务处理类bean -->
    <bean id="myJob" class="com.lyt.timer.job.MyJob"></bean>
    <bean id="otherJob" class="com.lyt.timer.job.OtherJob"></bean>

    <!-- 任务触发器详细信息bean -->
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myJob"></property>
        <property name="targetMethod" value="execute"></property>
        <property name="concurrent" value="false" /><!-- 作业不并发调度  -->
    </bean>
    <bean id="otherDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="otherJob"></property>
        <property name="targetMethod" value="execute"></property>
        <property name="concurrent" value="false" /><!-- 作业不并发调度  -->
    </bean>

    <!-- 定义trigger 触发器 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"></property>
        <property name="cronExpression" value="${crond.myJob}"></property>
    </bean>
    <bean id="otherTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="otherDetail"></property>
        <property name="cronExpression" value="${crond.otherJob}"></property>
    </bean>

    <!-- 设置触发器调度工厂 -->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
           <list>
                <ref bean="cronTrigger"/>
                <ref bean="otherTrigger"/>
           </list>
        </property>
    </bean>

</beans>

设置定时时间crontab.properties

<!-- 每天下午三点执行 -->
crond.myJob=0 0 15 * * ? 
crond.otherJob=0 0 15 * * ? 

最后必须要在applicationContext-beans.xml配置中引入上述配置,方式如下

<import resource="classpath:spring/applicationContext-quartz.xml"/>

执行代码(main方式运行就好)

package com.lyt.timer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

public class TimerCore {

    public static void main(String[] args) {
        System.out.println("timer start ...");

        ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-beans.xml");

    }
}

任务类

package com.lyt.timer.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;
@Component("MyJob")
public class MyJob {

    public void execute(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Quartz的任务调度!!!"+format.format(new Date()));

    }

}



package com.lyt.timer.job;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lyt.timer.soa.dao.PostOldDao;

@Component
public class OtherJob{

    @Autowired private PostOldDao topicDao;

    public void execute(){
        System.out.println("这是第二个任务开始");
        List<Long> lst = topicDao.getPostOldLst();
        System.out.println("出来:"+lst.size());
        System.out.println("这是第二个任务结束");
    }

}

以上就是ssm定时任务简单使用

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 可以使用Spring的定时任务实现,具体步骤如下: 1. 在Spring配置文件中添加定时任务配置,例如: <task:scheduled-tasks> <task:scheduled ref="taskService" method="execute" cron=" /5 * * * ?"/> </task:scheduled-tasks> 其中,ref属性指定定时任务实现类,method属性指定执行的方法,cron属性指定定时任务的执行时间。 2. 在定时任务实现类中编写执行逻辑,例如: @Service public class TaskServiceImpl implements TaskService { @Override public void execute() { // 执行定时任务的逻辑 } } 3. 启动Spring容器,定时任务会按照配置的时间自动执行。 注意:定时任务的执行时间可以使用cron表达式来指定,具体语法可以参考相关文档。 ### 回答2: SSM框架是Spring+Spring MVC+MyBatis的组合,可以很方便地实现Java Web开发的各种功能。以下是用SSM框架编写定时任务的示例代码: 1. 在Spring配置文件中添加以下配置: ```xml <!--配置任务调度器--> <task:executor id="taskExecutor" pool-size="5" /> <task:scheduler id="taskScheduler" pool-size="10" /> <!-- 配置定时任务--> <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" /> ``` 2. 创建一个定时任务的类,通过注解方式指定任务执行的时间: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyTask { @Scheduled(cron = "0 0 0 * * ?") //每天凌晨执行 public void doTask() { //执行定时任务的业务逻辑 System.out.println("执行定时任务..."); } } ``` 3. 在Spring配置文件中添加以下配置,以扫描定时任务类: ```xml <!-- 配置任务类的扫描路径--> <context:component-scan base-package="com.example.task" /> ``` 4. 在web.xml中添加配置,启动Spring容器: ```xml <!-- 启动Spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 以上代码实现了一个简单的定时任务,在每天凌晨执行,可以根据自己的需求修改执行时间。同时,定时任务类需要被Spring扫描到,所以需要在Spring配置文件中添加扫描路径。最后,在web.xml中配置启动Spring容器。 ### 回答3: SSM框架(Spring+Spring MVC+MyBatis)是一种常用的Java开发框架,可以很方便地实现定时任务。以下是使用SSM框架编写一个定时任务的简要步骤。 1. 首先,在Spring的配置文件中(如applicationContext.xml)中配置定时任务的相关配置。例如,使用"task"命名空间配置定时任务相关的bean和调度器。 2. 在Spring MVC的配置文件(如spring-mvc.xml)中配置扫描定时任务类的包路径,以便Spring能够找到并管理它们。 3. 创建一个定时任务类,可以命名为TaskJob或类似的名称,并使用@Component或@Service注解将其标记为Spring的托管bean。在该类中,可以定义一个方法作为定时任务的逻辑。 4. 在该方法上使用@Scheduled注解,指定定时任务的触发规则。可以使用cron表达式或固定延迟时间(如@Scheduled(fixedDelay = 5000))来配置任务的执行策略。 5. 在该方法内编写定时任务需要执行的逻辑代码,例如发送邮件、生成报表、数据备份等等。 6. 在需要启动定时任务的地方,注入定时任务类的实例,并调用相应的方法启动定时任务。可以使用@Autowired注解将定时任务类实例化,并在需要启动任务的地方调用对应的方法。 7. 最后,启动项目,定时任务会根据配置的触发规则,在指定的时间间隔内自动执行。 需要注意的是,以上步骤只是基本的实现定时任务框架配置和开发方法,具体的定时任务逻辑需要根据项目需求来编写。同时,也需要考虑定时任务的并发性、异常处理等情况,以确保定时任务的稳定和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值