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

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

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

    Junky's IT Notebook

    統(tǒng)計(jì)

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評論排行榜

    Spring in Action 筆記V -- 使用Spring來發(fā)郵件和執(zhí)行定時任務(wù)

    這次來看看Spring提高的一些有用過的JEE中使用到的功能,如 發(fā)送Email, 執(zhí)行定時任務(wù)...

    先來看看發(fā)送Email吧, 在Spring中發(fā)送Email是很簡單的,使用Spring提高的MailSender和MailMessage就可以了,配置代碼如下:

    <?

    xml version = "1.0" encoding = "UTF-8" ?>

    <!

    DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

    <

    beans >

    < bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl" >

    < property name = "host" >

    ?

    < value > smtp.163.com </ value >

    </ property >

    < property name = "javaMailProperties" >

    ?

    < props >

    ??? <!-- 如果要使用用戶名和密碼驗(yàn)證,這一步需要 -->

    ??? < prop key = "mail.smtp.auth" > true </ prop >

    ? </ props >

    </ property >

    < property name = "username" >

    < value > 你的Email地址 </ value >

    </ property >

    < property name = "password" >

    < value > 你的Email密碼 </ value >

    </ property >

    </ bean >

    <!-- 簡單的message -->

    < bean id = "mailMessage" class = "org.springframework.mail.SimpleMailMessage" >

    < property name = "to" >

    < value > 收件人地址 </ value >

    </ property >

    < property name = "from" >

    < value > 你的地址 </ value >

    </ property >

    < property name = "subject" > <!-- Email 標(biāo)題 -->

    < value > A Spring Mail sender </ value >

    </ property >

    </ bean >

    <!-- 測試發(fā)送的類 -->

    < bean id = "testMailSender" class = "test.mail.TestSenderMail" >

    < property name = "mailMessage" >

    < ref bean = "mailMessage" />

    </ property >

    < property name = "mailSender" >

    < ref bean = "mailSender" />

    </ property >

    </ bean >

    </

    beans >

    上面的配置好以后就可以直接發(fā)送了, 看看TestSenderMail.java的代碼:

    package? test.mail;

    import? org.springframework.mail.MailException;
    import? org.springframework.mail.MailSender;
    import? org.springframework.mail.SimpleMailMessage;

    public?class? TestSenderMail?{
    ?? private? MailSender?mailSender;
    ?? private? SimpleMailMessage?mailMessage;
    ??
    ?? public? TestSenderMail()?{
    ????
    ?? }
    ??
    ?? public? SimpleMailMessage?getMailMessage()?{
    ???? return? mailMessage;
    ?? }
    ?? public?void? setMailMessage(SimpleMailMessage?mailMessage)?{
    ???? this .mailMessage?=?mailMessage;
    ?? }
    ?? public? MailSender?getMailSender()?{
    ???? return? mailSender;
    ?? }
    ?? public?void? setMailSender(MailSender?mailSender)?{
    ???? this .mailSender?=?mailSender;
    ?? }
    ??
    ?? public?void? sendMail()?{
    ???? SimpleMailMessage?message?=? new? SimpleMailMessage(mailMessage);
    ??? ?//設(shè)置email內(nèi)容,
    ???? message.setText( "測試Spring?發(fā)送Email." );
    ????
    ???? try? {
    ?????? mailSender.send(message);
    ???? }? catch? (MailException?e)?{
    ?????? //?TODO?Auto-generated?catch?block
    ?????? System.out.println( "O?.?發(fā)送Email失敗了...." );
    ?????? e.printStackTrace();
    ???? }
    ?? }
    }

    很簡單吧. 下面是測試類: TestApp.java

    package? test.mail;

    import? org.springframework.context.ApplicationContext;
    import? org.springframework.context.support.ClassPathXmlApplicationContext;

    public?class? TestApp?{

    ?? /**
    ??? *? @param? args
    ??? */
    ?? public?static?void? main(String[]?args)?{
    ???? //?TODO?Auto-generated?method?stub
    ???? ApplicationContext?context?=? new? ClassPathXmlApplicationContext(
    ???????? "test/mail/mail.xml" );

    ???? TestSenderMail?sender?=?(TestSenderMail)?context
    ???????? .getBean( "testMailSender" );
    ???? sender.sendMail();
    ?? }
    ?? //拋出如下異常,是瑞星監(jiān)控的問題,關(guān)閉就可以了
    ?? /**
    ??? *?DEBUG?SMTP:?QUIT?failed?with?250?O?.?發(fā)送Email失敗了....
    ??? *?org.springframework.mail.MailSendException:?Could?not?send?mails:?354
    ??? *?
    ??? *?com.sun.mail.smtp.SMTPSendFailedException:?354
    ??? *?
    ??? *?at
    ??? *?com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
    ??? *?at?com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1215)?at
    ??? *?com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:586)
    ??? */
    }

    呵呵, 現(xiàn)在就可以發(fā)送Email了. It's easy:)

    和發(fā)送Email比起來,定時任務(wù)更簡單了, 只要實(shí)現(xiàn)TimerTask類,提供一個執(zhí)行的任務(wù),然后使用Spring的任務(wù)調(diào)度類來設(shè)置執(zhí)行的參數(shù),再使用任務(wù)觸發(fā)器來激活任務(wù)就可以了.

    定義一個任務(wù)是很簡單的實(shí)現(xiàn)TimerTask的run方法就可以了.

    如下:SayHelloTask.java

    package? test.timerTask;

    import? java.util.TimerTask;

    public?class? SayHelloTask? extends? TimerTask?{

    ?? @Override
    ?? public?void? run()?{
    ???? //?TODO?Auto-generated?method?stub
    ???? System.out.println( "測試TimerTask?:?Hello?!!" );
    ?? }

    }

    然后是配置文件:

    <?

    xml version = "1.0" encoding = "UTF-8" ?>

    <!

    DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

    <

    beans >

    < bean id = "sayHelloTask" class = "test.timerTask.SayHelloTask" ></ bean >

    < bean id = "scheduledTask" class = "org.springframework.scheduling.timer.ScheduledTimerTask" >

    < property name = "timerTask" >

    < ref bean = "sayHelloTask" />

    </ property >

    <!-- 任務(wù)執(zhí)行周期 2m 關(guān)于一些任務(wù)的參數(shù)請參考JDK doc文檔和Spring相關(guān)文檔-->

    < property name = "period" >

    < value > 2000 </ value >

    </ property >

    <!-- 延時1m 執(zhí)行任務(wù) -->

    < property name = "delay" >

    < value > 1000 </ value >

    </ property >

    </ bean >

    <!-- 啟動定時器 -->

    < bean id = "timerBean" class = "org.springframework.scheduling.timer.TimerFactoryBean" >

    < property name = "scheduledTimerTasks" >

    < list >

    < ref bean = "scheduledTask" />

    </ list >

    </ property >

    </ bean >

    </

    beans >

    測試類如下:TestApp.java

    package? test.timerTask;

    import? org.springframework.context.ApplicationContext;
    import? org.springframework.context.support.ClassPathXmlApplicationContext;

    public?class? TestApp?{

    ?? /**
    ??? *? @param? args
    ??? */
    ?? public?static?void? main(String[]?args)?{
    ???? //?TODO?Auto-generated?method?stub
    ????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("test/timerTask/javaTimer.xml");
    ?// ??? ApplicationContext?context2?=? new? ClassPathXmlApplicationContext( "test/timerTask/quartzTimer.xml" );
    ?? }
    // 只要加載配置文件就可以了,
    }

    使用Java中的定時器比較簡單,其提供的任務(wù)也比較簡單, 下面來看看使用quartz來執(zhí)行一個復(fù)雜的任務(wù).

    首先制定一個任務(wù), 實(shí)現(xiàn)QuartzJobBean中的方法.

    package? test.timerTask;

    import? org.quartz.JobExecutionContext;
    import? org.quartz.JobExecutionException;
    import? org.springframework.scheduling.quartz.QuartzJobBean;

    public?class? SayHelloTaskUsingQuartz? extends? QuartzJobBean?{

    ?? @Override
    ?? protected?void? executeInternal(JobExecutionContext?context)
    ?????? throws? JobExecutionException?{
    ???? //?TODO?Auto-generated?method?stub
    ???? System.out.println( "使用Quartz?認(rèn)為調(diào)度:?Hello!!" );
    ?? }

    }

    配置代碼如下:quartzTimer.xml

    <?

    xml version = "1.0" encoding = "UTF-8" ?>

    <!

    DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >

    <

    beans >

    < bean id = "sayHelloJob" class = "org.springframework.scheduling.quartz.JobDetailBean" >

    < property name = "jobClass" >

    < value > test.timerTask.SayHelloTaskUsingQuartz </ value >

    </ property >

    </ bean >

    <!-- 關(guān)鍵在如下兩個觸發(fā)器的配置 -->

    <!-- 類似于Java的簡單觸發(fā)器 -->

    < bean id = "helloTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerBean" >

    < property name = "jobDetail" >

    < ref bean = "sayHelloJob" />

    </ property >

    < property name = "startDelay" >

    < value > 1000 </ value >

    </ property >

    < property name = "repeatInterval" >

    < value > 3000 </ value >

    </ property >

    </ bean >

    <!-- 復(fù)雜觸發(fā)器 -->

    < bean id = "helloCronTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean" >

    < property name = "jobDetail" >

    < ref bean = "sayHelloJob" />

    </ property >

    < property name = "cronExpression" >

    <!-- 關(guān)鍵在配置此表達(dá)式 -->

    < value > 0 49 15 * * ? </ value >

    </ property >

    </ bean >

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

    < property name = "triggers" >

    < ref bean = "helloCronTrigger" />

    </ property >

    </ bean >

    </

    beans >

    關(guān)于簡單觸發(fā)器和復(fù)雜觸發(fā)器,查考下面的解釋:

    Quartz設(shè)計(jì)者做了一個設(shè)計(jì)選擇來從調(diào)度分離開作業(yè)。Quartz中的觸發(fā)器用來告訴調(diào)度程序作業(yè)什么時候觸發(fā)。框架提供了一把觸發(fā)器類型,但兩個最常用的是SimpleTrigger和CronTrigger。SimpleTrigger為需要簡單打火調(diào)度而設(shè)計(jì)。典型地,如果你需要在給定的時間和重復(fù)次數(shù)或者兩次打火之間等待的秒數(shù)打火一個作業(yè),那么SimpleTrigger適合你。另一方面,如果你有許多復(fù)雜的作業(yè)調(diào)度,那么或許需要CronTrigger。

    CronTrigger是基于Calendar-like調(diào)度的。當(dāng)你需要在除星期六和星期天外的每天上午10點(diǎn)半執(zhí)行作業(yè)時,那么應(yīng)該使用CronTrigger。正如它的名字所暗示的那樣,CronTrigger是基于Unix克隆表達(dá)式的。

    作為一個例子,下面的Quartz克隆表達(dá)式將在星期一到星期五的每天上午10點(diǎn)15分執(zhí)行一個作業(yè)。
    0 15 10 ? * MON-FRI

    下面的表達(dá)式
    0 15 10 ? * 6L 2002-2005
    將在2002年到2005年的每個月的最后一個星期五上午10點(diǎn)15分執(zhí)行作業(yè)。

    你不可能用SimpleTrigger來做這些事情。你可以用兩者之中的任何一個,但哪個跟合適則取決于你的調(diào)度需要。

    更多詳細(xì)介紹參考此處:

    關(guān)于cronExpression的介紹:

     
    字段?允許值?允許的特殊字符
    ? 0-59? , - * /
    ? 0-59? , - * /
    小時? 0-23? , - * /
    日期? 1-31? , - * ? / L W C
    月份? 1-12?或者 JAN-DEC? , - * /
    星期? 1-7?或者 SUN-SAT? , - * ? / L C #
    年(可選)? 留空, 1970-2099? , - * /
     

    如上面的表達(dá)式所示:

    詳細(xì)說明如下:

    The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".

    “*”字符被用來指定所有的值。如:”*“在分鐘的字段域里表示“每分鐘”。

    The '?' character is allowed for the mother day-of-month and mother day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.

    “?”字符只在日期域和星期域中使用。它被用來指定“非明確的值”。當(dāng)你需要通過在這兩個域中的一個來指定一些東西的時候,它是有用的。看下面的例子你就會明白。

    The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".

    “-”字符被用來指定一個范圍。如:“10-12”在小時域意味著“10點(diǎn)、11點(diǎn)、12點(diǎn)”。

    The ',' character is used to specify additional values. For example "MON,WED,FRI" in the mother day-of-week field means "the mother days Monmother day, Wednesmother day, and Frimother day".

    “,”字符被用來指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.

    The '/' character is used to specify increments. For example "0/15" in the seconds field means "the seconds 0, 15, 30, and 45". And "5/15" in the seconds field means "the seconds 5, 20, 35, and 50". Specifying '*' before the '/' is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for mother days of the month 0 to 31, and for months 1 to 12. The "/" character simply helps you turn on every "nth" value in the given set. Thus "7/6" in the month field only turns on month "7", it does NOT mean every 6th month, please note that subtlety.

    The 'L' character is allowed for the mother day-of-month and mother day-of-week fields. This character is short-hand for "last", but it has different meaning in each of the two fields. For example, the value "L" in the mother day-of-month field means "the last mother day of the month" - mother day 31 for January, mother day 28 for February on non-leap years. If used in the mother day-of-week field by itself, it simply means "7" or "SAT". But if used in the mother day-of-week field after another value, it means "the last xxx mother day of the month" - for example "6L" means "the last frimother day of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

    The 'W' character is allowed for the mother day-of-month field. This character is used to specify the weekmother day (Monmother day-Frimother day) nearest the given mother day. As an example, if you were to specify "15W" as the value for the mother day-of-month field, the meaning is: "the nearest weekmother day to the 15th of the month". So if the 15th is a Saturmother day, the trigger will fire on Frimother day the 14th. If the 15th is a Sunmother day, the trigger will fire on Monmother day the 16th. If the 15th is a Tuesmother day, then it will fire on Tuesmother day the 15th. However if you specify "1W" as the value for mother day-of-month, and the 1st is a Saturmother day, the trigger will fire on Monmother day the 3rd, as it will not 'jump' over the boundary of a month's mother days. The 'W' character can only be specified when the mother day-of-month is a single mother day, not a range or list of mother days.

    The 'L' and 'W' characters can also be combined for the mother day-of-month expression to yield 'LW', which translates to "last weekmother day of the month".

    The '#' character is allowed for the mother day-of-week field. This character is used to specify "the nth" XXX mother day of the month. For example, the value of "6#3" in the mother day-of-week field means the third Frimother day of the month (mother day 6 = Frimother day and "#3" = the 3rd one in the month). Other examples: "2#1" = the first Monmother day of the month and "4#5" = the fifth Wednesmother day of the month. Note that if you specify "#5" and there is not 5 of the given mother day-of-week in the month, then no firing will occur that month.

    The 'C' character is allowed for the mother day-of-month and mother day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the mother day-of-month field means "the first mother day included by the calendar on or after the 5th". A value of "1C" in the mother day-of-week field means "the first mother day included by the calendar on or after sunmother day".

     

    Spring in Action 中提到的Spring就先看到這里了,Spring提供的其他的東西,如RPC , WebWork 等. 有些現(xiàn)在用不到, 有些使用其他的會更好, 用不到的等用到的時候在看了, 如 RPC, 對于WebWork我還是喜歡使用JSF, 好了,該Spring in action 筆記暫時就結(jié)束了.

    總體感覺Spring 提供的很多功能還是很好用的.其AoP功能雖然還沒有用到, 但也是很好用的一個功能, 說不定明天就要使用了, 呵呵, who knows.

    posted on 2006-05-31 23:04 junky 閱讀(1972) 評論(2)  編輯  收藏 所屬分類: spring

    評論

    # re: Spring in Action 筆記V -- 使用Spring來發(fā)郵件和執(zhí)行定時任務(wù) 2007-03-06 11:42 lubaolin

    2007-03-06 11:45:34,703 ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/studyspring].[studyspring] - Servlet.service() for servlet studyspring threw exception
    org.springframework.mail.MailSendException: Could not send mails: Invalid Addresses;
    nested exception is:
    class javax.mail.SendFailedException: 554 <unknown[211.99.137.26]>: Client host rejected: Access denied

    javax.mail.SendFailedException: Invalid Addresses;
    nested exception is:
    class javax.mail.SendFailedException: 554 <unknown[211.99.137.26]>: Client host rejected: Access denied

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:926)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:389)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:382)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:291)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:277)
    at com.ite.studyspring.service.impl.SendMailServiceImpl.sendMail(SendMailServiceImpl.java:35)
    at com.ite.studyspring.action.SendMailController.handleRequest(SendMailController.java:18)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)

    按您的方法試了一下,會報(bào)如上錯誤  回復(fù)  更多評論   

    # re: Spring in Action 筆記V -- 使用Spring來發(fā)郵件和執(zhí)行定時任務(wù) 2007-04-18 17:16 zhouqb

    是因?yàn)槟愕腟MTP服務(wù)器拒絕你的IP發(fā)郵件,如果服務(wù)器是你的,需要重新配置一下  回復(fù)  更多評論   

    主站蜘蛛池模板: 色久悠悠婷婷综合在线亚洲| 亚洲精华液一二三产区| mm1313亚洲精品国产| 免费精品国产自产拍在线观看图片| caoporm碰最新免费公开视频| 亚洲已满18点击进入在线观看| 亚洲成色999久久网站| 久久精品国产精品亚洲人人 | 亚洲av中文无码乱人伦在线r▽| 国产男女性潮高清免费网站| 无码人妻一区二区三区免费手机| 久9久9精品免费观看| 国产免费内射又粗又爽密桃视频| 黄色a级片免费看| 午夜亚洲国产理论片二级港台二级 | 亚洲精品天天影视综合网| 久久亚洲欧洲国产综合| 国产免费av片在线无码免费看| 一二三四免费观看在线视频中文版 | 久久青草免费91线频观看不卡 | 不卡一卡二卡三亚洲| 免费一级黄色毛片| 国产精品免费小视频| 日韩毛片免费在线观看| 成人免费a级毛片| 欧洲黑大粗无码免费| 91免费资源网站入口| 岛国av无码免费无禁网站| 中国在线观看免费国语版| 欧洲黑大粗无码免费| 日本成人在线免费观看| 国产午夜影视大全免费观看| 国产成人涩涩涩视频在线观看免费| 国产性生交xxxxx免费| 免费人成无码大片在线观看| 亚洲精品无码久久久| 亚洲色欲一区二区三区在线观看| 亚洲无线码在线一区观看| 亚洲精品国产美女久久久| 亚洲AV无码码潮喷在线观看| 亚洲麻豆精品果冻传媒|