在application-quartz.xml配置文件中添加如下配置信息:

<!-- Quartz -->
     <bean id="getSendEmailObject" class="com.luguang.baseinfo.util.SendEmailJob">
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>  
      
    <bean id="getSendEmailtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
         <!-- 调用的类 -->
         <property name="targetObject">
             <ref bean="getSendEmailObject"/>
         </property>
         <!-- 调用类中的方法 -->
         <property name="targetMethod">
             <value>taskjob</value>
         </property>
     </bean>
     <!-- 定义触发时间 -->
     <bean id="getSendEmailTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
         <property name="jobDetail">
             <ref bean="getSendEmailtask"/>
         </property>
         <!-- cron表达式 -->
         <property name="cronExpression">
         <value>0 0 9 * * ?</value>
             <!--    <value>0 19 45 ? * * </value>--> 
         </property>
     </bean>

对应的调用类如下:

package com.luguang.baseinfo.util;

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

import javax.persistence.Transient;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.luguang.model.LgmUser;
import com.luguang.product.service.LgpProductLicenseService;
import com.luguang.project.service.LgpLicenseService;

public class SendEmailJob {
 
 private SessionFactory sessionFactory;
 private LgpLicenseService lgpLicenseService;
 private LgpProductLicenseService lgpProductLicenseService;
 
 
 public LgpProductLicenseService getLgpProductLicenseService() {
  return lgpProductLicenseService;
 }
 @Autowired
 public void setLgpProductLicenseService(
   LgpProductLicenseService lgpProductLicenseService) {
  this.lgpProductLicenseService = lgpProductLicenseService;
 }
 public LgpLicenseService getLgpLicenseService() {
  return lgpLicenseService;
 }
 @Autowired
 public void setLgpLicenseService(LgpLicenseService lgpLicenseService) {
  this.lgpLicenseService = lgpLicenseService;
 }

public SessionFactory getSessionFactory() {
  return sessionFactory;
 }

public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

public SendEmailJob() {
  
 }
 @Transient
 public void taskjob() throws JobExecutionException {
  lgpLicenseService.sendEmail();
  lgpProductLicenseService.sendEmail();
  
 }
 
 // 判断传入对象是否是空字符串
 public boolean isNullOrEmptyString(Object o) {
  if (o == null) {
   return true;
  }
  if (o instanceof String) {
   String str = (String) o;
   if (str.length() == 0) {
    return true;
   }
  }
  return false;
 }
 /**判断是否是节假日
  * @param checkDay
  * @param isWeekend 1是周末 0不是周末
  * @return true为是 ,否则为否
  */
 public boolean checkDayIsHoliday(Session session,Date checkDay)
 {
  int isWeekend = 0;
  Calendar cal = Calendar.getInstance();
  cal.setTime(checkDay);
  if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7) {
   isWeekend = 1;
  }
  
  String hql = "SELECT a FROM LgpHoliday a WHERE to_char(a.startDate,'YYYY-MM-DD') <= '" + new SimpleDateFormat("yyyy-MM-dd").format(checkDay) + "' "
  + " and to_char(a.endDate,'YYYY-MM-DD') >= '" + new SimpleDateFormat("yyyy-MM-dd").format(checkDay) + "' and a.isActive = '0' ";
  if (isWeekend == 0) {
   hql += " and a.holidayType != '2' ";
  }
  
  if (isWeekend == 1) {
   hql += " and a.holidayType = '2' ";
  }
  
  List list = session.createQuery(hql).list();
  if (isWeekend == 1 && list.size() > 0) {
   return false;
  }
  if (isWeekend == 0 && list.size() == 0) {
   return false;
  }
  
  return true;
 }

}

service层代码如下:

public String sendEmail(){
  return lgpProductLicenseDao.sendEmail();
  
 }

dao层代码如下:

