001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tests.fail.driver; 020 021import org.apache.reef.tang.annotations.Unit; 022import org.apache.reef.task.Task; 023import org.apache.reef.task.TaskMessage; 024import org.apache.reef.task.TaskMessageSource; 025import org.apache.reef.task.events.CloseEvent; 026import org.apache.reef.task.events.DriverMessage; 027import org.apache.reef.task.events.SuspendEvent; 028import org.apache.reef.task.events.TaskStop; 029import org.apache.reef.util.Optional; 030import org.apache.reef.wake.EventHandler; 031import org.apache.reef.wake.remote.impl.ObjectSerializableCodec; 032 033import javax.inject.Inject; 034import java.util.logging.Level; 035import java.util.logging.Logger; 036 037/** 038 * A basic task that quite successfully does nothing. 039 */ 040@Unit 041public final class NoopTask implements Task, TaskMessageSource { 042 043 private static final Logger LOG = Logger.getLogger(NoopTask.class.getName()); 044 private static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>(); 045 private static final TaskMessage INIT_MESSAGE = TaskMessage.from("", CODEC.encode("MESSAGE::INIT")); 046 private transient boolean isRunning = true; 047 private transient Optional<TaskMessage> message = Optional.empty(); 048 049 @Inject 050 public NoopTask() { 051 LOG.info("NoopTask created."); 052 } 053 054 @Override 055 public synchronized byte[] call(final byte[] memento) { 056 this.isRunning = true; 057 while (this.isRunning) { 058 try { 059 LOG.info("NoopTask.call(): Waiting for the message."); 060 this.wait(); 061 } catch (final InterruptedException ex) { 062 LOG.log(Level.WARNING, "NoopTask.wait() interrupted.", ex); 063 } 064 } 065 LOG.log(Level.INFO, "NoopTask.call(): Exiting with message {0}", 066 CODEC.decode(this.message.orElse(INIT_MESSAGE).get())); 067 return this.message.orElse(INIT_MESSAGE).get(); 068 } 069 070 @Override 071 public synchronized Optional<TaskMessage> getMessage() { 072 LOG.log(Level.INFO, "NoopTask.getMessage() invoked: {0}", 073 CODEC.decode(this.message.orElse(INIT_MESSAGE).get())); 074 return this.message; 075 } 076 077 private synchronized void stopTask() { 078 LOG.info("NoopTask.stopTask() invoked."); 079 this.isRunning = false; 080 this.notify(); 081 } 082 083 public class TaskSuspendHandler implements EventHandler<SuspendEvent> { 084 @Override 085 public void onNext(final SuspendEvent suspendEvent) { 086 LOG.info("NoopTask.TaskSuspendHandler.send() invoked."); 087 NoopTask.this.stopTask(); 088 } 089 } 090 091 public class TaskStopHandler implements EventHandler<TaskStop> { 092 @Override 093 public void onNext(final TaskStop event) { 094 LOG.info("NoopTask.TaskStopHandler.send() invoked."); 095 NoopTask.this.stopTask(); 096 } 097 } 098 099 public class TaskCloseHandler implements EventHandler<CloseEvent> { 100 @Override 101 public void onNext(final CloseEvent closeEvent) { 102 LOG.info("NoopTask.TaskCloseHandler.send() invoked."); 103 NoopTask.this.stopTask(); 104 } 105 } 106 107 public class DriverMessageHandler implements EventHandler<DriverMessage> { 108 @Override 109 public void onNext(DriverMessage driverMessage) { 110 final byte[] msg = driverMessage.get().get(); 111 LOG.log(Level.INFO, "NoopTask.DriverMessageHandler.send() invoked: {0}", CODEC.decode(msg)); 112 synchronized (NoopTask.this) { 113 NoopTask.this.message = Optional.of(TaskMessage.from(NoopTask.this.toString(), msg)); 114 } 115 } 116 } 117}