2013-06-24 9 views
3

WSO2 제품을 통해 log4j.properties를 사용합니다. SMTPAppender를 사용하고 Gmail SMTP 서버를 사용하여 전자 메일 알림을 보내려면 appender를 구현해야했습니다. 그래서, 내가 log4j를 설정하고 ESB WSO2 서버를 시작할 때, 관리 콘솔은 다음을 인쇄합니다 : log4j:ERROR Could not instantiate class [com.notification.GmailSMTPAppender].ERROR 클래스를 인스턴스화 할 수 없습니다.

내 구현했다 :

package com.notification; 

public class GmailSMTPAppender extends SMTPAppender { 

    protected Session session; 

    public GmailSMTPAppender() { 
      super(); 
    } 

    /** 
    * Create mail session. 
    * 
    * @return mail session, may not be null. 
    */ 
    protected Session createSession() { 
      Properties props = new Properties(); 
      props.put("mail.smtps.host", getSMTPHost()); 
      props.put("mail.smtps.auth", "true"); 

      Authenticator auth = null; 
      if (getSMTPPassword() != null && getSMTPUsername() != null) { 
        auth = new Authenticator() { 
          protected PasswordAuthentication getPasswordAuthentication() { 
            return new PasswordAuthentication(getSMTPUsername(), 
                getSMTPPassword()); 
          } 
        }; 
      } 
      session = Session.getInstance(props, auth); 
      if (getSMTPProtocol() != null) { 
        session.setProtocolForAddress("rfc822", getSMTPProtocol()); 
      } 
      if (getSMTPDebug()) { 
        session.setDebug(getSMTPDebug()); 
      } 
      return session; 
    } 

    /** 
    * Send the contents of the cyclic buffer as an e-mail message. 
    */ 
    protected void sendBuffer() { 
      try { 
        MimeBodyPart part = new MimeBodyPart(); 

        StringBuffer sbuf = new StringBuffer(); 
        String t = layout.getHeader(); 
        if (t != null) 
          sbuf.append(t); 
        int len = cb.length(); 
        for (int i = 0; i < len; i++) { 
          LoggingEvent event = cb.get(); 
          sbuf.append(layout.format(event)); 
          if (layout.ignoresThrowable()) { 
            String[] s = event.getThrowableStrRep(); 
            if (s != null) { 
              for (int j = 0; j < s.length; j++) { 
                sbuf.append(s[j]); 
                sbuf.append(Layout.LINE_SEP); 
              } 
            } 
          } 
        } 
        t = layout.getFooter(); 
        if (t != null) 
          sbuf.append(t); 
        part.setContent(sbuf.toString(), layout.getContentType()); 

        Multipart mp = new MimeMultipart(); 
        mp.addBodyPart(part); 
        msg.setContent(mp); 

        msg.setSentDate(new Date()); 
        send(msg); 
      } catch (Exception e) { 
        LogLog.error("Error occured while sending e-mail notification.", e); 
      } 
    } 

    /** 
    * Pulled email send stuff i.e. Transport.send()/Transport.sendMessage(). So 
    * that on required this logic can be enhanced. 
    * 
    * @param msg 
    *   Email Message 
    * @throws MessagingException 
    */ 
    protected void send(Message msg) throws MessagingException { 
      SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); 
      try { 
        t.connect(getSMTPHost(), getSMTPUsername(), getSMTPPassword()); 
        t.sendMessage(msg, msg.getAllRecipients()); 
      } finally { 
        System.out.println("Response: " + t.getLastServerResponse()); 
        t.close(); 
      } 
    } 

} 

내 log4j.properties 구성에이 어 펜더의 인스턴스를 수 있습니까?

+1

전체 스택 추적을 첨부하면 도움이 될 수도 있습니다 – radai

+1

어디에서 병을 넣었습니까? –

+0

은 WSO2_ESB를 사용하는 라이브러리의 저장소에 있습니다. – user2514698

답변

0

Appender를 프로그래밍 방식으로 추가해야합니까? 다음을 확인하십시오 post

관련 문제