当我将 log4j 附加到 textarea 时,JavaFX 应用程序冻结

JavaFX application freeze when i append log4j to textarea

我已经设法将我的 log4j 消息附加到 javafx textarea 组件,但是如果后台应用程序任务 运行s,GUI 会冻结。

所以实现中的某些内容丢失或配置错误。

这是我的自定义 log4j 附加程序:

public class TextAreaAppender extends WriterAppender {

    static Logger log = Logger.getLogger(TextAreaAppender.class.getName());

    private static volatile TextArea textArea = null;

    public static void setTextArea(final TextArea textArea) {
        TextAreaAppender.textArea = textArea;
    }

    @Override
    public void append(final LoggingEvent loggingEvent) {

        final String message = this.layout.format(loggingEvent);

        try {
            Platform.runLater(() -> {
                try {
                    if (textArea != null) {
                        if (textArea.getText().length() == 0) {
                            textArea.setText(message);
                        } else {
                            textArea.selectEnd();
                            textArea.insertText(textArea.getText().length(),
                                message);
                        }
                    }
                } catch (final Throwable t) {
                    System.out.println("Unable to append log to text area: "
                        + t.getMessage());
                }
            });
        } catch (final IllegalStateException e) {
            // ignore case when the platform hasn't yet been iniitialized
        }
    }

下面是我如何将 textarea 组件插入到我的 fxml 控制器中:

public class FXMLDocumentController implements Initializable {
    ...
    @FXML
    private TextArea logText;
    ...
    @Override
    @FXML
    public void initialize(URL url, ResourceBundle rb) {
        ...
        logText.setEditable(false);
        TextAreaAppender.setTextArea(logText);
    }

最后是我的 log4j 配置:

log4j.rootLogger=INFO, file, textarea, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Append the logs to the GUI
log4j.appender.textarea = com.npap.fxutils.TextAreaAppender
log4j.appender.textarea.Target=System.out
log4j.appender.textarea.layout=org.apache.log4j.PatternLayout
log4j.appender.textarea.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

GUI 冻结的原因可能是什么?

在后台应用程序 运行 中有几个 tasks/processes 并且当调用以下 class 及其方法时,应用程序冻结的操作是:

class StorageSCP extends StorageService  implements AssociationListener {
    ...
    private final DcmRcv dcmrcv;

     public StorageSCP(DcmRcv dcmrcv, String[] sopClasses) {
         super(sopClasses);
         this.dcmrcv = dcmrcv;
     }
     ...
    /** Overwrite {@link StorageService#cstore} to send delayed C-STORE RSP 
    * by separate Thread, so reading of following received C-STORE RQs from
    * the open association is not blocked.
    */
    @Override
    public void cstore(final Association as, final int pcid, DicomObject rq,
        PDVInputStream dataStream, String tsuid)
        throws DicomServiceException, IOException {

        final DicomObject rsp = CommandUtils.mkRSP(rq, CommandUtils.SUCCESS);
        onCStoreRQ(as, pcid, rq, dataStream, tsuid, rsp);

        if (dcmrcv.getDimseRspDelay() > 0) {
            dcmrcv.executor().execute(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(dcmrcv.getDimseRspDelay());
                        as.writeDimseRSP(pcid, rsp);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } else {
            as.writeDimseRSP(pcid, rsp);
        }

        onCStoreRSP(as, pcid, rq, dataStream, tsuid, rsp);
    }
     ...
     @Override
     protected void onCStoreRQ(Association association, int pcid, DicomObject dcmReqObj,
                                PDVInputStream dataStream, String transferSyntaxUID,
                                DicomObject dcmRspObj)
            throws DicomServiceException, IOException {
         final DicomOutputStream outStream = new DicomOutputStream(new BufferedOutputStream(new FileOutputStream(dicomFile), 600000));

        try {
            outStream.writeFileMetaInformation(fileMetaDcmObj);
            dataStream.copyTo(outStream);            
        } catch (DicomServiceException e) {
        } finally {
            outStream.close();  
        }
        ...
    }
    ...
    @Override
    public void associationAccepted(final AssociationAcceptEvent associationAcceptEvent) {
        final UUID assocUUID = UUID.randomUUID();
        final Association association = associationAcceptEvent.getAssociation();
        associationDataMap.put(association, assocUUID);
    }

    @Override
    public void associationClosed(final AssociationCloseEvent associationCloseEvent) {
        final Association association = associationCloseEvent.getAssociation();
        associationDataMap.remove(association);
        final Integer assocInstanceCnt = associationCounterMap.get(association);
    removeAssociationCounter(association);
    }

    private final Map<Association, UUID> associationDataMap = new HashMap<Association, UUID>();
    private final Map<Association, Integer> associationCounterMap = new HashMap<Association, Integer>();

这个 class 及其方法在侦听器(侦听传入的 dicom 图像关联请求收到此类请求)时在后台调用。

我不知道这段代码是否有帮助,但是应用程序行为如下所示:

  1. 应用程序启动正常,GUI 工作正常(可以切换选项卡,编辑 领域等)。我还可以在我的 log4j textarea
  2. 中看到它的日志
  3. 当我收到一些传入的 dicom 图像进行解析时(关联 请求)然后整个应用程序冻结。
  4. 在后台应用是运行正常,没有异常 抛出和任务 运行 没有任何问题
  5. 即使所有关联请求都结束(接受并处理) GUI 保持冻结状态。

我希望以上所有信息对您有所帮助...

TextArea 不适合大量文本,因为它由一个大文本节点支持。如果 ListView 不可编辑或 StyledText 控件之一可用,则最好使用其中一种虚拟控件,例如 ListView。

最常用的是: