(转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
http://blog.csdn.net/u010648555/article/details/60767633
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯。
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程!
本篇是在SSM框架——spring+SpringMVC+Mybatis的搭建教程这篇为基础上进行的,如果不了解SSM搭建请先点击闪现到查看详情:闪现
一:环境介绍
工具:Eclipse+ MySQL
框架:Spring+SpringMVC+Mybatis
日志:logback
构建工具:Maven
单元测试:Junit4
Quartz版本:2.2.1
二:SSMM+Quartz集成详解
1:概述
在之前SSM框架的基础集合Quartz写一个简单的Web项目,实现Quartz的动态添加,修改和删除功能!(这里只是对Cron类型的Trigger进行操作)
2:项目结构 
3:初始化
在项目中找到初始化sql——quartz_test.sql ,导入自己的数据库中!初始化的表信息如下:
qrtz_blob_triggers,
qrtz_calendars,
qrtz_fired_triggers,
qrtz_locks,
qrtz_paused_trigger_grps,
qrtz_scheduler_state,
qrtz_simple_triggers,
qrtz_simprop_triggers,
qrtz_triggers,
user_t
4:配置文件介绍
(1):在ApplicationContext.xml中添加下面的配置:
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="dataSource" ref ="dataSource" />
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>

(2):添加quartz.properties配置文件
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName: quartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 2
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
org.quartz.jobStore.misfireThreshold: 60000
#============================================================================
# Configure JobStore
#============================================================================
#default config
#org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
#持久化配置
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties:true
#============================================================================
#havent cluster spring
#============================================================================
org.quartz.jobStore.isClustered = false
#数据库表前缀
org.quartz.jobStore.tablePrefix:qrtz_
#org.quartz.jobStore.dataSource:qzDS
#============================================================================
# Configure Datasources
#============================================================================
#JDBC驱动 Sping去管理dataSource ,这里不在配置数据源信息
#org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
#org.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/quartz_test
#org.quartz.dataSource.qzDS.user:root
#org.quartz.dataSource.qzDS.password:root
#org.quartz.dataSource.qzDS.maxConnection:10
5:关键代码简单介绍
(1):过滤器
添加过滤器拦截请求,若用户没有登录,则跳转到登录页面!
a. 新增LoginFilter.Java,核心代码如下:
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
HttpSession session = request.getSession();
//判断Session中是否有登录用户信息
String toke = (String) session.getAttribute(CommonConstant.LONGIN_TOKE);
if(!StringUtils.isEmpty(toke)){
chain.doFilter(req, resp);
}else{
//若没有则,跳转到登录页面
response.sendRedirect(request.getContextPath() + "/user/toLogin");
}
}
b.在web.xml配置过滤器:
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>org.ssm.dufy.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
(2)BAO和Service接口
Dao主要是用户的一些操作!
public interface IUserDao {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
User findUser(User user);
}
Service主要是对定时任务的一些操作!
package org.ssm.dufy.service;
public interface QuartzService {
/**
* addJob(方法描述:添加一个定时任务) <br />
* (方法适用条件描述: – 可选)
*
* @param jobName
* 作业名称
* @param jobGroupName
* 作业组名称
* @param triggerName
* 触发器名称
* @param triggerGroupName
* 触发器组名称
* @param cls
* 定时任务的class
* @param cron
* 时间表达式 void
* @exception
* @since 1.0.0
*/
public void addJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName, Class cls, String cron);
/**
*
* @param oldjobName 原job name
* @param oldjobGroup 原job group
* @param oldtriggerName 原 trigger name
* @param oldtriggerGroup 原 trigger group
* @param jobName
* @param jobGroup
* @param triggerName
* @param triggerGroup
* @param cron
*/
public boolean modifyJobTime(String oldjobName,String oldjobGroup, String oldtriggerName, String oldtriggerGroup, String jobName, String jobGroup,String triggerName, String triggerGroup, String cron);
/**
* 修改触发器调度时间
* @param triggerName 触发器名称
* @param triggerGroupName 触发器组名称
* @param cron cron表达式
*/
public void modifyJobTime(String triggerName,
String triggerGroupName, String cron);
/**
* 暂停指定的任务
* @param jobName 任务名称
* @param jobGroupName 任务组名称
* @return
*/
public void pauseJob(String jobName,String jobGroupName);
/**
* 恢复指定的任务
* @param jobName 任务名称
* @param jobGroupName 任务组名称
* @return
*/
public void resumeJob(String jobName,String jobGroupName);
/**
* 删除指定组任务
* @param jobName 作业名称
* @param jobGroupName 作业组名称
* @param triggerName 触发器名称
* @param triggerGroupName 触发器组名称
*/
public void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName);
/**
* 开始所有定时任务。启动调度器
*/
public void startSchedule();
/**
* 关闭调度器
*/
public void shutdownSchedule();
}
操作用户通过Mybatis进行,操作定时器任务,使用的Quartz封装好的接口!
主要的实现代码就不在这里展示!如需查看请看源码!
(3)Controller
(1):UserController
处理一些用户的请求操作!
(2):QuartzController
处理Quartz的请求操作!
6:任务类
package org.ssm.dufy.job;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloWorldJob implements Job{
/**
* "0/5 * * * * ? 五秒运行一次
*/
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("----hello world---" + new Date());
}
}
7:待优化地方
- 界面的美观性
- 可以添加Simple类型的Trigger
- 用户信息的管理
- 抽象Job的Dao类
。。。。。。
三:运行效果介绍
有两种方式启动本实例项目:
1:Tomcat方式,Tomcat方式不在讲解!
2:Jetty方式
在pom.xml中配置了Jetty的依赖的插件!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.15.v20140411</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopPort>9999</stopPort>
<reload>manual</reload>
<webAppConfig>
<contextPath>/ssm_quratz</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
在Eclipse运行步骤如在截图:

