is it right to call ejb bean from thread by ThreadPoolExecutor?
Posted
by kislo_metal
on Stack Overflow
See other posts from Stack Overflow
or by kislo_metal
Published on 2010-04-08T08:36:22Z
Indexed on
2010/04/08
13:33 UTC
Read the original article
Hit count: 350
I trying to call some ejb bean method from tread. and getting error :
(as is glassfish v3)
Log Level
SEVERE Logger
javax.enterprise.system.std.com.sun.enterprise.v3.services.impl Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=42} Record Number 928 Message ID
java.lang.NullPointerException at ua.co.rufous.server.broker.TempLicService.run(TempLicService.java Complete Message 35) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:637)
here is tread
public class TempLicService implements Runnable {
String hash;
//it`s Stateful bean
@EJB
private LicActivatorLocal lActivator;
public TempLicService(String hash) {
this.hash= hash;
}
@Override
public void run() {
lActivator.proccessActivation(hash);
}
}
my ThreadPoolExecutor
public class RequestThreadPoolExecutor extends ThreadPoolExecutor {
private boolean isPaused;
private ReentrantLock pauseLock = new ReentrantLock();
private Condition unpaused = pauseLock.newCondition();
private static RequestThreadPoolExecutor threadPool;
private RequestThreadPoolExecutor() {
super(1, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
System.out.println("RequestThreadPoolExecutor created");
}
public static RequestThreadPoolExecutor getInstance() {
if (threadPool == null)
threadPool = new RequestThreadPoolExecutor();
return threadPool;
}
public void runService(Runnable task) {
threadPool.execute(task);
}
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
pauseLock.lock();
try {
while (isPaused) unpaused.await();
} catch (InterruptedException ie) {
t.interrupt();
} finally {
pauseLock.unlock();
}
}
public void pause() {
pauseLock.lock();
try {
isPaused = true;
} finally {
pauseLock.unlock();
}
}
public void resume() {
pauseLock.lock();
try {
isPaused = false;
unpaused.signalAll();
} finally {
pauseLock.unlock();
}
}
public void shutDown() {
threadPool.shutdown();
}
//<<<<<< creating thread here
public void runByHash(String hash) {
Runnable service = new TempLicService(hash);
threadPool.runService(service);
}
}
and method where i call it (it is gwt servlet, but there is no proble to call thread that not contain ejb) :
@Override
public Boolean submitHash(String hash) {
System.out.println("submiting hash");
try {
if (tBoxService.getTempLicStatus(hash) == 1) {
//<<< here is the call
RequestThreadPoolExecutor.getInstance().runByHash(hash);
return true;
}
} catch (NoResultException e) {
e.printStackTrace();
}
return false;
}
I need to organize some pool of submitting hash to server (calls of LicActivator bean), is ThreadPoolExecutor design good idea and why it is not working in my case? (as I know we can`t create thread inside bean, but could we call bean from different threads? ). If No, what is the bast practice for organize such request pool?
Thanks.
- << Answer: I am using DI (EJB 3.1) soo i do not need any look up here. (application packed in ear and both modules in it (web module and ejb), it works perfect for me). But I can use it only in managed classes.
So..
2.Can I use manual look up in Tread ?
- Could I use Bean that extends ThreadPoolExecutor and calling another bean that implements Runnable ? Or it is not allowed ?
© Stack Overflow or respective owner