/*
  * 发送电子邮件
  */
 public String sendEmail(){
  Date now=new Date();
  EmailForProduct emailForProduct;
  String hql="select new com.lg.product.model.EmailForProduct(a.productName,b.lgpLicenseId,c.userAlias,d.mailAddress,b.endDate,b.remindDays) from LgpProduct as a,LgpLicense as b,LgmUser as c,LgmUserIncrement as d where 1=1"
   +" and a.lgpProductId=b.lgpProductId"
   +" and a.productLeader=c.userId"
   +" and c.userId=d.userId";
  List list=this.getHibernateTemplate().find(hql);
  if(list!=null&&list.size()>0){
   for(int i=0;i<list.size();i++){
    emailForProduct=(EmailForProduct)list.get(i);
    long interval=(emailForProduct.getEndDate().getTime()-now.getTime())/1000;
    if(interval<emailForProduct.getRemindDays().longValue()*24*60*60){
     this.sendEmail(emailForProduct.getProductName(), emailForProduct.getEmailAddress());
    }
   }
  }
  return null;
  
 }
 public void sendEmail(String productName ,String emailAddress){
  SimpleEmail email=new SimpleEmail();
  email.setHostName("smtp.126.com");
  email.setAuthentication("name", "password");
  email.setCharset("UTF-8");
  try {
   email.addTo(emailAddress);
   email.setFrom(name@server.com);
   email.setSubject("subject");
   email.setMsg("msg");
   email.send();
  } catch (EmailException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

实现邮件的定时发送,通过在程序中判断邮件发送时间来控制邮件发送,避免在配置文件中设定动态的邮件发送时间(配置文件中获得动态参数是非常麻烦的,尽量在程序中实现)。

spring中的定时任务调度用例的更多相关文章

  1. Spring中实现定时调度

    1,   内容简介 所谓的定时调度,是指在无人值守的时候系统可以在某一时刻执行某些特定的功能采用的一种机制,对于传统的开发而言,定时调度的操作分为两种形式: 定时触发:到某一时间点上执行某些处理操作: ...

  2. 基于Spring Task的定时任务调度器实现

    在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...

  3. spring中如何向一个单例bean中注入非单例bean

    看到这个题目相信很多小伙伴都是懵懵的,平时我们的做法大都是下面的操作 @Component public class People{ @Autowired private Man man; } 这里如 ...

  4. Spring中的单例模式和多例模式的应用

    在Spring的配置中,Bean的scope属性中存在两种模式:singleton(单例模式).prototype(多例模式) singleton 单例模式:对象在整个系统中只有一份,所有的请求都用一 ...

  5. spring中的定时调度实现TimerFactoryBean引起的隐患

    手中的一个老项目,其中使用的TimerFactoryBean实现的调度任务.一般都是spring quartz实现,这种的着实少见.正因为少见资料比较少,当初为了确认这个会不会2个调度任务同时并行执行 ...

  6. Spring中的定时调度(Scheduling)和线程池(Thread Pooling)

    使用triggers和SchedulerFactoryBean来包装任务 我们已经创建了job details,jobs.我们同时回顾了允许你调用特定对象上某一个方法的便捷的bean. 当然我们仍需要 ...

  7. C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler,TaskScheduler,Gofer.NET,Coravel,Quartz.NET还是Hangfire?

    原文由Rector首发于 码友网 之 <C#/.NET/.NET Core应用程序编程中实现定时任务调度的方法或者组件有哪些,Timer,FluentScheduler,TaskSchedule ...

  8. C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是...

    原文:C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是... 原文由Rector首发于 码友网 之 <C#/.NET/.NET ...

  9. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

随机推荐

  1. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  2. Asp.net SignalR 初试和应用笔记一 认识和使用 SignalR

    如果你在用QQ,微信.你会知道,广告和消息无处不在.也有好的一面,比如通过QQ或微信,微博等及时聊天功能,你找到了你的初恋,你找到了小学的班级等等. 这里的及时通信在很多应用场所能用到,比如: 1.球 ...

  3. windows下安装配置Xampp

    XAMPP是一款开源.免费的网络服务器软件,经过简单安装后,就可以在个人电脑上搭建服务器环境.本文为大家介绍Windows中安装XAMPP(Apache+Mysql+PHP)及使用方法及其相关问题的总 ...

  4. Python学习(二) 运行Python,编译Python

    无论windos还是Linux只要安装了python,配置好了环境变量,则在命令行输入python这个命令的时候就会进入交互模式.在这个模式下可以进行一些简单的python代码编写.退出可以使用exi ...

  5. Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务

    第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...

  6. 修改IE8搜索框为指定搜索引擎,如CSDN、百度知道等

    1.运行regedit打开注册表编辑器2.找到\HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\SearchScopes\3.添加新搜索项 ...

  7. Android网络编程之Http通信

    Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序.以下是本人在学习中的总结与归纳.1. HttpURLConnection接口    首先需要 ...

  8. 浅谈标签构建——TagBuilder

    在很多项目中,可能我们需要写一些通用的控件标签,今天来简单的学习一下吧. 在前文中已经学习了 如何自定义MVC控件标签 ,感兴趣的朋友可以去看看. 今天主要还是讲解一下TagBuilder 我们打开源 ...

  9. COB Epoxy灌膠時氣泡產生的原因與解決方法

    COB的黑膠 (Epoxy)有氣泡通常是不被允許的,因為外部氣孔不但會影響到外觀,內部氣孔更有可能會破壞 Wire bonding 的鋁線穩定度.既使在COB製程剛完成的時候沒有通過功能測試,也不代表 ...

  10. Intuit Quicken Home & Business 2016(Manage your business and personal finances)

    Quicken Home & Business 2016 - Manage your business and personal finances all in one place. Cate ...