view analyzer/fx/src/main/java/jp/co/ntt/oss/heapstats/fx/plugin/builtin/jvmlive/errorreporter/ErrorReportServer.java @ 272:dd85c1cbc8c8

Bug 3752: Migrate to OpenJFX 13 Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/144
author Yasumasa Suenaga <yasuenag@gmail.com>
date Fri, 27 Sep 2019 14:47:03 +0900
parents
children
line wrap: on
line source

/*
 * Copyright (C) 2014-2019 Yasumasa Suenaga
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package jp.co.ntt.oss.heapstats.fx.plugin.builtin.jvmlive.errorreporter;

import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;

/**
 * Service thread to receive crash reports.
 * This thread can treat data which is generated by -XX:+TransmitErrorReport and -XX:ErrorReportServer=&lt;address&gt;:&lt;port&gt; .
 * 
 * @author Yasumasa Suenaga
 */
public class ErrorReportServer extends Task<Void>{
    
    public static final int DEFAULT_ERROR_REPORT_SERVER_PORT = 4711;
    
    private final int port;
    
    private final ObservableList<ErrorReportDecoder> crashList;
    
    private final ExecutorService hsErrProcPool;
    
    /**
     * Constructor of ErrorReportServer.
     * 
     * @param crashList List of crash jvms.
     * @param hsErrProcPool ThreadPool which processes ErrorReport.
     */
    public ErrorReportServer(ObservableList<ErrorReportDecoder> crashList, ExecutorService hsErrProcPool){
        this(DEFAULT_ERROR_REPORT_SERVER_PORT, crashList, hsErrProcPool);
    }
    
    /**
     * Constructor of ErrorReportServer.
     * @param port Port number of ErrorReportServer.
     * @param crashList List of crash jvms.
     * @param hsErrProcPool ThreadPool which processes ErrorReport.
     */
    public ErrorReportServer(int port, ObservableList<ErrorReportDecoder> crashList, ExecutorService hsErrProcPool){
        this.port = port;
        this.crashList = crashList;
        this.hsErrProcPool = hsErrProcPool;
    }
    
    @Override
    protected Void call() throws Exception {
        
        try(AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open()){
            server.bind(new InetSocketAddress(port));
            
            while(!isCancelled()){
                Future<AsynchronousSocketChannel> sock = server.accept();
                hsErrProcPool.submit(new ErrorReportDecoder(crashList, sock.get()));
            }
            
        }
        
        return null;
    }
    
}