001/*
002 * JPPF.
003 * Copyright (C) 2005-2015 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.node.event;
020
021import java.util.EventObject;
022
023import org.jppf.management.TaskInformation;
024import org.jppf.node.protocol.Task;
025
026/**
027 * Instances of this class represent events that occur during the life span of an individual JPPF task.
028 * @author Laurent Cohen
029 */
030public class TaskExecutionEvent extends EventObject
031{
032  /**
033   * Object encapsulating information about the task.
034   */
035  private final TaskInformation taskInformation;
036  /**
037   * User-defined object to send as part of the notification.
038   */
039  private final Object userObject;
040  /**
041   * If <code>true</code> then also send this notification via the JMX MBean, otherwise only send to local listeners.
042   */
043  private final boolean sendViaJmx;
044  /**
045   * Whether this is a task completion or user-sent event.
046   */
047  private final boolean taskCompletion;
048
049  /**
050   * Initialize this event object with the specified task.
051   * This constructor is used by the JPPF node when sending task completion notifications.
052   * @param task the JPPF task from which the event originates.
053   * @param jobId the id of the job this task belongs to.
054   * @param jobName the name of the job this task belongs to.
055   * @param cpuTime the cpu time taken by the task.
056   * @param elapsedTime the wall clock time taken by the task.
057   * @param error determines whether the task had an exception.
058   */
059  public TaskExecutionEvent(final Task<?> task, final String jobId, final String jobName, final long cpuTime, final long elapsedTime, final boolean error)
060  {
061    super(task);
062    this.taskInformation = new TaskInformation(task.getId(), jobId, jobName, cpuTime, elapsedTime, error, task.getPosition());
063    this.userObject = null;
064    this.sendViaJmx = true;
065    taskCompletion = true;
066  }
067
068  /**
069   * Initialize this event object with the specified task.
070   * This constructor is used when sending user-ddefined notifications from the tasks.
071   * @param task the JPPF task from which the event originates.
072   * @param jobId the id of the job this task belongs to.
073   * @param jobName the name of the job this task belongs to.
074   * @param userObject a user-defined object to send as part of the notification.
075   * @param sendViaJmx if <code>true</code> then also send this notification via the JMX MBean, otherwise only send to local listeners.
076   * @since 4.0
077   */
078  public TaskExecutionEvent(final Task<?> task, final String jobId, final String jobName, final Object userObject, final boolean sendViaJmx)
079  {
080    super(task);
081    this.taskInformation = new TaskInformation(task.getId(), jobId, jobName, -1, -1, false, task.getPosition());
082    this.userObject = userObject;
083    this.sendViaJmx = sendViaJmx;
084    taskCompletion = false;
085  }
086
087  /**
088   * Get the JPPF task from which the event originates.
089   * @return a <code>Task</code> instance.
090   */
091  public Task<?> getTask()
092  {
093    return (Task<?>) getSource();
094  }
095
096  /**
097   * Get the object encapsulating information about the task.
098   * @return a <code>TaskInformation</code> instance.
099   */
100  public TaskInformation getTaskInformation()
101  {
102    return taskInformation;
103  }
104
105  /**
106   * Get the user-defined object to send as part of the notification.
107   * @return the object specified in the constructor or <code>null</code>.
108   * @since 4.0
109   */
110  public Object getUserObject()
111  {
112    return userObject;
113  }
114
115  /**
116   * If <code>true</code> then also send this notification via the JMX MBean, otherwise only send to local listeners.
117   * @return <code>true</code> if this notification should be sent via JMX, <code>false</code> otherwise.
118   * @since 4.0
119   */
120  public boolean isSendViaJmx()
121  {
122    return sendViaJmx;
123  }
124
125  /**
126   * Determine whether this is a task completion or user-sent event.
127   * @return <code>true</code> if this is a task completion event, <code>false</code> otherwise.
128   * @since 4.0
129   * @exclude
130   */
131  public boolean isTaskCompletion()
132  {
133    return taskCompletion;
134  }
135
136  /**
137   * Determine whether this is a user-sent event.
138   * @return <code>true</code> if this is a task completion event, <code>false</code> otherwise.
139   * @since 4.1
140   */
141  public boolean isUserNotification()
142  {
143    return !taskCompletion;
144  }
145}