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.messaging.task;
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.DriverMessage;
026import org.apache.reef.util.Optional;
027import org.apache.reef.wake.EventHandler;
028import org.apache.reef.wake.remote.impl.ObjectSerializableCodec;
029
030import javax.inject.Inject;
031import java.util.logging.Level;
032import java.util.logging.Logger;
033
034/**
035 * A basic task that receives a message and sends it back to the driver.
036 */
037@Unit
038public final class TaskMessagingTask implements Task, TaskMessageSource {
039
040  private static final Logger LOG = Logger.getLogger(TaskMessagingTask.class.getName());
041  private static final ObjectSerializableCodec<String> CODEC = new ObjectSerializableCodec<>();
042  private static final TaskMessage INIT_MESSAGE = TaskMessage.from("", CODEC.encode("MESSAGE::INIT"));
043  private transient boolean isRunning = true;
044  private transient Optional<TaskMessage> message = Optional.empty();
045
046  @Inject
047  public TaskMessagingTask() {
048    LOG.info("TaskMsg created.");
049  }
050
051  @Override
052  public synchronized byte[] call(final byte[] memento) {
053    LOG.info("TaskMsg.call() invoked. Waiting for the message.");
054    while (this.isRunning) {
055      try {
056        this.wait();
057      } catch (final InterruptedException ex) {
058        LOG.log(Level.WARNING, "wait() interrupted.", ex);
059      }
060    }
061    return this.message.orElse(INIT_MESSAGE).get();
062  }
063
064  @Override
065  public synchronized Optional<TaskMessage> getMessage() {
066    LOG.log(Level.INFO, "TaskMsg.getMessage() invoked: {0}",
067        CODEC.decode(this.message.orElse(INIT_MESSAGE).get()));
068    if (this.message.isPresent()) {
069      this.isRunning = false;
070      this.notify();
071    }
072    return this.message;
073  }
074
075  /**
076   * Handler for DriverMessage.
077   */
078  public class DriverMessageHandler implements EventHandler<DriverMessage> {
079    @Override
080    public void onNext(final DriverMessage driverMessage) {
081      final byte[] driverMsg = driverMessage.get().get();
082      LOG.log(Level.INFO, "TaskMsg.send() invoked: {0}", CODEC.decode(driverMsg));
083      TaskMessagingTask.this.message = Optional.of(TaskMessage.from(this.toString(), driverMsg));
084    }
085  }
086}