2010-04-08 100 views
-1

我試圖從腳步調用一些ejb bean方法。並得到錯誤:是否正確通過ThreadPoolExecutor從線程調用ejb bean?

(如GlassFish的第三版)

日誌級別

嚴重記錄儀
javax.enterprise.system.std.com.sun.enterprise.v3.services.impl 名稱 - 值對
{_ThreadName =線程1,_ThreadID = 42} 記錄編號928消息ID
顯示java.lang.NullPointerException在 ua.co.rufous.server.broker.TempLicService.run(TempLi cService.java 在 java.util.concurrent.ThreadPoolExecutor中$ Worker.runTask(ThreadPoolExecutor.java:886) 完成消息35)在 java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run(Thread.java:637)

這裏踩

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); 
    } 

} 

我的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); 
    } 
} 

和方法,其中我把它(這是GWT的servlet,但沒有proble調用線程不包含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; 
     } 

我需要組織提交哈希服務器的一些游泳池(調用LicAtivator bean),是ThreadPoolExecutor設計好主意,爲什麼它不工作在我的情況? (因爲我知道我們不能在bean中創建線程,但是我們可以從不同的線程調用bean嗎?)。 如果不是,那麼組織這樣的練習如何請求池

謝謝。


  1. < < 答:我使用的DI(EJB 3.1)洙我不需要任何看起來在這裏。 (應用程序包裝在耳朵和兩個模塊(網絡模塊和ejb),它適用於我)。但是我只能在託管類中使用它。

所以..

2.可我在胎面使用手動樣子嗎? ----

3.我可以使用擴展ThreadPoolExecutor的Bean並調用另一個實現Runnable的bean嗎?或者它是不允許的?

+1

我可能會錯過一些東西,但是你在哪裏查找會話bean?如果您未初始化lActivator,則lActivator.proccessActivation(hash)將拋出NullPointerException。你的例子中@EJB注入不起作用。另請參閱http://stackoverflow.com/questions/848675/ejb-annotation-in-clients – 2010-04-08 11:01:22

+0

我正在使用DI(EJB 3.1)所以我不需要在這裏查找。 (應用程序包裝在耳朵和兩個模塊(網絡模塊和ejb),它適用於我)。但是我只能在託管類中使用它。 1.因此,我可以在Tread中使用手動查找? 2.我可以使用擴展ThreadPoolExecutor並調用另一個實現Runnable的bean的Bean嗎?或者它是不允許的? – 2010-04-08 11:42:51

回答

0

我只能在託管類中使用它來進行EJB注入。