2009-09-15 5 views
1

Main 메서드를 통해 다음 코드를 실행할 때 제대로 작동하지만 스윙 단추를 클릭 할 때 실행하려고하면 응답하지 않는다.스윙을 통해 LDAPConnection을 가져올 때 응답하지 않는다.

import java.util.Hashtable; 

import javax.naming.AuthenticationException; 
import javax.naming.Context; 
import javax.naming.NamingException; 
import javax.naming.directory.DirContext; 
import javax.naming.directory.InitialDirContext; 

public class SimpleLdapAuthentication { 
    public static void main(String[] args) { 
     String username = "user"; 
     String password = "password"; 
     String base = "ou=People,dc=objects,dc=com,dc=au"; 
     String dn = "uid=" + username + "," + base; 
     String ldapURL = "ldap://ldap.example.com:389"; 

     // Setup environment for authenticating 

     Hashtable<String, String> environment = new Hashtable<String, String>(); 
     environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     environment.put(Context.PROVIDER_URL, ldapURL); 
     environment.put(Context.SECURITY_AUTHENTICATION, "simple"); 
     environment.put(Context.SECURITY_PRINCIPAL, dn); 
     environment.put(Context.SECURITY_CREDENTIALS, password); 

     try { 
      DirContext authContext = 
      new InitialDirContext(environment); 

      // user is authenticated 
     } catch (AuthenticationException ex) { 

      // Authentication failed 

     } catch (NamingException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

답변

1

그것이 정말 끊지 않습니다 도와주세요, 아니면 그냥 돌아 오는 데 시간이 오래 걸릴?

스윙 이벤트 처리기에서 많은 처리를하는 것은 좋지 않습니다. 스윙이 사용자에게 반응해야하기 때문입니다. 장시간 실행되는 작업은 다른 스레드에 위임해야합니다.

+0

이미 스레드를 사용하여 LDAP 작업을 수행하고 있습니다. 다른 아이디어있어? –

+0

아니요. 코드가 작동하지 않는 코드를 게시해야한다고 생각합니다. –

+0

코드는 다음과 같습니다. 나는 위의 main 메소드 코드를 SimpleLdapAuthentication 클래스의 getT() 메소드에 넣었다. 개인 무효 jButton1MouseClicked (java.awt.event.MouseEvent의의 EVT) { // TODO가 여기에 처리 코드를 추가 SimpleLdapAuthentication의 SLA = 새로운 SimpleLdapAuthentication을(); sla.getT(); } –

0

올바른이 시도한 코드 중 하나입니다 ... 그리고 이것은 AWT 이벤트 수신기입니다. 그러나 이런 방식으로 문제를 실행하는 것이 무엇인지 말해주십시오. LDAPConnection이 main 메소드와 함께 작동하면 왜 이벤트 리스너와 함께 작동합니까? 나는 많은 포럼 해상도

0

사용하는 ActionListener 대신의 MouseListener를 얻을 수 없습니다에 유사한 게시물을 본

btnYourLdapButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ev) { 
      doLdapRequest(ev); 
     } 
    }); 
0

당신은 이벤트 리스너 내부의 코드가 실행되지 것이라는 점을 기억해야합니다 별도의 스레드. 이 코드는 항상 EDT (Event Dispatch Thread) 내에서 실행되며 해당 코드가 실행되는 동안 다른 GUI 업데이트가 불가능하며 응용 프로그램이 멈춰있는 것처럼 보입니다. 더 많은 것을 이해하려면 Writing Responsive User Interfaces with Swing을 읽으십시오. 다음은 다른 스레드로 작업을 전달하는 방법을 보여주는 매우 어려운 예제 코드입니다. 나는 LDAP 호출을 삽입 할 수있는 공간도 남겨 두었습니다!

package examples; 

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 

import javax.swing.AbstractAction; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class EDTSeparation { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame f = new JFrame(); 
       f.setLayout(new GridLayout(2, 1)); 

       final JLabel label = new JLabel(""); 
       f.add(label); 
       f.add(new JButton(new AbstractAction("Do Task") { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         // This method will be executed on the EDT 
         label.setText("Working..."); 

         // Create a new Thread to do the long-running task so this method can finish quickly. 
         Thread t = new Thread(new LDAPTask(label)); 
         t.setDaemon(true); 
         t.start(); 
        } 
       })); 

       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.pack(); 
       f.setVisible(true); 
      } 
     }); 
    } 

    private static class LDAPTask implements Runnable { 
     private final JLabel label; 

     public LDAPTask(JLabel label) { 
      this.label = label; 
     } 

     @Override 
     public void run() { 
      try { 
       // Use sleep to simulate something taking a long time. 
       // Replace this your long-running method call. 
       Thread.sleep(1000); 

       // If you need to handle GUI components, it must be done on the EDT 
       SwingUtilities.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
         label.setText("Done."); 
        } 
       }); 
      } catch (InterruptedException e) { 
      } 
     } 
    } 
}
관련 문제