I have a back-end thread, which I created inside start method of JBoss Mbeans:
public interface HScannerServiceMBean {
public void create();
public void start();
public void stop();
public void destroy();
public boolean isMasterNode();
public void startSingleton();
public void stopSingleton();
}
public class HScannerService implements HScannerServiceMBean {
public void create() {
log.info("Creating Service.......................................");
}
public void start() {
log.info("Starting Service.......................................");
TestThread tt;
try {
tt = new TestThread(1);
tt.startThread();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
public class TestThread implements Runnable {
private boolean running = false;
int mThreadName;
private Thread thread;
protected TestThread(int pThreadName) throws NamingException {
mThreadName = pThreadName;
this.thread = new Thread(this);
this.thread.setDaemon(true);
this.thread.setPriority(Thread.MIN_PRIORITY);
}
public void startThread() {
if (!this.running) {
this.running = true;
this.thread.start();
} else {
log.info("Thread already running");
}
}
public void run() {
System.out.println("Thread run method");
throw new NumberFormatException("incorrect number");
}
public void terminate() {
setRunning(false);
}
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
}
I have added AspectJ in the pom.xml:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
My aspect class looks like this:
@Aspect
public class ExceptionTranslationAspect {
@AfterThrowing(pointcut = "execution(* com.ent.service..*(..)) || execution(* com.ent.domain..*(..))", throwing = "ex")
public void logAfterThrowingExceptionCall(Exception ex) throws Throwable {
System.out.println("****exception received!");
}
}
The thread is throwing a simple exception, which should be caught by this aspect and print "****exception received!". But the aspect method is not getting called. My question is, can we use AspectJ with back-end threads? If yes, what is the problem with my code?
I am using JDK 8 and Wildfly 16.
Adding AspectJ libraries to your POM will not activate AspectJ. You either need to use load-time weaving by means of
-javaagent:/path/to/aspectweaver.jaror by another means of configuring your agent in WildFly, if there is any. Or you can use compile-time weaving, utilising AspectJ Maven Plugin.Besides, specifying two conflicting versions of AspectJ in the same POM is not helping either. I recommend to simply use the latest release AspectJ 1.9.20.1.