/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.twosigma.beakerx.kernel.threads;

import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.twosigma.beakerx.kernel.msg.MessageCreator;
import com.twosigma.beakerx.kernel.msg.MessageHolder;
import com.twosigma.beakerx.kernel.SocketEnum;

public class ExecutionResultSender implements Observer {

  public static Logger logger = LoggerFactory.getLogger(ExecutionResultSender.class);

  private final ConcurrentLinkedQueue<MessageHolder> messageQueue = new ConcurrentLinkedQueue<>();
  private AbstractThread workingThread;
  private KernelFunctionality kernel;

  public ExecutionResultSender(KernelFunctionality kernel) {
    this.kernel = kernel;
  }

  @Override
  public synchronized void update(Observable o, Object arg) {
    SimpleEvaluationObject seo = (SimpleEvaluationObject) o;
    if (seo != null) {
      messageQueue.addAll(MessageCreator.createMessage(seo));
      if (workingThread == null || !workingThread.isAlive()) {
        workingThread = new MessageRunnable();
        workingThread.start();
      }
    }
  }

  protected class MessageRunnable extends AbstractThread {

    @Override
    public boolean getRunning() {
      return running && !messageQueue.isEmpty();
    }

    @Override
    public void run() {
      while (getRunning()) {
        MessageHolder job = messageQueue.poll();
        if (job != null) {
          if (SocketEnum.IOPUB_SOCKET.equals(job.getSocketType())) {
            kernel.publish(job.getMessage());
          } else if (SocketEnum.SHELL_SOCKET.equals(job.getSocketType())) {
            kernel.send(job.getMessage());
          }
        }
      }
      logger.debug("MessageRunnable shutdown.");
    }
  }

  public void exit() {
    if (workingThread != null) {
      workingThread.halt();
    }
  }

}

@@ -662,16 +662,39 @@
 reads;%0A%0A
+import java.util.List;%0A
 import j
@@ -744,59 +744,8 @@
 ver;
-%0Aimport java.util.concurrent.ConcurrentLinkedQueue;
 %0A%0Aim
@@ -1223,148 +1223,8 @@
 ss);
-%0A%0A  private final ConcurrentLinkedQueue%3CMessageHolder%3E messageQueue = new ConcurrentLinkedQueue%3C%3E();%0A  private AbstractThread workingThread;
 %0A  p
@@ -1519,533 +1519,104 @@
     
-messageQueue.addAll(MessageCreator.createMessage(seo));%0A      if (workingThread == null %7C%7C !workingThread.isAlive()) %7B%0A        workingThread = new MessageRunnable();%0A        workingThread.start();%0A      %7D%0A    %7D%0A  %7D%0A%0A  protected class MessageRunnable extends AbstractThread %7B%0A%0A    @Override%0A    public boolean getRunning() %7B%0A      return running && !messageQueue.isEmpty();%0A    %7D%0A%0A    @Override%0A    public void run() %7B%0A      while (getRunning()) %7B%0A        MessageHolder job = messageQueue.poll();%0A        if (job != null)
+List%3CMessageHolder%3E message = MessageCreator.createMessage(seo);%0A      message.forEach(job -%3E
  %7B%0A
-  
     
@@ -1684,26 +1684,24 @@
 %7B%0A          
-  
 kernel.publi
@@ -1714,34 +1714,32 @@
 .getMessage());%0A
-  
         %7D else i
@@ -1802,26 +1802,24 @@
 %7B%0A          
- 
- 
 kernel.send(
@@ -1849,179 +1849,53 @@
     
-  %7D%0A  
+%7D%0A
       %7D
-%0A      %7D%0A      logger.debug(%22MessageRunnable shutdown.%22);%0A    %7D%0A  %7D%0A%0A  public void exit() %7B%0A    if (workingThread != null) %7B%0A      workingThread.halt();%0A    %7D
+);%0A    %7D%0A  %7D%0A%0A  public void exit() %7B
 %0A  %7D

remove thread from ExecutionResultSender (#6634)