Background Context
Hi everyone
I am implementing an Executor in a Spring Boot REST web app to execute all @Async methods.
I am implementing a custom ThreadFactory
because of this HazelcastException
being thrown:
<code>com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult Error happens during Process ABC: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult
<code>com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult Error happens during Process ABC: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult
</code>
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult Error happens during Process ABC: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult
We encountered this same Hazelcast exception before with services that use ForkJoinPool
& implemented the following:
CustomForkJoinWorkerThreadFactory.java
<code>import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
public class CustomForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
return new CustomForkJoinWorkerThread(pool);
private static class CustomForkJoinWorkerThread extends ForkJoinWorkerThread {
private CustomForkJoinWorkerThread(final ForkJoinPool pool) {
// set the correct classloader here
setContextClassLoader(Thread.currentThread().getContextClassLoader());
<code>import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
public class CustomForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
@Override
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
return new CustomForkJoinWorkerThread(pool);
}
private static class CustomForkJoinWorkerThread extends ForkJoinWorkerThread {
private CustomForkJoinWorkerThread(final ForkJoinPool pool) {
super(pool);
// set the correct classloader here
setContextClassLoader(Thread.currentThread().getContextClassLoader());
}
}
}
</code>
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
public class CustomForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {
@Override
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
return new CustomForkJoinWorkerThread(pool);
}
private static class CustomForkJoinWorkerThread extends ForkJoinWorkerThread {
private CustomForkJoinWorkerThread(final ForkJoinPool pool) {
super(pool);
// set the correct classloader here
setContextClassLoader(Thread.currentThread().getContextClassLoader());
}
}
}
Service.java
in the service using ForkJoinPool
<code>ForkJoinPool fjp = new ForkJoinPool(90, new CustomForkJoinWorkerThreadFactory(), null, false);
<code>ForkJoinPool fjp = new ForkJoinPool(90, new CustomForkJoinWorkerThreadFactory(), null, false);
</code>
ForkJoinPool fjp = new ForkJoinPool(90, new CustomForkJoinWorkerThreadFactory(), null, false);
My Implementation using ThreadPoolExecutor
Now I want to use a ThreadPoolExecutor
& implemented the following:
MyApp.java
public Executor getAsyncExecutor() {
return new ThreadPoolExecutor(10, 50, 30, TimeUnit.SECONDS, new SynchronousQueue<>(),
new CustomThreadFactory());
<code>...
@Override
public Executor getAsyncExecutor() {
return new ThreadPoolExecutor(10, 50, 30, TimeUnit.SECONDS, new SynchronousQueue<>(),
new CustomThreadFactory());
}
...
</code>
...
@Override
public Executor getAsyncExecutor() {
return new ThreadPoolExecutor(10, 50, 30, TimeUnit.SECONDS, new SynchronousQueue<>(),
new CustomThreadFactory());
}
...
MyService.java
<code>public void executeProcessA() {
// just after calling process B, the Hazelcast exception is thrown
public void executeProcessB() {
<code>public void executeProcessA() {
...
executeProcessB();
// just after calling process B, the Hazelcast exception is thrown
...
}
@Async
public void executeProcessB() {
...
}
</code>
public void executeProcessA() {
...
executeProcessB();
// just after calling process B, the Hazelcast exception is thrown
...
}
@Async
public void executeProcessB() {
...
}
CustomThreadFactory.java
<code>import java.util.concurrent.ThreadFactory;
public class CustomThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
return new CustomThread(r);
private static class CustomThread extends Thread {
private CustomThread(final Runnable r) {
setContextClassLoader(Thread.currentThread().getContextClassLoader());
<code>import java.util.concurrent.ThreadFactory;
public class CustomThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
return new CustomThread(r);
}
private static class CustomThread extends Thread {
private CustomThread(final Runnable r) {
super(r);
setContextClassLoader(Thread.currentThread().getContextClassLoader());
}
}
}
</code>
import java.util.concurrent.ThreadFactory;
public class CustomThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
return new CustomThread(r);
}
private static class CustomThread extends Thread {
private CustomThread(final Runnable r) {
super(r);
setContextClassLoader(Thread.currentThread().getContextClassLoader());
}
}
}
Current Situation
In executeProcessA()
of MyService.java
, just after executeProcessB()
is called, the following Hazelcast exception is thrown.
<code>com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult Error happens during Process ABC: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult
<code>com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult Error happens during Process ABC: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult
</code>
com.hazelcast.nio.serialization.HazelcastSerializationException: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult Error happens during Process ABC: java.lang.ClassNotFoundException: com.company.group.common.model.APIResult
Is there anything fundamentally different between a ForkJoinPool
& ThreadPoolExecutor
that I’m missing in my implementation?