org.apache.jmeter.protocol.java.sampler

## Class JavaSampler

- java.lang.Object
- - org.apache.jmeter.testelement.AbstractTestElement
  - - org.apache.jmeter.samplers.AbstractSampler
    - - org.apache.jmeter.protocol.java.sampler.JavaSampler

All Implemented Interfaces:
  Serializable, Cloneable, ConfigMergabilityIndicator, Searchable, Interruptible, Sampler, TestElement, TestStateListener

    public class JavaSampler
    extends AbstractSampler
    implements TestStateListener, Interruptible

A sampler for executing custom Java code in each sample. See `JavaSamplerClient` and `AbstractJavaSamplerClient` for information on writing Java code to be executed by this sampler.

See Also:
  Serialized Form

### Field Summary

Fields
`static String` `ARGUMENTS` Property key representing the arguments for the JavaSamplerClient.
`static String` `CLASSNAME` Property key representing the classname of the JavaSamplerClient to user.

### Fields inherited from interface org.apache.jmeter.testelement.TestElement

`COMMENTS, ENABLED, GUI_CLASS, NAME, TEST_CLASS`

### Constructor Summary

Constructors
Constructor and Description
`JavaSampler()` Create a JavaSampler.

### Method Summary

All Methods Instance Methods Concrete Methods
`boolean` `applies(ConfigTestElement configElement)` Does configElement apply to Sampler
`Object` `clone()`
`Arguments` `getArguments()` Get the arguments (parameters) for the JavaSamplerClient to be executed with.
`String` `getClassname()` Gets the Classname attribute of the JavaConfig object
`boolean` `interrupt()` Interrupt the current operation if possible.
`SampleResult` `sample(Entry entry)` Performs a test sample.
`void` `setArguments(Arguments args)` Set the arguments (parameters) for the JavaSamplerClient to be executed with.
`void` `setClassname(String classname)` Sets the Classname attribute of the JavaConfig object
`void` `testEnded()` Method called at the end of the test.
`void` `testEnded(String host)` Called once for all threads after the end of a test.
`void` `testStarted()` Called just before the start of the test from the main engine thread.
`void` `testStarted(String host)` Called just before the start of the test from the main engine thread.

### Methods inherited from class org.apache.jmeter.testelement.AbstractTestElement

`addPropertiesValues, addProperty, addProperty, addTestElement, canRemove, clear, clearTemporary, clearTestElementChildren, emptyTemporary, equals, getComment, getName, getProperty, getPropertyAsBoolean, getPropertyAsBoolean, getPropertyAsDouble, getPropertyAsFloat, getPropertyAsInt, getPropertyAsInt, getPropertyAsLong, getPropertyAsLong, getPropertyAsString, getPropertyAsString, getSearchableTokens, getThreadContext, getThreadName, hashCode, isEnabled, isRunningVersion, isTemporary, logProperties, mergeIn, propertyIterator, recoverRunningVersion, removeProperty, setComment, setEnabled, setName, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setRunningVersion, setTemporary, setThreadContext, setThreadName, traverse, traverseCollection, traverseMap, traverseProperty`

### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.apache.jmeter.testelement.TestElement

`addTestElement, canRemove, clear, clearTestElementChildren, getComment, getName, getProperty, getPropertyAsBoolean, getPropertyAsBoolean, getPropertyAsDouble, getPropertyAsFloat, getPropertyAsInt, getPropertyAsInt, getPropertyAsLong, getPropertyAsLong, getPropertyAsString, getPropertyAsString, getThreadContext, getThreadName, isEnabled, isRunningVersion, isTemporary, propertyIterator, recoverRunningVersion, removed, removeProperty, setComment, setEnabled, setName, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setProperty, setRunningVersion, setTemporary, setThreadContext, setThreadName, traverse`

### Field Detail

#### CLASSNAME

    public static final String CLASSNAME

Property key representing the classname of the JavaSamplerClient to user.

See Also:
  Constant Field Values

#### ARGUMENTS

    public static final String ARGUMENTS

Property key representing the arguments for the JavaSamplerClient.

See Also:
  Constant Field Values

### Constructor Detail

#### JavaSampler

    public JavaSampler()

Create a JavaSampler.

### Method Detail

#### clone

    public Object clone()

Specified by:
  `clone` in interface `TestElement`
Overrides:
  `clone` in class `AbstractTestElement`

#### setArguments

    public void setArguments(Arguments args)

Set the arguments (parameters) for the JavaSamplerClient to be executed with.

Parameters:
  `args` - the new arguments. These replace any existing arguments.

#### getArguments

    public Arguments getArguments()

Get the arguments (parameters) for the JavaSamplerClient to be executed with.

Returns:
  the arguments

#### setClassname

    public void setClassname(String classname)

Sets the Classname attribute of the JavaConfig object

Parameters:
  `classname` - the new Classname value

#### getClassname

    public String getClassname()

Gets the Classname attribute of the JavaConfig object

Returns:
  the Classname value

#### sample

    public SampleResult sample(Entry entry)

Performs a test sample. The `sample()` method retrieves the reference to the Java client and calls its `runTest()` method.

Specified by:
  `sample` in interface `Sampler`
Parameters:
  `entry` - the Entry for this sample
Returns:
  test SampleResult
See Also:
  `JavaSamplerClient.runTest(JavaSamplerContext)`

#### testStarted

    public void testStarted()

Description copied from interface: `TestStateListener`

Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testStarted` in interface `TestStateListener`
See Also:
  `StandardJMeterEngine.run()`

#### testStarted

    public void testStarted(String host)

Description copied from interface: `TestStateListener`

Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testStarted` in interface `TestStateListener`
Parameters:
  `host` - name of host
See Also:
  `StandardJMeterEngine.run()`

#### testEnded

    public void testEnded()

Method called at the end of the test. This is called only on one instance of JavaSampler. This method will loop through all of the other JavaSamplers which have been registered (automatically in the constructor) and notify them that the test has ended, allowing the JavaSamplerClients to cleanup.

Specified by:
  `testEnded` in interface `TestStateListener`
See Also:
  `StandardJMeterEngine.stopTest()`

#### testEnded

    public void testEnded(String host)

Description copied from interface: `TestStateListener`

Called once for all threads after the end of a test. This will use the same element instances as at the start of the test.

N.B. testStarted() and testEnded() are called from different threads.

Specified by:
  `testEnded` in interface `TestStateListener`
Parameters:
  `host` - name of host
See Also:
  `StandardJMeterEngine.stopTest()`

#### applies

    public boolean applies(ConfigTestElement configElement)

Description copied from class: `AbstractSampler`
Does configElement apply to Sampler

Specified by:
  `applies` in interface `ConfigMergabilityIndicator`
Overrides:
  `applies` in class `AbstractSampler`
Parameters:
  `configElement` - `ConfigTestElement`
Returns:
  boolean
See Also:
  `AbstractSampler.applies(org.apache.jmeter.config.ConfigTestElement)`

#### interrupt

    public boolean interrupt()

Description copied from interface: `Interruptible`
Interrupt the current operation if possible.

Specified by:
  `interrupt` in interface `Interruptible`
Returns:
  `true` if there was an operation to interrupt.