Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Including Java Agent in Standalone Spring Boot Application

DZone's Guide to

Including Java Agent in Standalone Spring Boot Application

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Recently at DevSKiller.com we've decided to move majority of our stuff to simple containers. It was pretty easy due to use of Spring Boot uber-jars, but the problem was in NewRelic agents which should have to be included separately. That caused uncomfortable situation so we decided to solve it by including NewRelic agent into our uber-jar applications. If you also want to simplify your life please follow provided instructions :)

At first we have to add proper dependency into our pom.xml descriptor:

<dependency>
  <groupiId>com.newrelic.agent.java<<groupId>
  <artifactId>newrelic-agent</artifactId>
  <version>3.12.1</version>
  <scope>provided</scope>
</dependency>

Now since we have proper jar included into our project it's time to unpack the dependency to have all necessary classes in our application jar file:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.9</version>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
         <goal>unpack-dependencies</goal>
      </goals>
      <configuration>
        <includeArtifactIds>newrelic-agent</includeArtifactIds>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

After this step we've all agent related classes accessible directly from our jar. But still the file cannot be used as an agent jar. There are some important manifest entries that have to be present in every agent jar. The most important is the Premain-Class attribute specifying main agent class including premain() method. In case of NewRelic it's also important to include Can-Redefine-Classes and Can-Retransform-Classes attributes. The easiest way to do that is to extend maven-jar-plugin configuration:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <archive>
      <manifestEntries>
        <Premain-Class>com.newrelic.bootstrap.BootstrapAgent</Premain-Class>
        <Can-Redefine-Classes>true</Can-Redefine-Classes>
        <Can-Retransform-Classes>true</Can-Retransform-Classes>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

Now is coming the tricky part :) NewRelic agent also contains class with main() method which causes that Spring Boot repackager plugin is unable to find single main() method so build fails. It's not a problem but we have to remember to specify proper main class in spring-boot-maven-plugin (or in gradle plugin):

<configuration>
  <mainClass>my.custom.Application</mainClass>
</configuration>

That's all! You can execute your application with following command:

java -javaagent:myapp.jar -jar myapp.jar
Last but not least: don't forget to include NewRelic configuration file ( newrelic.yml) in the same directory as your application jar. The other solution is to set  newrelic.config.file system property to point the fully qualified file name.

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:
java ,enterprise-integration

Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}