2016-09-21 8 views
0

私はGroovyクラスで以下のコードを使用しています。このメソッドを他のさまざまなGroovyクラスから非同期的に呼び出す必要があります。groovyでの非同期電子メール通知

public void sendNotification(){ 

     //async true 
     String from = ApplicationConfig.email_From; 
     String sendTo = ApplicationConfig.email_To; 
     String host = ApplicationConfig.email_Host; 
     String subject = ApplicationConfig.email_Subject; 
     String textToSend = ApplicationConfig.email_Text; 

     Properties properties = System.getProperties(); 
     properties.setProperty("mail.smtp.host", host); 
     Session session = Session.getDefaultInstance(properties); 

     try{ 

      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(from)); 
      message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(sendTo)); 
      message.setSubject(subject); 
      message.setText(textToSend); 
      Transport.send(message); 

     }catch (MessagingException mex) { 
      mex.printStackTrace(); 
     } 
    } 

は、これまでのところ私は、Grailsの中にいくつかのプラグインがありますが、私の要件に合ったものを見つけることができませんでしたが、私はGrailsのを使用していませんよ。

答えて

2

ただExecutorServiceのに使用

ExecutorService pool = Executors.newFixedThreadPool(2) 

def sender = { -> 
    Properties properties = System.getProperties(); 
    properties.setProperty("mail.smtp.host", ApplicationConfig.email_Host); 
    Session session = Session.getDefaultInstance(properties); 
    try{ 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(ApplicationConfig.email_From)); 
     message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(ApplicationConfig.email_To)); 
     message.setSubject(ApplicationConfig.email_Subject); 
     message.setText(ApplicationConfig.email_Text); 
     Transport.send(message); 
    }catch (MessagingException mex) { 
     mex.printStackTrace(); 
    } 
} 

public void sendNotification() { 
    pool.submit(sender) 
} 
関連する問題