io.github.isharipov:method-counter-core

Simple counter of method calls through AOP

License

License

GroupId

GroupId

io.github.isharipov
ArtifactId

ArtifactId

method-counter-core
Last Version

Last Version

0.0.7
Release Date

Release Date

Type

Type

jar
Description

Description

Simple counter of method calls through AOP

Download method-counter-core

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.isharipov/method-counter-core/ -->
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>method-counter-core</artifactId>
    <version>0.0.7</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.isharipov/method-counter-core/
implementation 'io.github.isharipov:method-counter-core:0.0.7'
// https://jarcasting.com/artifacts/io.github.isharipov/method-counter-core/
implementation ("io.github.isharipov:method-counter-core:0.0.7")
'io.github.isharipov:method-counter-core:jar:0.0.7'
<dependency org="io.github.isharipov" name="method-counter-core" rev="0.0.7">
  <artifact name="method-counter-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.isharipov', module='method-counter-core', version='0.0.7')
)
libraryDependencies += "io.github.isharipov" % "method-counter-core" % "0.0.7"
[io.github.isharipov/method-counter-core "0.0.7"]

Dependencies

compile (6)

Group / Artifact Type Version
org.aspectj : aspectjrt jar 1.9.2
org.aspectj : aspectjweaver jar 1.9.2
io.micrometer : micrometer-core jar 1.1.1
com.fasterxml.jackson.core : jackson-databind jar 2.9.7
org.slf4j : slf4j-api jar 1.8.0-beta2
ch.qos.logback : logback-classic jar 1.3.0-alpha4

Project Modules

There are no modules declared in this project.

Method counter

Simple counter of method calls through AOP.

Counter works based on CTW (compile-time weaving) principle.

You can use the library for both cases:

  • Simple libraries (Testing purposes)

  • With Spring Framework

Getting Started

Dependencies

Gradle

You can see a detailed example at the Gradle example project.

To add library to your project without Spring, add core dependency

build.gradle
compile group: 'io.github.isharipov', name: 'method-counter-core', version: '0.0.3

If you doesnt have aspect support in your project you have to setting up an gradle aspectj plugin and add core library as aspectpath

build.gradle
buildscript {
    dependencies {
        classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    }
}

ext{
    aspectjVersion = '1.9.2'
}

dependencies {
    implementation "io.github.isharipov:method-counter-core:0.0.3"
    aspectpath "io.github.isharipov:method-counter-core:0.0.3"
}

Maven

You can see a detailed example at the Maven example project.

pom.xml
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>method-counter-core</artifactId>
    <version>0.0.3</version>
</dependency>

If you doesnt have aspect support in your project you have to setting up an aspectj maven plugin and add core library as aspectLibrary

pom.xml
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>io.github.isharipov</groupId>
                <artifactId>method-counter-core</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
        <execution>
    </executions>
</plugin>

Project Lombok Support

Gradle

If you have Project Lombok in your current Gradle project you have to use ant directly

You can see a detailed example at the Gradle Lombok example project.

build.gradle
configurations {
    ajc
    aspects
    compile {
        extendsFrom aspects
    }
}

dependencies {
    ajc "org.aspectj:aspectjtools:1.9.2"

    implementation "io.github.isharipov:method-counter-core:0.0.6"
    aspects "io.github.isharipov:method-counter-core:0.0.6"

    compileOnly "org.projectlombok:lombok:1.18.4"
    annotationProcessor "org.projectlombok:lombok:1.18.4"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

def aspectj = { destDir, aspectPath, inpath, classpath ->
    ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
            classpath: configurations.ajc.asPath)

    ant.iajc(
            maxmem: "1024m", fork: "true", Xlint: "ignore",
            destDir: destDir,
            aspectPath: aspectPath,
            inpath: inpath,
            classpath: classpath,
            source: project.sourceCompatibility,
            target: project.targetCompatibility
    )
}

compileJava {
    doLast {
        aspectj project.sourceSets.main.output.classesDir.absolutePath,
                configurations.aspects.asPath,
                project.sourceSets.main.output.classesDir.absolutePath,
                project.sourceSets.main.runtimeClasspath.asPath
    }
}

Additional explanation you can find in the article.

Maven

You can see a detailed example at the Maven Lombok example project.

pom.xml
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>io.github.isharipov.method.counter.maven.lombok.example.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <complianceLevel>1.8</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>io.github.isharipov</groupId>
                            <artifactId>method-counter-core</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <forceAjcCompile>true</forceAjcCompile>
                    <sources/>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                    </weaveDirectories>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <id>aspectj-compile</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

Spring Boot Support

To use method counter with Spring Boot, there is Method counter Spring Boot Starter project

To use it in your Spring Boot Project just add dependency

Maven

pom.xml
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>method-counter-spring-boot-starter</artifactId>
    <version>0.0.3</version>
</dependency>

And setting up aspectj maven plugin .pom.xml

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>io.github.isharipov</groupId>
                <artifactId>method-counter-core</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
            </configuration>
        </execution>
    </executions>
</plugin>

Basic Functionality

The Counter is an annotation @Counter

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Inherited
public @interface Counter {

    String name() default "";

    Class<? extends CounterType> type() default DefaultCounterType.class;

    Class<? extends Behaviour> behaviour() default Success.class;

    boolean timer() default false;
}

By default it has a set of parameters:

  • name - ""

  • type - DefaultCounterType.class

  • behaviour - Success.class

  • timer - false

Out of the box Counter has three behaviours:

  • Success - count only if successful method execution

  • Always - count anyway

  • Failure - count only if method ended with an exception

Versions

Version
0.0.7
0.0.6
0.0.3
0.0.2