<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kingpub

    海內存知己,博客若比鄰

     

    Quartz應用----發送郵件工作調度

    ??????????? Quartz是一個開源的作業調度框架,它完全由Java寫成,并設計用于J2SE和J2EE應用中。下面介紹在J2SE中應用的郵件發送工作調度程序.
    ?????????? Quartz要運行起來,最簡單需要1. ***Job? 自己實現的工作類 ;2. ***Quartz 調度你實現的工作類.
    ?????????? 一.?
    ***Job.java 實現Quartz框架的Job接口.
    /**
    ?*
    ?* Title:???? 發送郵件工作類??
    ?* Copyright:??? Copyright (c) 2006
    ?* Company:????? stone
    ?* @author:??? ?yangstone
    ?* @version 1.0
    ?*/
    package org.yq.myQuartz.jobs;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.quartz.Job;
    import org.quartz.JobDataMap;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.yq.myQuartz.utils.MailJobUtil;

    /**
    ?* @ MailJob.java
    ?* 描述:
    ?* @author yangstone?
    ?* 創建日期:2006-3-18
    ?* @version bookstore 1.0
    ?* 楊強 [stone] 版權所有
    ?*/
    public class MailJob implements Job{

    ??? private static Log _log = LogFactory.getLog(MailJob.class);
    ???
    ??? public static final String FROMMAIL = "fromMail";
    ??? public static final String TOMAIL = "toMail";
    ??? public static final String CONTENT = "content";
    ??? public static final String SUBJECT = "subject";

    ??? public void execute(JobExecutionContext context) throws JobExecutionException {
    ??? ??? JobDataMap data = context.getJobDetail().getJobDataMap();
    ??? ??? MailJobUtil.sendMail(data.getString(FROMMAIL),
    ??? ??? ??? ??? data.getString(TOMAIL), data.getString(CONTENT), data.getString(SUBJECT));
    ??? ???
    ??? }

    }
    ?????? 二.? ***Quartz.java 調度自己實現的Job類,主要是通過Quartz中的觸發器用來告訴調度程序作業什么時候觸發.????
    /**
    ?*
    ?* Title:???? 執行工作類
    ?* Copyright:??? Copyright (c) 2006
    ?* Company:????? stone
    ?* @author:??? ?yangstone
    ?* @version 1.0
    ?*/
    package org.yq.myQuartz.execute;

    import java.util.Date;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerFactory;
    import org.quartz.SchedulerMetaData;
    import org.quartz.Trigger;
    import org.quartz.TriggerUtils;
    import org.quartz.impl.StdSchedulerFactory;
    import org.yq.myQuartz.jobs.MailJob;

    /**
    ?* @ myEmailQuartz.java
    ?* 描述:
    ?* @author yangstone?
    ?* 創建日期:2006-3-18
    ?* @version bookstore 1.0
    ?* 楊強 [stone] 版權所有
    ?*/
    public class myEmailQuartz {

    ??? public void run() throws Exception {
    ??? ??? Log log = LogFactory.getLog(myEmailQuartz.class);

    ??? ??? log.info("------- Initializing -------------------");

    ??? ??? // First we must get a reference to a scheduler
    ??? ??? SchedulerFactory sf = new StdSchedulerFactory();
    ??? ??? Scheduler sched = sf.getScheduler();

    ??? ??? log.info("------- Initialization Complete --------");

    ??? ??? log.info("------- Scheduling Jobs ----------------");

    ??? ??? JobDetail job1 = new JobDetail("job1", "group1", MailJob.class);
    ??? ???
    ??? ??? Trigger trigger = TriggerUtils.makeDailyTrigger(23, 00); //每日 23:00觸發工作執行
    ??? ??? trigger.setName("trigger1");
    ??? ??? trigger.setGroup("group1");
    ??? ??? trigger.setJobName("job1");
    ??? ??? trigger.setJobGroup("group1");

    ??? ??? // pass initialization parameters into the job
    ??? ??? job1.getJobDataMap().put(MailJob.FROMMAIL, "*****");
    ??? ??? job1.getJobDataMap().put(MailJob.TOMAIL, "*****");
    ??? ??? job1.getJobDataMap().put(MailJob.CONTENT, "*****");
    ??? ??? job1.getJobDataMap().put(MailJob.SUBJECT, "*****");
    ??? ???
    ??? ??? // schedule the job to run
    ??? ??? Date scheduleTime1 = sched.scheduleJob(job1, trigger);
    ??? ??? log.info(job1.getFullName() +
    ??? ??? ??? ??? " will run at: " + scheduleTime1);
    ??? ??? log.info(job1.getFullName() +
    ??? ??? ??? ??? " will run at: " + scheduleTime1 );



    ??? ??? log.info("------- Starting Scheduler ----------------");

    ??? ??? // All of the jobs have been added to the scheduler, but none of the jobs
    ??? ??? // will run until the scheduler has been started
    ??? ??? sched.start();

    ??? ??? log.info("------- Started Scheduler -----------------");
    ??? ???
    ??? ??? log.info("------- Waiting 60 seconds... -------------");
    ??? ??? try {
    ??? ??? ??? // wait five minutes to show jobs
    ??? ??? ??? Thread.sleep(60L * 1000L);
    ??? ??? ??? // executing...
    ??? ??? } catch (Exception e) {
    ??? ??? }

    ??? ??? log.info("------- Shutting Down ---------------------");

    ??? ??? sched.shutdown(true);

    ??? ??? log.info("------- Shutdown Complete -----------------");

    ??? ??? SchedulerMetaData metaData = sched.getMetaData();
    ??? ??? log.info("Executed " + metaData.numJobsExecuted() + " jobs.");

    ??? }

    ??? public static void main(String[] args) throws Exception {

    ??? ??? myEmailQuartz example = new myEmailQuartz();
    ??? ??? example.run();
    ??? }
    }
    ???????
    ??????? 至此,一個簡單的應用Quartz進行日發送郵件的程序已經完成.
    ???????? 以下是發送郵件的簡單實現程序:
    /**
    ?*
    ?* Title:??? 發送郵件類??????
    ?* Copyright:??? Copyright (c) 2006
    ?* Company:????? stone
    ?* @author:??? ?yangstone
    ?* @version??? 1.0
    ?*/
    package org.yq.myQuartz.utils;

    import java.util.Date;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;

    /**
    ?* @ SendMail.java
    ?* 描述:
    ?* @author yangstone?
    ?* 創建日期:2006-3-18
    ?* @version bookstore 1.0
    ?* 楊強 [stone] 版權所有
    ?*/
    public class SendMail {
    ??? private String tomail;
    ??? ? private String frommail;
    ??? ? private String subject;
    ??? ? private String content;
    ??? ? private String imagecard;
    ??? ? String smtp="smtp.*****.com";//設置郵件服務器

    ??? ? public SendMail() {
    ??? ? }
    ??? ? public String getTomail() {
    ??? ??? return tomail;
    ??? ? }
    ??? ? public void setTomail(String tomail) {
    ??? ??? this.tomail = tomail;
    ??? ? }
    ??? ? public String getFrommail() {
    ??? ??? return frommail;
    ??? ? }
    ??? ? public void setFrommail(String frommail) {
    ??? ??? this.frommail = frommail;
    ??? ? }
    ??? ? public String getSubject() {
    ??? ??? return subject;
    ??? ? }
    ??? ? public void setSubject(String subject) {
    ??? ??? this.subject = subject;
    ??? ? }
    ??? ? public String getContent() {
    ??? ??? return content;
    ??? ? }
    ??? ? public void setContent(String content) {
    ??? ??? this.content = content;
    ??? ? }
    ??? ? public String getImagecard() {
    ??? ??? return imagecard;
    ??? ? }
    ??? ? public void setImagecard(String imagecard) {
    ??? ??? this.imagecard = imagecard;
    ??? ? }

    ??? ? /**
    ??? ?? *
    ??? ? * 描述:發送郵件
    ??? ? * @return true:發送成功 false:發送失敗
    ??? ? * @author{user}
    ??? ? * 創建時間:2006-3-19
    ??? ?? */
    ??? ? public boolean sendMail(){
    ??? ?? try{
    ??? ???? Properties p=System.getProperties();
    ??? ???? p.put("mail.smtp.host",this.smtp);
    ??? ???? p.put("mail.smtp.auth","true");? //設置為須驗證的模式
    ??? ????
    ??? ???? Authenticator auth = new Email_ca("郵箱用戶名","密碼");
    ??? ???? Session session=Session.getDefaultInstance(p, auth);
    ??? ???? MimeMessage msg=new MimeMessage(session);
    ??? ???? msg.setSentDate(new Date());
    ??? ???? InternetAddress from=new InternetAddress(frommail);
    ??? ???? msg.setFrom(from);
    ??? ???? InternetAddress[] address = {
    ??? ???????? new InternetAddress(tomail)};
    ??? ???? msg.setRecipients(Message.RecipientType.TO, address);
    ??? ???? msg.setSubject(this.subject); //設置郵件主題
    ??? ???? msg.setText(this.content); //設置郵件內容
    ??? ???? Transport.send(msg);
    ??? ???? return true;
    ??? ?? }catch(AddressException addr_e){
    ??? ???? System.out.println(addr_e.getMessage());
    ??? ???? return false;
    ??? ?? }catch(MessagingException msg_e){
    ??? ???? System.out.println(msg_e.getMessage());
    ??? ???? return false;
    ??? ?? }
    ??? }
    }


    /**
    ?*
    ?* Title:????? 郵箱身份認證類
    ?* Copyright:??? Copyright (c) 2006
    ?* Company:????? stone
    ?* @author:??? ?yangstone
    ?* @version 1.0
    ?*/
    package org.yq.myQuartz.utils;

    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;

    /**
    ?* @ Email_ca.java
    ?* 描述:
    ?* @author yangstone?
    ?* 創建日期:2006-3-18
    ?* @version bookstore 1.0
    ?* 楊強 [stone] 版權所有
    ?*/
    public class Email_ca extends Authenticator{
    ??? private String user=null;
    ??? ? private String pwd=null;
    ??? ? public Email_ca(){
    ??? ??? super();
    ??? ? }
    ??? ? public void setUser(String user){
    ??? ??? this.user=user;
    ??? ? }
    ??? ? public void setPwd(String pwd){
    ??? ??? this.pwd=pwd;
    ??? ? }
    ??? ? public Email_ca(String user,String pwd){
    ??? ??? super();
    ??? ??? setUser(user);
    ??? ??? setPwd(pwd);
    ??? ? }
    ??? ? public PasswordAuthentication getPasswordAuthentication(){
    ??? ??? return new PasswordAuthentication(user,pwd);
    ??? ? }
    }


    /**
    ?*
    ?* Title:????? 郵件發送工具類
    ?* Copyright:??? Copyright (c) 2006
    ?* Company:????? stone
    ?* @author:??? ?yangstone
    ?* @version 1.0
    ?*/
    package org.yq.myQuartz.utils;

    /**
    ?* @ MailJob.java
    ?* 描述: 用于QUARTZ調用的門面
    ?* @author yangstone?
    ?* 創建日期:2006-3-18
    ?* @version bookstore 1.0
    ?* 楊強 [stone] 版權所有
    ?*/
    public class MailJobUtil {
    ???
    ??? /**
    ??? ?*
    ??? * 描述:
    ??? * @param fromMail 發送郵箱
    ??? * @param toMail 送至郵箱
    ??? * @param content 郵件內容
    ??? * @param subject 郵件主題
    ??? * @author yangstone
    ??? * 創建時間:2006-3-19
    ??? ?*/
    ??? public static void sendMail(String fromMail, String toMail, String content, String subject){
    ??? ??? SendMail se = new SendMail();
    ??? ??? se.setFrommail(fromMail);
    ??? ??? se.setTomail(toMail);
    ??? ??? se.setContent(content);
    ??? ??? se.setSubject(subject);
    ??? ??? se.sendMail();
    ??? }
    }

    log4j.xml 配置日志輸出格式

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    ? <appender name="default" class="org.apache.log4j.ConsoleAppender">
    ??? <param name="target" value="System.out"/>
    ??? <layout class="org.apache.log4j.PatternLayout">
    ????? <param name="ConversionPattern" value="[%p] %d{dd MMM hh:mm:ss.SSS aa} %t [%c]%n%m%n%n"/>
    ??? </layout>
    ? </appender>


    ?<logger name="org.quartz">
    ?? <level value="debug" />
    ?</logger>

    ? <root>
    ??? <level value="debug" />
    ??? <appender-ref ref="default" />
    ? </root>

    ?
    </log4j:configuration>

    ??????????????? 上面這個郵件發送程序非常實用,很多系統都應該可以用到,要應用于WEB應用需要改動的地方也不多.希望大家試試改造一番,用于自己的網站,不過很多網站已經有這樣的應用了,哈哈!


    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=629471

    posted on 2006-06-05 17:54 xiaofeng 閱讀(282) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费精品国产自产拍观看| 久久精品女人天堂AV免费观看| 亚洲色大成网站www久久九| 亚洲中文字幕无码一去台湾| 亚洲中文字幕乱码AV波多JI| www.黄色免费网站| 在线a亚洲v天堂网2019无码| 亚洲AV本道一区二区三区四区| 亚洲精品**中文毛片| 美女被免费视频网站| 中文字幕视频免费| 性做久久久久免费观看| 亚洲国产精品va在线播放| 亚洲日本va一区二区三区| 成人影片一区免费观看| 国产人在线成免费视频| 国产成人综合亚洲AV第一页| 成全视成人免费观看在线看| 成年午夜视频免费观看视频| 亚洲av无码无在线观看红杏| 国产精品亚洲色婷婷99久久精品| 在线毛片片免费观看| 亚洲AV永久精品爱情岛论坛| 亚欧免费视频一区二区三区| 国外亚洲成AV人片在线观看| 国产午夜免费高清久久影院| 亚洲国产专区一区| 国产成人精品亚洲日本在线 | 久久精品亚洲日本波多野结衣 | 无码国产精品一区二区免费虚拟VR| 亚洲av成人一区二区三区| 久9久9精品免费观看| 亚洲欧洲精品成人久久曰影片| 国产免费久久久久久无码| 成人免费福利电影| 一边摸一边桶一边脱免费视频| 久久久久久久久久免免费精品 | 亚洲国产精品第一区二区三区| 亚洲精品第五页中文字幕| 成人免费a级毛片| 日日摸夜夜添夜夜免费视频|