/*
* Copyright 2013 Alexander Bartash <AlexanderBartash@mail.ru>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package client;
import common.Compute;
import common.Task;
import java.net.MalformedURLException;
import java.rmi.AccessException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Alexander Bartash <AlexanderBartash@mail.ru>
* @version 06.03.2013 1:27:36
*/
public class RemoteTask implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(RemoteTask.class);
private final String URL;
private final Task task;
private Object result;
private List<FutureEvent> futureEvents;
public RemoteTask(final String url, final Task task) {
Validate.notNull(url, "Entry parameter 'url' can't be null.");
Validate.notNull(task, "Entry parameter 'task' can't be null.");
this.URL = url;
this.task = task;
}
public RemoteTask(final String url, final Task task, final FutureEvent futureEvent) {
Validate.notNull(url, "Entry parameter 'url' can't be null.");
Validate.notNull(task, "Entry parameter 'task' can't be null.");
Validate.notNull(futureEvent, "Entry parameter 'futureEvent' can't be null.");
this.URL = url;
this.task = task;
this.futureEvents = Arrays.asList(futureEvent);
}
public RemoteTask(final String url, final Task task, final List<FutureEvent> futureEvents) {
Validate.notNull(url, "Entry parameter 'url' can't be null.");
Validate.notNull(task, "Entry parameter 'task' can't be null.");
Validate.noNullElements(futureEvents, "Entry parameter 'futureEvents' can't contain null elements or be null itself.");
this.URL = url;
this.task = task;
this.futureEvents = futureEvents;
}
@Override
public void run() {
try {
final Compute comp = (Compute) Naming.lookup(URL);
final long startTime = System.currentTimeMillis();
LOG.info("Sending request to execute task: {}", this.task.toString());
this.result = comp.executeTask(this.task);
LOG.info("Execution finished in {} ms, result is: {}",
System.currentTimeMillis() - startTime,
this.result);
for (FutureEvent futureEvent : futureEvents) {
futureEvent.taskCompleted(this.result);
}
} catch (AccessException ex) {
LOG.error("This operation is not permitted.", ex);
} catch (NotBoundException ex) {
LOG.error("Specefied URL is not bound.", ex);
} catch (MalformedURLException ex) {
LOG.error("Specefied URL is not an appropriately formatted URL.", ex);
} catch (RemoteException ex) {
LOG.error("RMI registry could not be contacted.", ex);
}
}
public Object getResult() {
return result;
}
public void addFutureEvent(final FutureEvent futureEvent) {
Validate.notNull(futureEvent, "Entry parameter 'futureEvent' can't be null.");
if (null == this.futureEvents) {
this.futureEvents = new ArrayList<>(1);
}
this.futureEvents.add(futureEvent);
}
}