2016-09-21 3 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는 사용하지 않았습니다.

답변

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