WebApplication

时间:2016-10-14 18:29:52

标签: serialization websocket wicket

我正在继续使用WebSocketBehavior进行日志记录行为。它当前将正确的数据记录到控制台,但也会引发可怕的序列化错误。这是因为我提供WicketApplication本身作为行为的构造函数参数。我已经尝试将它传递给我的会话对象并使用它来获取WebApplication,但它始终返回null。广播公司对象需要应用程序才能正常运行。我的问题是如何在避免令人讨厌的序列化错误的同时为行为提供WebApplication?这是我的行为类:

    public class LogWebSocketBehavior extends WebSocketBehavior implements Serializable {

private static final long serialVersionUID = 1L;

private Console console;
private Handler logHandler;
private Model<LogRecord> model = new Model<>();
private WebApplication application;

public LogWebSocketBehavior(Console console, WebApplication application) {
    super();
    configureLogger();
    this.console = console;
    this.application = application;


}

private void configureLogger() {

    Enumeration<String> list = LogManager.getLogManager().getLoggerNames();

    list.hasMoreElements();

    Logger l = Logger.getLogger(AppUtils.loggerName);
    l.addHandler(getLoggerHandler());

}

@Override
protected synchronized void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) {
    LogRecord r = model.getObject();
    sendRecordToConsole(handler, r);
}

private Handler getLoggerHandler() {
    return new LogHandler() {

        private static final long serialVersionUID = 1L;

        @Override
        public void publish(LogRecord record) {
            model.setObject(record);

            sendToAllConnectedClients("data");
        }
    };
}

private synchronized void sendToAllConnectedClients(String message) {

        IWebSocketConnectionRegistry registry = new SimpleWebSocketConnectionRegistry();
        WebSocketPushBroadcaster b = new WebSocketPushBroadcaster(registry);


        b.broadcastAll(application, new Message());

}

private void sendRecordToConsole(WebSocketRequestHandler handler, LogRecord r) {

    Level level = r.getLevel();

    if (level.equals(Level.INFO)) {
        console.info(handler, new SimpleFormatter().formatMessage(r));
    } else {
        console.error(handler, new SimpleFormatter().formatMessage(r));
    }
}

class Message implements IWebSocketPushMessage {

    public Message() {

    }

}

}

以下是用于显示消息的面板:

public class FooterPanel extends Panel {

private static final long serialVersionUID = 1L;

private Form form;
private Console console;

public FooterPanel(String id) {
    super(id);
}

@Override
public void onInitialize() {
    super.onInitialize();
    form = new Form("form");
    form.add(console = getConsole("feedback_console"));
    console.setOutputMarkupId(true);
    form.setOutputMarkupId(true);
    add(form);

    add(getLoggingBehavior());

}

private Console getConsole(String id) {
    return new Console(id) {

        private static final long serialVersionUID = 1L;

    };
}

private WebSocketBehavior getLoggingBehavior() {

    return new LogWebSocketBehavior(console, this.getWebApplication());

}

}

我按如下方式更新了我的行为:

    public class LogWebSocketBehavior extends WebSocketBehavior implements Serializable {

private static final long serialVersionUID = 1L;

private Console console;
private Handler logHandler;
private Model<LogRecord> model = new Model<>();

public LogWebSocketBehavior(Console console) {
    super();
    configureLogger();
    this.console = console;

}

private void configureLogger() {

    Enumeration<String> list = LogManager.getLogManager().getLoggerNames();

    list.hasMoreElements();

    Logger l = Logger.getLogger(AppUtils.loggerName);
    l.addHandler(getLoggerHandler());

}

@Override
protected synchronized void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) {
    LogRecord r = model.getObject();
    sendRecordToConsole(handler, r);
}

private Handler getLoggerHandler() {
    return new LogHandler() {

        private static final long serialVersionUID = 1L;

        @Override
        public void publish(LogRecord record) {
            model.setObject(record);

            sendToAllConnectedClients("data");
        }
    };
}

private synchronized void sendToAllConnectedClients(String message) {

    WebApplication application = WebApplication.get();
    IWebSocketConnectionRegistry registry = new SimpleWebSocketConnectionRegistry();
    WebSocketPushBroadcaster b = new WebSocketPushBroadcaster(registry);

    b.broadcastAll(application, new Message());

}

