Javamail의 AuthenticationFailedException 예외
2419 단어 다중 스레드
public class MyAuthenticator extends Authenticator {
private String strUser;
private String strPswd;
/**
* Initial the authentication parameters.
*
* @param username
* @param password
*/
public MyAuthenticator(String username, String password) {
this.strUser = username;
this.strPswd = password;
}
/**
* @return
*/
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPswd);
}
}
그리고 세션에서 이 종류를 사용하는 실례.
Properties props = null;
Session session = null;
props = System.getProperties();
props.put("mail.pop3.host", host);
props.put("mail.pop3.auth", "true");
props.put("mail.pop3.port", port);
Authenticator auth = new MyAuthenticator(userName, password);
session = Session.getDefaultInstance(props, auth);
Store store = session.getStore("pop3");
store.connect();
...
이렇게 하면 우리는 My Authenticator를 통해 사용자의 검증 정보를 저장할 수 있다.
단일 스레드를 사용하여 실행하면 위 코드가 올바르게 실행될 수 있지만 다중 스레드 환경에서 이 코드는 Authenticatio inFailed Exception을 일으킬 수 있습니다
왜냐하면session과props는 하나의 독립된 실례가 아니기 때문에 다중 노드가 있을 때 서로 영향을 미친다. 특히 서로 다른 POP3 서버를 읽을 때
이때 우리는 2단 코드에 대해 약간의 변경을 해야 한다
Properties props = null;
Session session = null;
// props = System.getProperties();
props = new Properties();
props.put("mail.pop3.host", host);
props.put("mail.pop3.auth", "true");
props.put("mail.pop3.port", port);
Authenticator auth = new MyAuthenticator(userName, password);
// session = Session.getDefaultInstance(props, auth);
session = Session.getInstance(props, auth);
Store store = session.getStore("pop3");
store.connect();
...
new Properties() 및 session.Instance(props,auth)는 모든 스레드 간의 실례가 독립적임을 확보하고 다중 스레드 운행 환경에서 자원이 충돌하지 않도록 하여AutherticationFailedException의 발생을 피할 수 있다.
(지적 및 보완을 환영합니다)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약 동료는 무심결에 이 문제를 제기하고 두 가지 방법을 직접 실천했다.물론 더 좋은 방법이 있을 거야. 방법 1 이런 방법은 비교적 흔히 볼 수 있는 해결 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.