我已经设法将我的log4j消息附加到javafx textarea组件,但是如果运行后台应用程序任务,GUI就会冻结。
因此实施中的某些内容丢失或配置错误。
这是我的自定义log4j appender:
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冻结的原因是什么?
在后台应用程序中运行多个任务/进程以及应用程序冻结的操作是在调用以下类及其方法时:
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>();
当侦听器(侦听传入的dicom图像关联请求收到此类)时,在后台调用此类及其方法。
我不知道此代码是否有帮助,但应用程序行为如下所示:
我希望以上所有信息都有帮助...
答案 0 :(得分:1)
TextArea不适用于大量文本,因为它由一个大文本节点支持。你最好使用其中一个虚拟控件,例如ListView,如果它不可编辑或者有一个StyledText控件可用。
最常用的是: