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

来源:互联网 发布:图片墙制作软件 编辑:程序博客网 时间:2024/06/08 18:42

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 TaskEventcase class TaskSubmitted(name:String) extends TaskEventcase class TaskSucceeded(name:String) extends TaskEventcase class TaskFailed(name:String) extends TaskEvent

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

package threadimport java.util.concurrent.atomic.AtomicBooleanimport 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 invoketask-1task-2task-3task-4task-5task-6task-7task-8task-9task-10
原创粉丝点击