Javamail의 AuthenticationFailedException 예외

2419 단어 다중 스레드
Javamail 이 pop3 프로토콜로 메일을 받을 때, 우리는 Authenication 클래스를 만들어서 사용자 검증 정보를 저장할 수 있습니다
 
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의 발생을 피할 수 있다.
 
(지적 및 보완을 환영합니다)

좋은 웹페이지 즐겨찾기