Define an Asynchronous Thread Handoff
If your checkout method also does a thread handoff and executes some business logic that you want to monitor in a separate thread, register the worker thread with the Business Transaction:
public String checkout(List<ItemOrders> orders) {
Transaction transaction = null;
try {
transaction = AppdynamicsAgent.startTransaction("Checkout", null, EntryTypes.POJO, false);
// ... Method code
// Custom thread handoff using custom queue
asyncTaskQueue.add(task);
} finally {
if (transaction != null) {
transaction.end();
}
}
}
To instrument this, modify the add method to mark a thread handoff and then start a new segment where the thread begins running:
public class AsyncTaskQueue {
public void add(Task task) {
AppdynamicsAgent.getTransaction().markHandoff(task);
/*******************
* Method Body
*******************/
}
public class Task {
public void run() {
Transaction transaction = null;
try {
transaction = AppdynamicsAgent.startSegment(this);
/*******************
* Method Body
*******************/
} finally {
if (transaction != null) {
transaction.endSegment();
}
}
}
public void cancel() {
AppdynamicsAgent.cancelHandoff(this);
/*******************
* Method Body
*******************/
}
}
The task object is used by the agent to link the segments. Correlating thread segments using the agent API requires that the agent is running in the Executor mode.