<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 总管理类 如果将 lazy-init='false' 那么容器启动就会执行调度程序 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="userMigarationScheduleTrigger"/>
<ref local="mailSendScheduleTrigger"/>
<ref local="mailSendToNoFeedBackTrigger"/>
<ref local="noticeToAttendeTraingTrigger"/>
<ref local="mailSendToNoActionTrigger"/>
</list>
</property>
</bean>
//============== part one
<!-- 定义任务触发时间 -->
<bean id="userMigarationScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="userMigarationScheduleJobDetail"/>
</property>
<property name="cronExpression">
<value>0 0/22 8-18 ? * MON-FRI</value>
</property>
</bean>
<!-- 定义任务调用的对象和方法 -->
<bean id="userMigarationScheduleJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="IUserMigarationSchedule"/></property>
<property name="targetMethod" value="doRun"/>
</bean>
<bean id="IUserMigarationSchedule" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="userMigarationSchedule"></property>
<property name="transactionAttributes">
<props>
<prop key="doRun">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
</props>
</property>
</bean>
<!--- 定义 bean 对应的类 -->
<bean id="userMigarationSchedule" class="com.amaxgs.elearning.schedule.UserMigarationSchedule">
//class 中注册dao 使用时不用申明
<property name="externalHRDAO" ref="IExternalHRDAO"/>
<property name="userDAO" ref="IUserDAO"/>
</bean>
二。
UserMigarationSchedule 类
package com.amaxgs.elearning.schedule;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amaxgs.elearning.bo.EmployeeDepartment;
import com.amaxgs.elearning.bo.OrganizationChar;
import com.amaxgs.elearning.bo.User;
import com.amaxgs.elearning.dao.IExternalHRDAO;
import com.amaxgs.elearning.dao.IUserDAO;
import com.amaxgs.elearning.exception.ApplicationRuntimeException;
import com.amaxgs.elearning.service.spring.AbstractService;
import com.amaxgs.elearning.vo.UserMigarationVo;
public class UserMigarationSchedule extends
AbstractService<User, UserMigarationVo> {
private Log log = LogFactory.getLog(UserMigarationSchedule.class);
//spring 中自动注册dao,后面?????????????查查具体的知识
private IExternalHRDAO externalHRDAO;
private IUserDAO userDAO;
public IExternalHRDAO getExternalHRDAO() {
return externalHRDAO;
}
public void setExternalHRDAO(IExternalHRDAO externalHRDAO) {
this.externalHRDAO = externalHRDAO;
}
public IUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) {
this.userDAO = userDAO;
}
public void doRun() {
try {
log.info("====copy user from external HR database ========");
List<UserMigarationVo> usersFromHRDB = externalHRDAO.getAllUsers();
if (null != usersFromHRDB) {
for (UserMigarationVo userFromHRDB : usersFromHRDB) {
User user = userDAO
.findUserByEmpId(userFromHRDB.getEmpId());
if (null != user) {
vo2Bo(userFromHRDB, user);
log.info(" update old user==>"+user.getUsername());
userDAO.update(user);
} else {
if (userFromHRDB.getActive().equals("Y")) {
user = new User();
vo2Bo(userFromHRDB, user);
log.info(" add new user==>"+user.getUsername());
user.setAccountBalance(0);
user.setAccountFreeze(0);
userDAO.save(user);
}
}
if (userFromHRDB.getActive().equals("Y")) {
List<EmployeeDepartment> employeeDepartments = externalHRDAO
.getEmployeeDepartmentByEmpId(userFromHRDB
.getEmpId());
if (null != employeeDepartments) {
OrganizationChar.cachesForOC
.addAll(employeeDepartments);
}
}
}
}
} catch (ApplicationRuntimeException ex) {
throw ex;
} catch (Exception ex) {
log.error("System Internal Error!" + ex.getMessage());
ex.printStackTrace();
throw new ApplicationRuntimeException("System Internal Error!", ex);
}
}
}
//=============== part two
<!--job触发器,绑定下面的jobDetail -->
<bean id="mailSendScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="mailSendScheduleJobDetail"/>
</property>
<property name="cronExpression">
<value>0 0/5 * * * ?</value>
</property>
</bean>
<bean id="mailSendScheduleJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="IMailSendSchedule"/></property>
<property name="targetMethod" value="doRun"/>
</bean>
<bean id="IMailSendSchedule" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="mailSendSchedule" ></property>
<property name="transactionAttributes">
<props>
<prop key="doRun">PROPAGATION_REQUIRED,-ApplicationRuntimeException</prop>
</props>
</property>
</bean>
<bean id="mailSendSchedule" class="com.amaxgs.elearning.schedule.MailSendSchedule">
//spring 中注册相关的属性
<property name="mailDAO" ref="IMailDAO"/>
<property name="isTestModel" value="${isTestingModel}"/>
<property name="testerMailList" value="${testerMailList}"/>
<property name="mailSender" ref="IMailSender"/>
</bean>
<bean id="IMailSender" class="com.amaxgs.elearning.util.MailSender">
<property name="mailsender" value="${mailsender}"/>
<property name="mailpassword" value="${mailpassword}"/>
</bean>
========相关的 MailSendSchedule 类
package com.amaxgs.elearning.schedule;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amaxgs.elearning.bo.Mail;
import com.amaxgs.elearning.bo.MailTypeEnum;
import com.amaxgs.elearning.bo.User;
import com.amaxgs.elearning.dao.IMailDAO;
import com.amaxgs.elearning.exception.ApplicationRuntimeException;
import com.amaxgs.elearning.util.MailSender;
import flex.messaging.io.ArrayCollection;
public class MailSendSchedule{
private Log log = LogFactory.getLog(MailSendSchedule.class);
private IMailDAO mailDAO;
private String isTestModel;
private String testerMailList;
private MailSender mailSender ;
public IMailDAO getMailDAO() {
return mailDAO;
}
public void setMailDAO(IMailDAO mailDAO) {
this.mailDAO = mailDAO;
}
public String getIsTestModel() {
return isTestModel;
}
public void setIsTestModel(String isTestModel) {
this.isTestModel = isTestModel;
}
public String getTesterMailList() {
return testerMailList;
}
public void setTesterMailList(String testerMailList) {
this.testerMailList = testerMailList;
}
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void doRun() {
try{
List<Mail> mails = mailDAO.getAllNoSendMails();
Iterator<Mail> mailsIterator = mails.iterator();
int countTrainer=0;
String userTrainerEmail="";
while(mailsIterator.hasNext()){
Mail mail = mailsIterator.next();
//add by gavin
//just send the publish trainer only one mail
//update more than one training
if(mail.getType().equals(MailTypeEnum.PUBLISH_TRAINING_TO_TRAINER)){
if (userTrainerEmail.equals(mail.getTo())){
countTrainer++;
}else{
userTrainerEmail=mail.getTo();
countTrainer=1;
}
if (countTrainer>1){
mail.setHasSend(true);
mail.setSendTime(new Date());
mailDAO.update(mail);
continue;
}
}
//end of add
String[] mail_to = null;
String[] mail_cc = null;
String mail_subject = null;
String mail_content = null;
Boolean isTest = Boolean.parseBoolean(isTestModel);
if(isTest){
if(null!=testerMailList&&!testerMailList.equals("")){
mail_to = testerMailList.split(",");
}else{
log.debug("Tester's mail has not been set,please check system settings!");
throw new ApplicationRuntimeException("Tester's mail has not been set,please check system settings!");
}
mail_subject = mail.getSubject();
mail_content = "to list: " + mail.getTo() +"<br><br>" +
"cc list: " + mail.getCc() + "<br><br>" +
mail.getContent();
}else{
if(null!=mail.getTo()){
mail_to = mail.getTo().split(",");
}else{
throw new ApplicationRuntimeException("Mail must have mail-to filed");
}
if(null!=mail.getCc()){
mail_cc = mail.getCc().split(",");
}
mail_subject = mail.getSubject();
mail_content = mail.getContent();
}
Boolean sendMailResult = mailSender.send(mail_to, mail_cc, mail_subject, mail_content);
if(sendMailResult){
mail.setHasSend(true);
mail.setSendTime(new Date());
mailDAO.update(mail);
}
}
}catch(Exception ex){
log.error("Sending mail failed by mailsendschedule at " + new Date() + " cause by" + ex.getMessage());
ex.printStackTrace();
}
}
}
======= 相关的MailSender 类
package com.amaxgs.elearning.util;
import com.amaxgs.elearning.util.MyAuthenticator;
import java.util.Properties;
import java.util.Date;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class MailSender {
private static final String MAIL_HOST = "mail.amaxgs.com";
public static final String SYSTEM_EMAIL_ADDRESS = "E-Learning@amaxgs.com";
private static final String USERNAME = "adam_xie";
private static final String PASSWORD = "amaxgs";
private String mailsender;
private String mailpassword;
private Session mailSession;
private Transport transport;
// public void MailSender(){}
public boolean send(String[] mail_to,String[] mail_cc,String mail_subject,String mail_content){
try{
Properties props = new Properties();
props.put("mail.smtp.host", MAIL_HOST);
props.put("mail.smtp.auth","true");
// props.put("mail.smtp.auth","true");
// Added for authentication 1104
MyAuthenticator myauth = new MyAuthenticator(mailsender, mailpassword);
mailSession = Session.getDefaultInstance(props,myauth);
// Added for authentication 1104
// Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
MimeMessage message = new MimeMessage(mailSession);
InternetAddress mailfrom = new InternetAddress(SYSTEM_EMAIL_ADDRESS);
if(null!=mail_to&&mail_to.length>0){
for(int i = 0 ;i<mail_to.length; i++){
InternetAddress mailto = new InternetAddress(mail_to[i]);
message.addRecipient(Message.RecipientType.TO, mailto);
// message.setRecipient(Message.RecipientType.TO, mailto);
}
}
if(null!=mail_cc&&mail_cc.length>0){
for(int i = 0 ;i<mail_cc.length; i++){
InternetAddress mailcc = new InternetAddress(mail_cc[i]);
message.addRecipient(Message.RecipientType.CC, mailcc);
// message.setRecipient(Message.RecipientType.CC, mailcc);
}
}
message.setFrom(mailfrom);
// Modified in 1005
message.setSubject(MimeUtility.encodeText(mail_subject, "UTF-8", "B"));
// Modified in 1005
// message.setSubject(mail_subject);
// message.setText(mail_content);
message.setSentDate(new Date());
BodyPart bodyPart = new MimeBodyPart();
// It's interesting Modified 1005
// bodyPart.setDataHandler(new DataHandler(mail_content,"text/html;charset=utf8"));
// It's interesting Modified 1005
bodyPart.setDataHandler(new DataHandler(mail_content,"text/html;charset=gb2312"));
Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(bodyPart);
message.setContent(multipart);
message.saveChanges();
transport = mailSession.getTransport("smtp");
transport.connect(mailsender, mailpassword);
// transport.connect();
Transport.send(message,message.getAllRecipients());
transport.close();
return true;
}catch(Exception ex){
ex.printStackTrace();
try{
transport.close();
}catch(Exception ex1){
ex1.printStackTrace();
}
return false;
}
}
public String getMailsender() {
return mailsender;
}
public void setMailsender(String mailsender) {
this.mailsender = mailsender;
}
public String getMailpassword() {
return mailpassword;
}
public void setMailpassword(String mailpassword) {
this.mailpassword = mailpassword;
}
}