private void sendRecordToConsole(WebSocketRequestHandler handler, LogRecord r) {

    Level level = r.getLevel();
    String message = AppUtils.consoleDateTimeFormat.format(LocalDateTime.now()) + " - " + AppUtils.LogFormatter.formatMessage(r);
    if (level.equals(Level.INFO)) {
        console.info(handler, message);
    } else {
        console.error(handler, message);
    }
}

class Message implements IWebSocketPushMessage {

    public Message() {

    }

}

}

我回到了我开始时的原始问题,这是以下错误:

错误 - ErrorLogger - 作业(report.DB5E002E046235586592E7E984338DEE3:653引发异常。 org.quartz.SchedulerException:

Job抛出了未处理的异常。 [请参阅嵌套异常:org.apache.wicket.WicketRuntimeException:当前线程DefaultQuartzScheduler_Worker-1没有附加应用程序]     在org.quartz.core.JobRunShell.run(JobRunShell.java:213)

at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

引起:org.apache.wicket.WicketRuntimeException:当前线程没有附加应用程序DefaultQuartzScheduler_Worker-1     在org.apache.wicket.Application.get(Application.java:236)

at org.apache.wicket.protocol.http.WebApplication.get(WebApplication.java:160)

at eb.wicket.behaviors.LogWebSocketBehavior.sendToAllConnectedClients(LogWebSocketBehavior.java:77)

at eb.wicket.behaviors.LogWebSocketBehavior.access$100(LogWebSocketBehavior.java:29)

at eb.wicket.behaviors.LogWebSocketBehavior$1.publish(LogWebSocketBehavior.java:70)

最后按照需要工作..这是行为类:

public class LogWebSocketBehavior extends WebSocketBehavior implements Serializable {

private static final long serialVersionUID = 1L;

private Console console;
private Model<LogRecord> model = new Model<>();

public LogWebSocketBehavior(Console console) {
    super();
    configureLogger();
    this.console = console;

}

private void configureLogger() {

    Enumeration<String> list = LogManager.getLogManager().getLoggerNames();

    list.hasMoreElements();

    Logger l = Logger.getLogger(AppUtils.loggerName);
    l.addHandler(getLoggerHandler());

}

@Override
protected synchronized void onPush(WebSocketRequestHandler handler, IWebSocketPushMessage message) {
    LogRecord r = model.getObject();
    sendRecordToConsole(handler, r);
}

private Handler getLoggerHandler() {
    return new LogHandler() {

        private static final long serialVersionUID = 1L;

        @Override
        public void publish(LogRecord record) {
            model.setObject(record);

            sendToAllConnectedClients("data");
        }
    };
}

private synchronized void sendToAllConnectedClients(String message) {

    IWebSocketConnectionRegistry registry = new SimpleWebSocketConnectionRegistry();
    WebSocketPushBroadcaster b = new WebSocketPushBroadcaster(registry);
    b.broadcastAll(Application.get("eb.wicket.MyWicketFilter"), new Message());

}

private void sendRecordToConsole(WebSocketRequestHandler handler, LogRecord r) {

    Level level = r.getLevel();
    String message = AppUtils.consoleDateTimeFormat.format(LocalDateTime.now()) + " - " + AppUtils.LogFormatter.formatMessage(r);
    if (level.equals(Level.INFO)) {
        console.info(handler, message);
    } else {
        console.error(handler, message);
    }
}

class Message implements IWebSocketPushMessage {

    public Message() {

    }

}

}

1 个答案:

答案 0 :(得分:2)

而不是保留对应用程序的引用,只需在需要时查找它:Application.get()

更新问题后,我们可以看到:

 Caused by: org.apache.wicket.WicketRuntimeException: 
 There is no application attached to current thread DefaultQuartzScheduler_Worker-1

这解释了它 - 这是一个由Quartz启动的线程,它不是一个http线程。

解决此问题的唯一方法是使用Application.get(String)。该值应为应用程序名称(Application#getName()),该名称在web.xml中指定为<filter-name>的值。

通过这种方式,您可以获取Application实例,但是如果您需要,也无法对Session和/或RequestCycle执行相同的操作。