Java 当我通过Swing执行get-LDAPConnection时,它将挂起
当我通过Main方法运行下面的代码时,它工作得很好,但当我试图在单击swing按钮时执行它时,它挂起 请帮忙Java 当我通过Swing执行get-LDAPConnection时,它将挂起,java,multithreading,swing,ldap,connection,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.
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();
}
}
}
import java.util.Hashtable;
导入javax.naming.AuthenticationException;
导入javax.naming.Context;
导入javax.naming.NamingException;
导入javax.naming.directory.DirContext;
导入javax.naming.directory.InitialDirContext;
公共类SimpleDapAuthentication{
公共静态void main(字符串[]args){
字符串username=“user”;
字符串password=“password”;
String base=“ou=People,dc=objects,dc=com,dc=au”;
字符串dn=“uid=“+username+”,“+base;
字符串ldapURL=”ldap://ldap.example.com:389";
//设置用于身份验证的环境
Hashtable环境=新的Hashtable();
put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
put(Context.PROVIDER\u URL,ldapURL);
put(Context.SECURITY_身份验证,“simple”);
environment.put(Context.SECURITY_PRINCIPAL,dn);
环境.put(Context.SECURITY\u凭证、密码);
试一试{
DirContext authContext=
新环境;
//用户已通过身份验证
}捕获(AuthenticationException ex){
//身份验证失败
}捕获(NamingException-ex){
例如printStackTrace();
}
}
}
它真的挂起了,还是需要很长时间才能恢复
在Swing事件处理程序中进行大量处理不是一个好主意,因为Swing需要对用户做出响应。您应该将长时间运行的操作委派给另一个线程。它真的挂起了,还是需要很长时间才能恢复
在Swing事件处理程序中进行大量处理不是一个好主意,因为Swing需要对用户做出响应。您应该将长时间运行的操作委派给另一个线程。更正这是我尝试的代码之一。。。这是一个AWT事件侦听器。但是告诉我用这种方式执行的问题是什么。如果LDAPConnection与main方法一起工作,为什么不与事件侦听器一起工作?我在许多论坛上都看到过类似的帖子,但无法获得正确的解析,这是我尝试的代码之一。。。这是一个AWT事件侦听器。但是告诉我用这种方式执行的问题是什么。如果LDAPConnection与main方法一起工作,为什么不与事件侦听器一起工作?我在许多论坛上都看到过类似的帖子,但都无法获得解决方案使用ActionListener而不是MouseListener
btnYourLdapButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
doLdapRequest(ev);
}
});
使用ActionListener而不是MouseStener
btnYourLdapButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
doLdapRequest(ev);
}
});
您需要记住,事件侦听器中的代码不会在单独的线程中运行。它将始终在事件调度线程(EDT)内运行,只要代码在运行,就不可能进行其他GUI更新,应用程序看起来就像挂起了一样。阅读以了解更多。下面是一些非常粗略的示例代码,演示如何将任务交给另一个线程。我甚至给你留了一个空间来插入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) {
}
}
}
}
您需要记住,事件侦听器中的代码不会在单独的线程中运行。它将始终在事件调度线程(EDT)内运行,只要代码在运行,就不可能进行其他GUI更新,应用程序看起来就像挂起了一样。阅读以了解更多。下面是一些非常粗略的示例代码,演示如何将任务交给另一个线程。我甚至给你留了一个空间来插入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) {
}
}
}
}
我已经在使用线程执行ldap操作。你还有其他想法吗?没有。我认为你应该发布那些不起作用的代码,而不是那些真正起作用的代码。我已经将上述主要方法代码放在SimpleDapAuthentication类的getT()方法中。private void jButton1MouseClicked(java.awt.event.MouseEvent evt){//TODO在此处添加您的处理代码:simpleldapaauthentication sla=new simpleldapaauthentication();sla.getT();}Rajes,其他线程在哪里生成?如果我理解正确,这是Matis生成的代码。而jButton1MouseClicked是从AWT调用的。@rajesh-您只是在事件处理程序中执行此操作,不是吗?我已经在使用线程执行ldap操作了。你还有其他想法吗?没有。我认为你应该发布那些不起作用的代码,而不是那些真正起作用的代码。我已经将上述主要方法代码放在SimpleDapAuthentication类的getT()方法中。private void jButton1MouseClicked(java.awt.event.MouseEvent evt){//TODO在此处添加您的处理代码:simpleldapaauthentication sla=new simpleldapaauthentication();sla.getT();}Rajes,其他线程在哪里生成?如果我理解正确,这是Matis生成的代码。而jButton1MouseClicked是从AWT调用的。@rajesh-您只是在事件处理程序中执行它,不是吗?