Java command annotations are used for the developing Java commands in PAK.
This chapter describes the functionality of the @PreDestroy
annotation.
It also gives an example of how you can use this annotation in your implementation.
1. Basics
The @PreDestroy
annotation marks a method, that should be run after the command execution is finished.
It is intended for cleaning up allocated resources.
This method is always executed, even if the @Run
command method has thrown an exception.
The fields are written to the datastore after the execution of the method annotated with @PreDestroy
and if the @Run
execution was successful.
This means that any field manipulations performed in this method will also affect the values written to the datastore.
A method annotated with this annotation has to be of type void
, but might accept a boolean as argument, which contains the success state of the command.
The name of the @PreDestroy method must not collide with the getter/setter name of a class variable.For further information please refer to the @Persistent documentation.
|
2. Usage
Listing 1 shows the example usage of the @PreDestroy
annotation.
In this case it annotates a void method without a method parameter.
It appends the postfix _Test
to the element name which will be afterwards written to the datastore.
@PreDestroy
/**
* @workflowDocu This command gets an element by its name.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
/**
* @workflowDocu The element name
*/
@Persistent(scope = FieldScope.READ_WRITE)
private String element;
/**
* Manipulate the
*/
@PreDestroy (1)
public void manipulateOutput() {
this.element = "_Test";
}
[...]
}
1 | Use of the @PreDestroy annotation |
In the usage case of Listing 2 a void method with a boolean method parameter is annotated with @PreDestroy
.
This parameter success
represents the success or fail of the @Run
method.
In case of success the element name is manipulated and in case of fail a log message is provided to the user.
@PreDestroy
for a method with a parameter/**
* @workflowDocu This command gets an element by its name.
*/
@JavaCommand
@CommandGroup("Element")
public class GetElement {
private static final Logger LOG = LoggerFactory.getLogger(GetElement.class);
/**
* @workflowDocu The element name
*/
@Persistent(scope = FieldScope.READ_WRITE)
private String name;
/**
* Manipulate the variable output
*/
@PreDestroy (1)
public void manipulateOutput(final boolean success) {
if (success) {
this.name = "_success";
} else {
LOG.error("Execution failed");
}
}
[...]
/**
* Fetch the element with a given name.
*/
@Run
public void fetchElement() {
// Command functionality
[...]
}
}
1 | Use of the @PreDestroy annotation for a method with a boolean parameter |