This project has retired. For details please refer to its Attic page.
Source code
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.task;
020
021import org.apache.reef.tang.annotations.Unit;
022import org.apache.reef.task.Task;
023import org.apache.reef.task.events.CloseEvent;
024import org.apache.reef.task.events.TaskStop;
025import org.apache.reef.tests.library.exceptions.SimulatedTaskFailure;
026import org.apache.reef.wake.EventHandler;
027
028import javax.inject.Inject;
029import java.util.logging.Level;
030import java.util.logging.Logger;
031
032/**
033 * A basic task that just fails when we stop it.
034 */
035@Unit
036public final class FailTaskStop implements Task, EventHandler<TaskStop> {
037
038  private static final Logger LOG = Logger.getLogger(FailTaskStop.class.getName());
039
040  private transient boolean isRunning = true;
041
042  @Inject
043  public FailTaskStop() {
044    LOG.fine("FailTaskStop created.");
045  }
046
047  @Override
048  public byte[] call(final byte[] memento) {
049    synchronized (this) {
050      LOG.fine("FailTaskStop.call() invoked. Waiting for the message.");
051      while (this.isRunning) {
052        try {
053          this.wait();
054        } catch (final InterruptedException ex) {
055          LOG.log(Level.WARNING, "wait() interrupted.", ex);
056        }
057      }
058    }
059    return null;
060  }
061
062  @Override
063  public void onNext(final TaskStop event) throws SimulatedTaskFailure {
064    final SimulatedTaskFailure ex = new SimulatedTaskFailure("FailTaskStop.send() invoked.");
065    LOG.log(Level.FINE, "FailTaskStop.onNext() invoked. Raise exception: {0}", ex.toString());
066    throw ex;
067  }
068
069  /**
070   * Handler for CloseEvent.
071   */
072  public final class CloseEventHandler implements EventHandler<CloseEvent> {
073    @Override
074    public void onNext(final CloseEvent event) {
075      LOG.log(Level.FINEST, "FailTaskStop.CloseEventHandler.onNext() invoked: {0}", event);
076      synchronized (FailTaskStop.this) {
077        isRunning = false;
078        FailTaskStop.this.notify();
079      }
080    }
081  }
082}