Coder Social home page Coder Social logo

jonaskahn / invesdwin-instrument Goto Github PK

View Code? Open in Web Editor NEW

This project forked from invesdwin/invesdwin-instrument

0.0 1.0 0.0 411 KB

An instrumentation java agent that is able to inject itself into the running process

License: GNU Lesser General Public License v3.0

Java 100.00%

invesdwin-instrument's Introduction

invesdwin-instrument

This is an instrumentation java agent that is able to load itself into the running JVM. It is able to dynamically setup springs InstrumentationLoadTimeWeaver to enable support for aspects without having to start the JVM with an explicit java agent.

Maven

Releases and snapshots are deployed to this maven repository:

https://invesdwin.de/repo/invesdwin-oss-remote/

Dependency declaration:

<dependency>
	<groupId>de.invesdwin</groupId>
	<artifactId>invesdwin-instrument</artifactId>
	<version>1.0.14</version><!---project.version.invesdwin-instrument-->
</dependency>

There is no plan to publish artifacts to maven central, see answer here for alternatives.

Usage

Simply define a static initializer like this:

static {
  DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present
  DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans
}

Spring-Boot

With spring-boot you have to ensure that the context in the aspects is updated by importing the one responsible for initializing the load time weaving into the spring-boot configuration:

@SpringBootApplication
/** 
 * Make @Configurable work via @EnableLoadTimeWeaving.
 * If it does not work, alternatively you can try: 
 * @ImportResource(locations = "classpath:/META-INF/ctx.spring.weaving.xml") 
 */
@EnableLoadTimeWeaving
public class MySpringBootApplication {
    public static void main(final String[] args) {
        DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present
        DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans
	//org.aspectj.weaver.loadtime.Agent.agentmain("", InstrumentationSavingAgent.getInstrumentation()); //workaround for spring-boot-devtools RestartLauncher
        SpringApplication.run(MySpringBootApplication.class, args); //start application, load some classes
    }
}

To enable the spring aspects, just add the spring-aspects.jar dependency to your project, they are enabled directly, others might need a aop.xml to be enabled. Note that the "ctx.spring.weaving.xml" is part of the invesdwin-instrument jar.

Make sure that the instrumentation is loaded before the classes of the aspects or the classes that use the aspects are loaded by the classloader. Only classes that get loaded after initializing load time weaving will be successfully woven by aspectj.

Please note that you need to have a JDK installed with tools.jar available in it for this to work properly (this is not required anymore since Java 9). Alternatively just load the invesdwin-instrument jar via the -javaagent <path> JVM parameter if you only have a JRE installed. The dynamic loading will be skipped then.

For a sample usage see the junit test cases in the invesdwin-aspects project or the spring-boot example project in invesdwin-nowicket.

Spring-Boot-Devtools

Note that spring-boot-devtools uses a org.springframework.boot.devtools.restart.RestartLauncher that reinitializes the application in a nested classloader. This restart mechanism can be disabled via the system property spring.devtools.restart.enabled=false, but that might disable functionality of spring-boot-devtools. To work around this, use org.aspectj.weaver.loadtime.Agent.agentmain("", InstrumentationSavingAgent.getInstrumentation()); inside the nested classloader to reinitialize AspectJ. Also you might have to add the AspectJ weaver option -Xreweavable to make it work properly. For more details see: invesdwin#24

Testing Aspects inside Unit Tests

See discussion here. The workaround is to use the aspects inside nested classes that are loaded after invesdwin-instrument is initialized by the unit test class. Example here.

Support

If you need further assistance or have some ideas for improvements and don't want to create an issue here on github, feel free to start a discussion in our invesdwin-platform mailing list.

invesdwin-instrument's People

Contributors

subes avatar mojo2012 avatar torstei avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.