-1

I am trying to use Hystrix in my Java Application, its a Non spring java application.

Used following Maven Dependencies in POM to enable Hystrix commands :

  <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-javanica</artifactId>
      <version>1.5.8</version>
    </dependency>

    <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-core</artifactId>
      <version>1.5.12</version>
    </dependency>

    <dependency>
      <groupId>com.netflix.rxjava</groupId>
      <artifactId>rxjava-core</artifactId>
      <version>0.20.7</version>
    </dependency>

Used following Dependencies to enable AspectJ :

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.7</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.7</version>
</dependency>

Created a aop.xml in META-INF with following configuration :

<aspectj>

  <aspects>
    <aspect name="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
  </aspects>

  <weaver options="-verbose">
    <include within="*" />
  </weaver>
</aspectj>

Used Hystrix Command in my Service Class :

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component
@Service
public class TestHystrix 

    @HystrixCommand(commandKey = "testHystrix", threadPoolKey = "testHystrix", commandProperties = {
            @HystrixProperty(name = "hystrix.command.testHystrix.execution.isolation.thread.timeoutInMilliseconds", value = "30") }, threadPoolProperties = {
                    @HystrixProperty(name = "hystrix.threadpool.testHystrix.maximumSize", value = "3") })
public void  testHystrix() {

Added following JVM Argument :

-DWeavingMode=compile

But at both Junit testing and application Runtime, its resulting into following error :

java.lang.NoSuchMethodError: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.aspectOf()Lcom/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect;

Please help.

kriegaex
  • 50,139
  • 12
  • 92
  • 145
Narendra
  • 131
  • 1
  • 8
  • What was unclear to you after reading the [configuration guide](https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#aspect-weaving)? – kriegaex Jun 23 '18 at 02:38

2 Answers2

1

Before asking a question, you should first consult the manual of any tool you like to use. I am just quoting form there:

Aspect weaving

Javanica supports two weaving modes: compile and runtime. (...)

  • CTW. To use CTW mode you need to use specific jar version: hystrix-javanica-ctw-X.Y.Z. This jar is assembled with aspects compiled with using AJC compiler. If you will try to use regular hystrix-javanica-X.Y.Z with CTW then you get NoSuchMethodError aspectOf() at runtime from building with iajc. Also, you need to start your app with using java property: -DWeavingMode=compile. (...)

So maybe you want to switch your library.

BTW, if you use compile-time weaving (CTW), you should not need aop.xml because AspectJ only uses it for load-time weaving (LTW).

kriegaex
  • 50,139
  • 12
  • 92
  • 145
  • Actually I do not have 'hystrix-javanica-ctw-X.Y.Z' available in my companies Maven Repository, so I am trying to have LTW or RTW or other workaround with above configuration, also tried by adding following plugin, but NO luck : org.codehaus.mojo aspectj-maven-plugin 1.7 ... com.netflix.hystrix hystrix-javanica ... compile – Narendra Jun 23 '18 at 23:55
  • I have never used Hystrix before and do not feel inclined to piece together your project from the information you provided in code snippets. Please publish an [MCVE](http://stackoverflow.com/help/mcve) reproducing the problem on GitHub, so I can build locally and see what happens. – kriegaex Jun 24 '18 at 07:12
  • Its a huge legacy application built using Apache Felix, I am sorry I am not allowed to the share the more the code. Thank you for your help. – Narendra Jun 25 '18 at 00:48
1

I was able to fix the issue by using following AspectJ Plugin configuration along with above maven dependencies :

 <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <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>
          <!-- Provide the Source information.  -->          
          <!-- <aspectLibraries>
            <aspectLibrary>
              <groupId>com.netflix.hystrix</groupId>
              <artifactId>hystrix-javanica</artifactId>
            </aspectLibrary>
          </aspectLibraries> -->
          <!--Weaving already compiled JAR artifacts  -->
          <weaveDependencies>
            <weaveDependency>
              <groupId>com.netflix.hystrix</groupId>
              <artifactId>hystrix-javanica</artifactId>
            </weaveDependency>
          </weaveDependencies>
        </configuration>
        <executions>
          <execution>
            <goals>
              <!-- use this goal to weave all your main classes -->
              <goal>compile</goal>
              <!-- use this goal to weave all your test classes -->
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

This plugin will enable Post Compile weaving, for more details refer a very good article @ http://www.baeldung.com/aspectj https://www.mojohaus.org/aspectj-maven-plugin/examples/weaveJars.html With this plugin, aop.xml and -DWeavingMode=compile are also not required

Narendra
  • 131
  • 1
  • 8
  • If You use CTW mode, The -DWeavingMode=compile is required, because hystrix-javanica default weavingmode is runtime. You can check it at [link](https://github.com/Netflix/Hystrix/blob/master/hystrix-contrib/hystrix-javanica/src/main/java/com/netflix/hystrix/contrib/javanica/utils/EnvUtils.java) – Michael.Zhang Jul 19 '18 at 07:41
  • Thanks!, my problem was not related to the inclusion of Hystric Aspect in my project. I was trying to include Aspects from a test-framework-library in another project and I was getting the ...aspectOf() error. When I included the configuration the problem was fixed. – Jorge Muñoz May 30 '19 at 13:20