Multithreading Wicket:没有应用程序连接到当前线程

Multithreading Wicket:没有应用程序连接到当前线程,multithreading,wicket,Multithreading,Wicket,我想从桌面应用程序登录到wicket网页。因此,我希望定期检查是否存在某些特定参数 但当我运行代码时,会引发以下异常 Exception in thread "Thread-13" org.apache.wicket.WicketRuntimeException: There is no application attached to current thread Thread-13 这是我的代码: public class SignIn extends WebPage { public Si

我想从桌面应用程序登录到wicket网页。因此,我希望定期检查是否存在某些特定参数

但当我运行代码时,会引发以下异常

Exception in thread "Thread-13" org.apache.wicket.WicketRuntimeException: There is no application attached to current thread Thread-13
这是我的代码:

public class SignIn extends WebPage {
public SignIn()
{

    // Create feedback panel and add to page
    add(new FeedbackPanel("feedback"));

    // Add sign-in form to page
    add(new SignInForm("signInForm"));
    startLoginChecker(new LoginRunnable());

}

/**
 * Sign in form
 */
public final class SignInForm extends Form<Void>
{
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";

    // El-cheapo model for form
    private final ValueMap properties = new ValueMap();

    /**
     * Constructor
     *
     * @param id
     *            id of the form component
     */
    public SignInForm(final String id)
    {
        super(id);

        // Attach textfield components that edit properties map model
        add(new TextField<String>(USERNAME, new PropertyModel<String>(properties, USERNAME)));
        add(new PasswordTextField(PASSWORD, new PropertyModel<String>(properties, PASSWORD)));
    }

    /**
     * @see org.apache.wicket.markup.html.form.Form#onSubmit()
     */
    @Override
    public final void onSubmit()
    {
        // Get session info
        SignInSession session = getMySession();

        // Sign the user in
        if (session.signIn(getUsername(), getPassword()))
        {

                setResponsePage(getApplication().getHomePage());

        }
        else
        {
            // Get the error message from the properties file associated with the Component
            String errmsg = getString("loginError", null, "Unable to sign you in");

            // Register the error message with the feedback panel
            error(errmsg);
        }
    }

    /**
     * @return
     */
    private String getPassword()
    {
        return properties.getString(PASSWORD);
    }

    /**
     * @return
     */
    private String getUsername()
    {
        return properties.getString(USERNAME);
    }

    /**
     * @return
     */
    private SignInSession getMySession()
    {
        return (SignInSession)getSession();
    }
}

public void startLoginChecker(Runnable runnable) {
    Thread loginThread = new Thread(runnable);
    loginThread.start();
}

private class LoginRunnable implements Runnable {
    private int checkLoginPeriod = 1000; //1sec in milliseconds
    @Override
    public void run() {
        try {
            Thread.sleep(checkLoginPeriod);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        final SignInSession session = (SignInSession)getSession();

        while(!session.isSignedIn()) {
            if (session.signIn("wicket", "wicket"))

            {

                setResponsePage(getApplication().getHomePage());

            } else

            {
                // Get the error message from the properties file associated with the Component
                String errmsg = getString("loginError", null, "Unable to sign you in");

                // Register the error message with the feedback panel
                error(errmsg);
            }
        }

    }

}
公共类登录扩展网页{
公共签名
{
//创建反馈面板并添加到页面
添加(新反馈面板(“反馈”);
//将登录表单添加到页面
添加(新的SignInfo(“SignInfo”));
startLoginChecker(新登录名());
}
/**
*签到表
*/
公共最终类SignInfo扩展表单
{
私有静态最终字符串USERNAME=“USERNAME”;
私有静态最终字符串PASSWORD=“PASSWORD”;
//形式的El cheapo模型
私有最终ValueMap属性=新ValueMap();
/**
*建造师
*
*@param-id
*表单组件的id
*/
public signinfo(最终字符串id)
{
超级(id);
//附加用于编辑地图模型特性的textfield组件
添加(新文本字段(用户名,新属性模型(属性,用户名));
添加(新密码文本字段(密码,新属性模型(属性,密码));
}
/**
*@see org.apache.wicket.markup.html.form.form#onSubmit()
*/
@凌驾
提交时公开最终作废()
{
//获取会话信息
SigningSession会话=getMySession();
//让用户登录
if(session.sign(getUsername(),getPassword()))
{
setResponsePage(getApplication().getHomePage());
}
其他的
{
//从与组件关联的属性文件中获取错误消息
String errmsg=getString(“登录错误”,null,“无法登录”);
//向反馈面板注册错误消息
错误(errmsg);
}
}
/**
*@返回
*/
私有字符串getPassword()
{
返回properties.getString(密码);
}
/**
*@返回
*/
私有字符串getUsername()
{
返回properties.getString(用户名);
}
/**
*@返回
*/
私人签名会话getMySession()
{
返回(SigningSession)getSession();
}
}
公共空数据库(可运行可运行){
线程登录读取=新线程(可运行);
loginThread.start();
}
私有类LoginRunnable实现可运行{
private int checkLoginPeriod=1000;//1秒(毫秒)
@凌驾
公开募捐{
试一试{
线程睡眠(checkLoginPeriod);
}捕捉(中断异常e){
e、 printStackTrace();
}
最终登录会话=(登录会话)getSession();
而(!session.isSignedIn()){
if(会话签名(“wicket”、“wicket”))
{
setResponsePage(getApplication().getHomePage());
}否则
{
//从与组件关联的属性文件中获取错误消息
String errmsg=getString(“登录错误”,null,“无法登录”);
//向反馈面板注册错误消息
错误(errmsg);
}
}
}
}
}

如何避免此问题,并定期从线程检查多个参数


提前感谢。

我认为您应该使用
AjaxSelfUpdatengTimerBehavior
来实现所需的功能。在这种情况下(因为您正在移动到另一个页面),我认为更适合使用其超类:
AbstractAjaxTimerBehavior

只需将此添加到您的页面/面板

  add(new AbstractAjaxTimerBehavior(Duration.seconds(1)){

      protected void onTimer(AjaxRequestTarget target)
      {
          // Get session info
          SignInSession session = getMySession();              
          // Sign the user in
          if (session.signIn(getUsername(), getPassword()))
          {
              setResponsePage(getApplication().getHomePage());
          }
      }
  });
我想是的:

将页面中的(应用程序)转到线程池,在池添加处,如果:

if (!Application.exists()) {
    ThreadContext.setApplication(application);
}

我不知道你为什么想要这条线。。它将做什么?您好,谢谢您的提示,但似乎onTimer方法是最终的,不能被覆盖?