Monitoring Java Background Processes

You can monitor background transactions with the Java agent by instrumenting your background task code. Metrics for background tasks will be reported in their own Background Tasks tab in the RPM UI.

Instrument using Annotations

Mark the entry method of the background task with our com.newrelic.agent.Trace annotation:

@Trace(dispatcher=true)
public void run() {
  // background task
}

The dispatcher=true option causes the annotated method to be treated like a web transaction--the agent will report errors and transaction traces for the task.

You'll need to include the New Relic agent jar in your compilation classpath if using com.newrelic.agent.Trace.

If you're using version 1.2.004.1 or later of the Java agent, you can define your own trace annotation class:

package com.test;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
    public static final String NULL = "";
    String metricName() default NULL;
    boolean dispatcher() default false;
    String tracerFactoryName() default NULL;
}

If defining your own Trace annotation, there's no dependency on newrelic_agent.jar.

Configure newrelic.yml

Add this to the common section of newrelic.yml:

enable_custom_tracing: true

If using a custom Trace annotation, configure the agent to use this annotation in newrelic.yml:

class_transformer:
  trace_annotation_class_name: com.test.Trace