spark(5)--spark模型中FIFO的实现

pom文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tianjun</groupId>
    <artifactId>scala-test</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.10.6</scala.version>

    </properties>

    <dependencies>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>


        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>

        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-make:transitive</arg>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>




</project>

接口和样本类

package thread

/**
 * Created by tianjun on 2017/8/22.
 */
trait TaskEvent

case class TaskSubmitted(name:String) extends TaskEvent

case class TaskSucceeded(name:String) extends TaskEvent

case class TaskFailed(name:String) extends TaskEvent

任务进队列,队列的处理的抽象类

package thread

import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.{LinkedBlockingDeque, BlockingQueue}

import scala.util.control.NonFatal

/**
 * Created by tianjun on 2017/8/22.
 */
abstract class EventLoop[E](name: String) {

  private val eventQueue: BlockingQueue[E] = new LinkedBlockingDeque[E]()

  private val stopped = new AtomicBoolean(false)

  private val eventThread = new Thread(name){

    override def run():Unit = {
      try{
        while (!stopped.get()){
          val event = eventQueue.take()
          try{
            //偏函数,某一类类型的任务交给某一类型去处理
            onReceive(event)//在子类TaskProcessEventLoop中有实现,就是打印event
          }catch {
            case NonFatal(e) => {
              try{
                onError(e)
              }catch {
                case NonFatal(e) => println("Unexpected error in " + name,e)
              }
            }

          }
        }
      }catch {
        case ie:InterruptedException => // exit even if eventQuene is not empty
        case NonFatal(e) => println("Unexpected error in "+name,e)
      }

    }
  }

  def start(): Unit = {
    if(stopped.get()){
      throw new IllegalStateException(name+" has already been stopped")
    }
    onStart()
    eventThread.start()
  }

  def stop(): Unit ={
    if(stopped.compareAndSet(false,true)){
      eventThread.interrupt()
      var onStopCalled = false
      try{
        eventThread.join()
        onStopCalled = true
        onStop()
      }catch{
        case ie: InterruptedException =>
          Thread.currentThread().interrupt()
          if(!onStopCalled){
            onStop()
          }
      }
    }else{

    }
  }

  def post(event : E) : Unit = {
    eventQueue.put(event)
  }

  def isActive: Boolean = eventThread.isAlive

  protected def onStart(): Unit = {}

  protected def onStop(): Unit = {}

  protected def onReceive: PartialFunction[E,Unit]

  protected def onError(e: Throwable): Unit


}

队列处理的实现类

package thread

/**
 * Created by tianjun on 2017/8/22.
 */
class TaskProcessEventLoop(name:String) extends EventLoop[TaskEvent](name){

  override protected def onReceive:PartialFunction[TaskEvent,Unit] = {
    case TaskSubmitted(taskName) => println(taskName)
  }

  override protected def onError(e:Throwable):Unit = {

  }

  override protected def onStart(): Unit = {
    println("on start invoke")
  }

}

FIFO队列的调用

package thread

/**
 * Created by tianjun on 2017/8/22.
 */
object Bootstrap {

  def main(args: Array[String]) {
    val eventLoop = new TaskProcessEventLoop("task-event-loop")
    eventLoop.start()
    for(i<- 1 to 10){
      eventLoop.post(TaskSubmitted(s"task-$i"))
    }
    Thread.sleep(10000)
  }

}

运行结果

on start invoke
task-1
task-2
task-3
task-4
task-5
task-6
task-7
task-8
task-9
task-10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值