コンシューマ/プロデューサデザインを使用して作成したソケットサーバーを実行しているときに、この問題が発生し、エラーがログでエラーcpu time limit exceeded
でクラッシュしました。私もcpu
の使用が90%
以上であることがわかりました。ここではサーバーのコードは、何が間違って行くことができ、どのように私はこれを最適化することができますか?コンシューマ/プロデューサデザインを使用するTCPソケットサーバー
私はこのリクエストを毎回threads
という非常に多く作成することを避けるために、このqueue
というアプローチを使用しました。 mainメソッドで
(メインスレッド)
//holds socket instances
ConcurrentLinkedQueue<Socket> queue = new ConcurrentLinkedQueue<>();
//create producer thread
Thread producer = new Thread(new RequestProducer(queue));
//create consumer thread
Thread consumer = new Thread(new RequestConsumer(queue));
producer.start();
consumer.start();
RequestProducerスレッド
//this holds queue instance coming from main thread
ConcurrentLinkedQueue<Socket> queue
//constructor, initiate queue
public RequestProducer(
ConcurrentLinkedQueue<Socket> queue
) {
this.queue = queue;
}
public void run() {
try {
//create serversocket instance on port 19029
ServerSocket serverSocket = new ServerSocket(19029);
while (true) {
try {
//keep accept connections
Socket socket = serverSocket.accept();
//add socket to queue
queue.offer(socket);
} catch (ConnectException ce) {//handle exception
} catch (SocketException e) {//handle exception
}
}
} catch (IOException ex) {//handle exception}
}
RequestConsumerスレッド
//this holds queue instance coming from main thread, same as requestproducer
ConcurrentLinkedQueue<Socket> queue
//constructor, initiate queue
public RequestConsumer(
ConcurrentLinkedQueue<Socket> queue
) {
this.queue = queue;
}
public void run() {
try {
Socket socket = null;
while (true) {
//get head of the queue (socket instance)
socket = queue.poll();
if (null != socket) {
//process data stream
String in = DataStreamUtil.parseAsciiSockStream(socket.getInputStream());
//close socket conection
socket.close();
//excecute database insert of processed data
excecuteDbInsert(in);
}
}
} catch (IOException | ParseException ex) {//handle exceptions}
}
データストリームパーサ
public static String parseAsciiSockStream(InputStream in) throws IOException {
StringBuilder builder = new StringBuilder();
if (null != in) {
byte[] b = new byte[BYTE_STREAM_MAX];
int length = in.read(b);
for (int i = 0; i < length; i++) {
builder.append((char) (int) b[i]);
}
in.close();
}
return builder.toString();
}
Javaプログラムにはありません'エラーが発生してクラッシュするCPUの時間制限を超えました'。これはPHPのエラーメッセージです。あなたが何を求めているのか不明です。 – EJP
私はこのプログラムを 'java -jar GpsServer.jar> /var/log/gps-server-log.log 2>&1'コマンドで起動しました。数分後、このサーバが停止してこのログファイルの最後の行が' CPU時間制限を超えました。また、サービスとしてサーバを起動するために 'upstart conf'ファイルを書きました。 –
@EJPこれは間違いなくPHPのエラーメッセージではなく、Javaでも発生します。 LinuxではSignal 24(SIGXCPU)です。 – F0G