Reputation: 4318
I am calling rabbitMQ client (and not server) from inside my Java code running in Glassfish V4.0 (build 89) container to connect to rabbitmq server on another machine. As per rabbitmq client document, we should supply a ManagedThreadFactory instance to rabbitmq connection for creating threads.
I tried injecting DefaultManagedThreadFactory using
ctx.lookup("java:comp/DefaultManagedThreadFactory")
That failed with
Lookup failed for 'java:comp/DefaultManagedThreadFactory' in SerialContext...
Trying @Resource(lookup="concurrent/__DefaultManagedThreadFactory") results in NPE.
I am not heavy into java EE and I am using this container just as a frontend for my jersey web services. Clearly I need to do something more/different. However I have been not able to locate much apart from
http://javahowto.blogspot.in/2011/02/how-to-create-and-look-up-thread-pool.html
Can any Java EE expert tell me the right Voodoo incantations to make this work?
@Resource(name = "concurrent/__defaultManagedThreadFactory")
ManagedThreadFactory threadFactory;
// set on rabbitmq connection Factory
factory.setThreadFactory(threadFactory);
java.lang.NullPointerException
at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1312)
at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1233)
at java.util.concurrent.Executors.newFixedThreadPool(Executors.java:114)
at com.rabbitmq.client.impl.ConsumerWorkService.<init>(ConsumerWorkService.java:36)
axel - there is nothing special about my case. Just trying a rabbitmq java client inside GFV4 is what I am looking at. so to reproduce the issues
I can see ManagedThreadFactory name from GF Admin console so it should be there.
The Managed Thread Factory and Managed executor service can be located using JNDI. However the same resources cannot be injected into my code that uses Jersey web services. The resource injection works with a simple servlet example (e.g. java EE7 example on Github)
is this due to some interplay of jersey/Hk2/CDI/weld etc.? I am looking for an authorative answer to why I cannot make resource injection work?
Upvotes: 4
Views: 2387
Reputation: 164
I'm injecting the default managed thread factory like this in my Java EE projects that run on Glassfish 4:
@Singleton
@Startup
public class Messenger {
@EJB
MyMessenger me;
@Resource(name = "concurrent/__defaultManagedThreadFactory")
ManagedThreadFactory threadFactory;
@Resource
ManagedExecutorService executorService;
@PostConstruct
public void postConstruct() {
me.waitAndInitialize();
}
@Asynchronous
public Future<?> waitAndInitialize() {
try {
final AtomicInteger done = new AtomicInteger(0);
int i = 0;
while (done.intValue() == 0 && i < 20) {
i++;
getExecutorService().submit(
new Runnable() {
@Override
public void run() {
int incrementAndGet = done.incrementAndGet();
}
});
Thread.sleep(500);
}
if (done.intValue() == 0) {
Logger.getAnonymousLogger().severe("Waited a long time for the ExecutorService do become ready, but it never did. Will not initialize!");
} else {
init();
}
} catch (Exception e) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Exception in waitAndInitialize: " + e.getMessage(), e);
}
return new AsyncResult<>(null);
}
protected void init() {
connectionFactory.setExecutorService(executorService);
connectionFactory.setThreadFactory(threadFactory);
// connect to rabbit and listen to queues
}
}
Output of asadmin list-containers
List all known application containers
resources_ear
resources
ejb
weld
weld
grizzly
web
connector
webservices
appclient
jpa
jpa
ear
osgi
security
Upvotes: 1