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

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

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

    Junky's IT Notebook

    統計

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評論排行榜

    Spring in Action 筆記V -- 使用Spring來發郵件和執行定時任務

    這次來看看Spring提高的一些有用過的JEE中使用到的功能,如 發送Email, 執行定時任務...

    先來看看發送Email吧, 在Spring中發送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 >

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

    ??? < 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 標題 -->

    < value > A Spring Mail sender </ value >

    </ property >

    </ bean >

    <!-- 測試發送的類 -->

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

    < property name = "mailMessage" >

    < ref bean = "mailMessage" />

    </ property >

    < property name = "mailSender" >

    < ref bean = "mailSender" />

    </ property >

    </ bean >

    </

    beans >

    上面的配置好以后就可以直接發送了, 看看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);
    ??? ?//設置email內容,
    ???? message.setText( "測試Spring?發送Email." );
    ????
    ???? try? {
    ?????? mailSender.send(message);
    ???? }? catch? (MailException?e)?{
    ?????? //?TODO?Auto-generated?catch?block
    ?????? System.out.println( "O?.?發送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();
    ?? }
    ?? //拋出如下異常,是瑞星監控的問題,關閉就可以了
    ?? /**
    ??? *?DEBUG?SMTP:?QUIT?failed?with?250?O?.?發送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)
    ??? */
    }

    呵呵, 現在就可以發送Email了. It's easy:)

    和發送Email比起來,定時任務更簡單了, 只要實現TimerTask類,提供一個執行的任務,然后使用Spring的任務調度類來設置執行的參數,再使用任務觸發器來激活任務就可以了.

    定義一個任務是很簡單的實現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 >

    <!-- 任務執行周期 2m 關于一些任務的參數請參考JDK doc文檔和Spring相關文檔-->

    < property name = "period" >

    < value > 2000 </ value >

    </ property >

    <!-- 延時1m 執行任務 -->

    < 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中的定時器比較簡單,其提供的任務也比較簡單, 下面來看看使用quartz來執行一個復雜的任務.

    首先制定一個任務, 實現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?認為調度:?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 >

    <!-- 關鍵在如下兩個觸發器的配置 -->

    <!-- 類似于Java的簡單觸發器 -->

    < 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 >

    <!-- 復雜觸發器 -->

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

    < property name = "jobDetail" >

    < ref bean = "sayHelloJob" />

    </ property >

    < property name = "cronExpression" >

    <!-- 關鍵在配置此表達式 -->

    < 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 >

    關于簡單觸發器和復雜觸發器,查考下面的解釋:

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

    CronTrigger是基于Calendar-like調度的。當你需要在除星期六和星期天外的每天上午10點半執行作業時,那么應該使用CronTrigger。正如它的名字所暗示的那樣,CronTrigger是基于Unix克隆表達式的。

    作為一個例子,下面的Quartz克隆表達式將在星期一到星期五的每天上午10點15分執行一個作業。
    0 15 10 ? * MON-FRI

    下面的表達式
    0 15 10 ? * 6L 2002-2005
    將在2002年到2005年的每個月的最后一個星期五上午10點15分執行作業。

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

    更多詳細介紹參考此處:

    關于cronExpression的介紹:

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

    如上面的表達式所示:

    詳細說明如下:

    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.

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

    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點、11點、12點”。

    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 等. 有些現在用不到, 有些使用其他的會更好, 用不到的等用到的時候在看了, 如 RPC, 對于WebWork我還是喜歡使用JSF, 好了,該Spring in action 筆記暫時就結束了.

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

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

    評論

    # re: Spring in Action 筆記V -- 使用Spring來發郵件和執行定時任務 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)

    按您的方法試了一下,會報如上錯誤  回復  更多評論   

    # re: Spring in Action 筆記V -- 使用Spring來發郵件和執行定時任務 2007-04-18 17:16 zhouqb

    是因為你的SMTP服務器拒絕你的IP發郵件,如果服務器是你的,需要重新配置一下  回復  更多評論   

    主站蜘蛛池模板: 亚洲视频在线观看网址| 久久精品亚洲男人的天堂| 亚洲国产精品成人久久| 全黄A免费一级毛片| 免费在线不卡视频| 337P日本欧洲亚洲大胆艺术图| 免费做爰猛烈吃奶摸视频在线观看| 亚洲婷婷天堂在线综合| 台湾一级毛片永久免费| 中文日韩亚洲欧美制服| 成年女人视频网站免费m| 亚洲中文无码mv| 国产精品免费综合一区视频| 边摸边吃奶边做爽免费视频99| 亚洲阿v天堂在线2017免费| 四虎影视久久久免费| 亚洲人色婷婷成人网站在线观看| 叮咚影视在线观看免费完整版| 亚洲AV色香蕉一区二区| 免费A级毛片无码无遮挡内射| 亚洲熟妇AV一区二区三区浪潮| 国产精品无码一二区免费| 一级特级aaaa毛片免费观看| 亚洲va久久久噜噜噜久久| 免费在线看v网址| 国产成人高清亚洲一区久久 | 亚洲av激情无码专区在线播放| 一区二区三区四区免费视频 | 成在人线av无码免费高潮喷水| 亚洲尹人九九大色香蕉网站 | 亚洲av永久无码精品秋霞电影秋| 亚洲国产成人久久精品99| 免费网站观看WWW在线观看| 亚洲婷婷在线视频| 免费一级毛片不卡不收费| 好紧我太爽了视频免费国产| 国产成人亚洲合集青青草原精品| 亚洲精品99久久久久中文字幕 | 成人免费午夜视频| 精品久久久久久国产免费了| 亚洲另类古典武侠|