当我通过Swing执行获取LDAPConnection时,它会挂起

时间:2009-09-15 07:15:18

标签: java multithreading swing ldap connection

当我通过Main方法运行以下代码时,它工作正常,但当我尝试单击swing按钮执行它时,它会挂起。

请帮忙

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

4 个答案:

答案 0 :(得分:1)

它真的挂起,还是只需要很长时间才能回来?

在Swing事件处理程序中进行大量处理并不是一个好主意,因为Swing需要响应用户。您应该将长时间运行的操作委托给另一个线程。

答案 1 :(得分:0)

正确这是我尝试过的代码中的一个......这是一个AWT事件监听器。但是告诉我这样做的问题是什么。如果LDAPConnection与main方法一起使用,为什么它不能与事件监听器一起使用?我在许多论坛上看过类似的帖子而无法获得解决方案

答案 2 :(得分:0)

使用ActionListener而不是MouseListener

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

答案 3 :(得分:0)

您需要记住,事件侦听器中的代码不会在单独的线程中运行。它将始终在事件调度线程(EDT)内运行,并且只要该代码正在运行,就不可能有其他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) {
            }
        }
    }
}