动态进度条(邮件发送)

一直看博客,很少动手去写,今天就写一点,算是自己的笔记吧,同时也欢迎各位大神吐槽!

群发邮件,在web页面动态显示邮件总数,发送成功及失败的个数(动态进度条显示);

web页面:使用progressbar插件

jsp代码:

var progressbar = $("#progressbar");
var progressLabel = progressbar.find(".progress-label");
$(function() {
 progressbar = $("#progressbar");
 progressLabel = progressbar.find(".progress-label");
 progressbar.progressbar({
  value: false,
  change: function() {
  },
  complete: function() {
  }
 });
});

 

var d;
var current;
var total;
var failed = 0;

function sendEmail() {
 d = false;
 progressbar.progressbar( "value", 0);
 progressLabel.text("Sending...");
 var param = new Object();
 var url = "<%=basePath%>sendEmail/sendEmailManagement.action?temp="+Math.random();
 $("#Subutton").attr("disabled",false);

 $.get(url, $.param(param, true), function(data) {

  progress();
 });
}
 var current;
 var total;
 var failed = 0;
function progress() {
    $.get("<%=basePath%>sendEmail/checkSendingStatus.action?temp="+Math.random(), function(data1) {
    if(data1 == "finished"){
   progressbar.progressbar( "value", 100);
   progressLabel.text("Complete, with " + failed + " mails failed"); 
      d = true;
      return;
     }
     try{
      current = data1.split("-")[0];
   total = data1.split("-")[1];
   failed = data1.split("-")[2];
   var percent = 100 * parseFloat(current)/parseFloat(total);
   try{
    progressbar.progressbar( "value", percent);
    progressLabel.text("Sending .. " + current + "/" + total + " - " + failed + " failed");
   }
   catch(e){}
     }
     catch(e){
      progressbar.progressbar( "value", 100);
   progressLabel.text("Complete!");
     }
 });
 if(d == false){
  setTimeout( "progress()", 1000 );
 } 
}
</script>

 </head>

 <body>
  <center>
   <div class="container" style="width:90%;height: 200px;">
   
    <div class="operation">
     <div class="oButton"><input type="submit" class="button" value="Send" id="Subutton" onclick="javascript: sendEmail();" >
     </div>
    </div>
    <div id="progressbar">
    <div class="progress-label">send emails</div>
    </div>
   </div>
    </center>
 </body>
</html>

 

在这之前要记得导入progressbar相关的js文件,同时在jsp中要有

<div id="progressbar">
    <div class="progress-label">send emails</div>
    </div>
action代码:

为了进度条能够动态显示邮件个数,后台代码使用了线程:

public String sendEmailManagement() throws Exception {
  logger.info("sendEmaiManagement method");
  emailList = sendEmailService.getEmailInfo();
  int count = 0;
  for (int i = 0; i < emailList.size(); i++) {
   ++count;
  }
  sendEmail(emailList,count);
  return SUCCESS;
 }

 public void sendEmail(List<EmailBean> list,int emailCount) throws Exception{
  status = "0-0-0";
  instance = this;
  final List<EmailBean> eamilList = list;
  Thread task = new Thread(){
   public Object instance;
   public void run(){
    int count = 0;
    int failCount = 0;
    try {
     for(int i = 0; i < eamilList.size(); i++){
      EmailBean bean = emailList.get(i);
       invoke("emailSubscribeWithoutData", bean);
       this.sleep(1000);
     }

    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    status = "finished";
   }
  };
  
  task.start();
  writeOutStr("success");
 }
 
 public boolean emailSubscribeWithoutData(EmailBean email) throws Exception {
  
  String toAddress = "";//邮箱地址
  String subject = "test";
  String content = "testdd";
  boolean sendStatus;
  sendStatus = emailSender(toAddress,subject,content);
       
  return sendStatus;
 }
 
 
 public String checkSendingStatus() throws Exception{
  System.out.println("checkSendStatus info --");
  writeOutStr(status);
  return null;
 }
 public void invoke(String method, EmailBean bean){
  int total=100;
  count++;
  Boolean result = false;
  try{
   Method m = instance.getClass().getMethod(method, EmailBean.class);
   result = (Boolean)m.invoke(instance, bean);
   if(!result){
    failCount++;
   }
  }
  catch(Exception e){
   failCount++;
   e.printStackTrace();
  }
  status = "" + count + "-" + total + "-" + failCount;
 }
 
 public boolean emailSender(String toAddress,String subject,String content){
  MailSenderInfo configBean = ConfigEmailReader.readEmailConfig();
  
  configBean.setToAddress(toAddress);    
  configBean.setSubject(subject);    
  configBean.setContent(content);    
         //这个类主要来发送邮件   
      SimpleMailSender sms = new SimpleMailSender();   
      return sms.sendHtmlMail(configBean);//发送html格式 
//  return true;
      
 }

  

以上只是部分测试代码,比较凌乱,邮件发送相关方法再次省略,欢迎各位大神批评指正。

 

 

 

 

你可能感兴趣的:(邮件,动态进度条)