Quartz-Spring

 

1.applicationContext-quartz.xml
<?xml version="1.0" encoding="GBK"?>
<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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
	default-lazy-init="false">

	<bean id="jstInterface" class="com.picc.flowctrl.quartz.JstInterface"/>
	<bean id="jstInterfaceJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="jstInterface"/>
		</property>
		<property name="targetMethod">
			<value>execute</value>
		</property>
	</bean>
	<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jstInterfaceJobMethod"></property>
		<property name="cronExpression" value="0 0/10 * * * ?"></property>
	</bean>
	<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="CronTriggerBean"/>
			</list>
		</property>
	</bean>
</beans>
2.JstInterface.java
import java.text.SimpleDateFormat;
import java.util.Date;

public class JstInterface {
	public void execute(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		System.out.println("定时任务,当前时间:"+df.format(new Date()));// new Date()为获取当前系统时间
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Quartz Starter是一个用于在Spring Boot应用程序中集成Quartz调度框架的库。Quartz是一个功能强大的开源任务调度框架,可以用于在Java应用程序中执行定时任务、计划任务等。 使用Spring Boot Quartz Starter,你可以很方便地将Quartz集成到你的Spring Boot应用程序中。它提供了一些自动配置和便利的功能,让你能够更快地开始使用Quartz。 要使用Spring Boot Quartz Starter,你需要在你的项目中添加相应的依赖。在Maven项目中,你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 添加了依赖之后,你可以使用Spring Boot提供的注解和配置来定义和管理Quartz任务。通过使用`@EnableScheduling`注解,你可以启用Spring的任务调度功能。然后,你可以使用`@Scheduled`注解来定义定时任务的执行规则。 下面是一个简单的示例,演示了如何使用Spring Boot Quartz Starter创建一个定时任务: ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @EnableScheduling public class MyScheduler { @Scheduled(cron = "0 0/5 * * * ?") // 每5分钟执行一次 public void myTask() { // 定时任务的业务逻辑 System.out.println("定时任务执行了!"); } } ``` 在这个示例中,我们创建了一个名为`MyScheduler`的组件,并使用`@EnableScheduling`注解启用了Spring的任务调度功能。然后,我们使用`@Scheduled`注解定义了一个定时任务`myTask()`,它将每5分钟执行一次。 这只是一个简单的示例,你可以根据自己的需求来定义更复杂的定时任务。Spring Boot Quartz Starter提供了更多的功能和配置选项,可以帮助你更好地管理和调度任务。 希望能对你有所帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值