配置的参数:
-server -Xms1024m -Xmx2048m -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDetails -Xloggc:%M2_HOME%/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%M2_HOME%/java_pid.hproyuan
- 1
- 1
配置好之后运行启动不报错就ok!
最后打开浏览器,输入 http://localhost:8080/ssm_quratz
3:运行结果图
(1)登录页面

(2)任务列表页面
(3)新增页面(编辑页面和此类似)
(4)Eclipse控制台打印的信息

四:源码
SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成 源码
(转) Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)的更多相关文章
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...
- 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)
如约而至,Java 10 正式发布! 3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...
- SSM后台管理系统(Spring SpringMVC Mybatis Mysql EasyUI)
非常简单的一个后台管理系统,功能不多,框架也不复杂, 源码下载(附数据库)-ssm后台管理系统框架(Spring mvc + mybatis + mysql + easyui ) 实例图片
- Java架构学习 转(Spring+SpringMVC+MyBatis+easyUI)
Spring+SpringMVC+MyBatis+easyUI : http://www.cnblogs.com/han-1034683568/p/6730869.html
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 简介 这是一篇关于Redis使用的总结类型文章,会先简单的谈一下缓存 ...
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller //控制类 service //服务接口 service.impl //服务 ...
- 带你搭建一个简单的mybatis项目:IDEA+spring+springMVC+mybatis+Mysql
最近小编有点闲,突发奇想想重温一下mybatis,然后在脑海中搜索了一下,纳尼,居然不太会用了,想到这里都是泪啊!!现在我所呆的的公司使用的是springboot+hebinate,编程都是使用的JP ...
- SSM框架整合(注解)-Spring+SpringMVC+MyBatis+MySql
准备工作: 下载整合所需的jar包 点击此处下载 使用MyBatis Generator生成dao接口.映射文件和实体类 如何生成 搭建过程: 先来看一下项目的 目录结构 1.配置dispatcher ...
随机推荐
- jQuery -> 获取/设置HTML或TEXT内容
jQuery提供了两个API能够直接用来为元素加入内容. html() text() 当中html()是为指定的元素加入html内容 text()是为指定的元素加入文本内容 两者的差别在于,text中 ...
- POJ-3268-最短路(dijkstra算法)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12494 Accepted: 5568 ...
- HDU 1421 搬寝室 (线性dp 贪心预处理)
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- LeetCode 160. Intersection of Two Linked Lists (两个链表的交点)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Android数据存储之IO
Android开发中免不了数据本地的存储,今天我们来说一说怎样利用IO流来进行数据存储. 这里我们通过模拟一个QQ登陆界面的小demo来实际操作IO流. 功能描写叙述:点击button能够保存用户输入 ...
- FourCC
https://en.wikipedia.org/wiki/FourCC A FourCC (literally, four-character code) is a sequence of four ...
- TextWatcher基本用法
editText.addTextChangedListener(new TextWatcher() { /** * 内容改变前调用 * 原有的文本s中,从start开始的count个字符将会被一个新的 ...
- SQL SERVER 语句大全
·SQL的简单查询实例教程关键词:SQL语句大全 中文网 整理编辑,经典SQL语句大全(SQL语句大总结),欢迎网友投稿 下列语句部分是Mssql语句,不可以在access中使用.SQL分类:DDL— ...
- 46. Ext中namespace的作用(转)
转自:https://www.cnblogs.com/givemeanorange/p/5569954.html Ext中在每一个页面中添加一个namespace呢,就像下面的代码: // creat ...
- SS配置,Brook是什么?,Brook如何配置(Android篇)
很长时间没有更新了,今天给大家分享一下什么是Brook,和SS有什么区别?写的不好,请勿见外,大佬绕过. Brook简单介绍 Brook 是一个高效的 Socks5 代理软件,官方支持Windows. ...