SSM框架 quartz问题

来源:15-3 定时统计店铺的商品日销量

慕粉4229098

2018-03-14

老师您好,刚才看了您推荐的quartz课程,但还是没解决quartz跑不起来的问题,因为规定要做一个SSM框架的程序,所以不能转成Spring boot,如果您有时间可以具体的帮我看看问题是出在了什么地方么?运行tomcat时不报错。我会把相关代码附在下方

@Service

@Component("myJob")

public class ProductSellDailyServiceImpl implements ProductSellDailyService {

@Autowired

private ProductSellDailyDao productSellDailyDao;

@Override

public void dailyCalculate() {

System.out.println("Quartz Running!");

}

}

xml文件所在位置

http://img.mukewang.com/szimg/5aa7f8340001eff102180116.jpg

spring-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"

xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 定义一个任务类 

    <bean id="myJob" class="com.yuanlin.o2o.service.ProductSellDailyService"></bean>

-->

    <!-- jobDetail -->

    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

    <property name="name" value="product_sell_daily_job"/>

        <property name="group" value="job_product_sell_daily_group"/>

        <property name="concurrent" value="false" /><!-- 作业不并发调度  -->

        <property name="targetObject" ref="myJob"></property>

        <property name="targetMethod" value="dailyCalculate"></property>

    </bean>


    <!-- 定义trigger 触发器 -->

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

    <property name="name" value="product_sell_daily_trigger"/>

        <property name="group" value="job_product_sell_daily_group"/>

        <property name="jobDetail" ref="jobDetail"></property>

        <property name="cronExpression" value="0/3 * * * * ? *"></property>

    </bean>


    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

        <property name="triggers">

           <list>

                <ref bean="cronTrigger"/>

           </list>

        </property>

    </bean>

</beans>


web.xml中与spring-quartz.xml相关代码

<servlet>

<servlet-name>spring-dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/spring-*.xml</param-value>

</init-param>

</servlet>


写回答

2回答

翔仔

2018-03-14

同学好,下不为例哈。只需要替换以下配置即可,然后执行类和方法对号入座,同时,需要访问一次你的web才开始启动,这个也是合理的

spring-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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "
       default-lazy-init="true">
	<!-- 定义一个任务类 <bean id="myJob" class="com.yuanlin.o2o.service.ProductSellDailyService"></bean> -->
    <!-- 数据分析定时下发工作类 -->
    <bean id="quartzJob" class="com.imooc.o2o.service.impl.ProductSellDailyServiceImpl"/>	
    <!-- 定义调用对象和调用对象的方法 -->
    <bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 调用的类 -->
        <property name="targetObject">
            <ref bean="quartzJob"/>
        </property>
        <!-- 调用类中的方法 -->
        <property name="targetMethod">
            <value>dailyCalculate</value>
        </property>
        <property name="concurrent" value="false"/>
    </bean>
    <!-- 定义触发时间 -->
    <bean id="jobTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="jobTask"/>
        </property>
        <!-- cron表达式 -->
        <property name="cronExpression">
            <value>*/5 * * * * ?</value>
        </property>
    </bean>
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
    <bean id="taskManager" autowire="no"
          class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
          <property name="startupDelay" value="60"/> 
        <property name="triggers">
            <list>
                <ref bean="jobTaskTrigger"/>
            </list>
        </property>
    </bean>
</beans>

pom.xml

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.3.0</version>
		</dependency>

记得访问一下项目的某个页面启动应用,看看控制台信息有没有报错

0
1
慕粉4229098
非常感谢!
2018-03-15
共1条回复

慕粉4229098

提问者

2018-03-14

自己实在是解决不了了,如果老师有时间的话仔细看一下呗( •̥́ ˍ •̀ू )

0
1
慕虎9190267
同学,怎么解决呀,能提供一下思路吗,写完这个quarz.xml,是需要在spring -dao配置吗
2019-11-20
共1条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5113 学习 · 8144 问题

查看课程