# Java task

A Java task runs a class of your own as a step of a job. Where an [Execute](Task-Execute.md) task starts a separate program, a Java task runs inside the job engine and works directly on the list of items: it can read every value of every item, change them, add items and take them away.

It is for developers. Everybody else is better served by the built-in tasks.

> [!WARNING]
> A class loaded into the engine can do anything the engine can. Only full administrators can put this task into a job; no role can grant it.

## Fields

| Field | What it does |
|---|---|
| **Fully qualified class** | the class to run, with its package: `tasks.MyTask` |
| **Parameters** | pairs of **Key** and **Value**, added with **Add Parameter**. They are handed to the class. [Variables](Jobs-Variables.md) in the values are filled in before the class sees them. |
| **Help** | shows the note and the sample below |

> This class needs to extend the "tasks.Task" class from the CrushTask.jar file in your plugins folder.

## The class

```java
package tasks;

import java.util.Properties;
import java.util.Vector;

public class MyTask extends Task
{
	public MyTask(Properties prefs, Vector items, Properties info, Vector threads)
	{
		super(prefs, items, info, threads);
	}

	public Task go()
	{
		run();
		return null;
	}

	public void run()
	{
		try
		{
			newItems.addAll(items); // what the next task gets
			for (int x = 0; x < items.size(); x++)
			{
				Properties item = (Properties) items.elementAt(x);
				msg("Looking at " + item.getProperty("url"));
			}
			// the task's parameters
			Vector v = (Vector) prefs.get("prop_item");
			for (int x = 0; v != null && x < v.size(); x++)
			{
				Properties p = (Properties) v.elementAt(x);
				msg(p.getProperty("key") + "=" + p.getProperty("val", ""));
			}
		}
		catch (Exception e)
		{
			error = e; // makes the task fail
			msg(e);
			log(false, e.toString(), null);
		}
	}
}
```

| In the class | Is |
|---|---|
| `items` | the list the task was given: a `Vector` of `Properties`, one per item. `url`, `the_file_name`, `the_file_path`, `the_file_size` and whatever earlier tasks set. |
| `newItems` | what the task hands on. Empty unless you fill it. |
| `prefs` | the task's settings; the parameters are in `prop_item` |
| `info` | the run's shared values |
| `msg(…)` | writes a line to the job's log |
| `error` | set it to an exception to make the task fail and its red line be taken |

## Installing it

1. Compile the class against `CrushTask.jar`.
2. Put it, as a jar, where the server loads plugins from: `plugins/lib` in the server's folder.
3. Restart the server once, so that the class is found.

Engines on other machines need the jar too.

## What it hands on

Whatever the class puts into `newItems`. A class that forgets that hands on nothing, and the tasks behind it have no files.

## Things to know

- **The whole list is in memory.** A Java task is always given every item at once, also for lists that otherwise live on disk. When there is not enough memory for that, the task fails and says how much it would have needed, before it starts. See [Jobs with a million items](Jobs-Large-Lists.md).
- **A runner of its own.** A job with code that is not the server's is best kept apart from the other jobs: set **Runner: A runner of its own** in the job's [Setup](Jobs-Schedules.md#job-engine). A fault in the class then ends that job's process and nothing else.
- **Stopping.** A long loop in the class should call `CrushTask.Start.checkStatus(tracker, this)` now and then: that is where a run that was stopped or paused takes notice.
- **Updates.** The class is compiled against one version of the server's code. Test it after every update of the server.
- **Values are data.** Parameters are filled in from variables once, before the class runs. What the class reads from an item is the raw value: a file name can hold anything.
