简体   繁体   English

锁定从Singleton EJB到无状态会话Bean的传播

[英]Lock propagation from Singleton EJB to Stateless Session Bean

I have this EJB Singleton (EJB 3.1): 我有这个EJB Singleton(EJB 3.1):

@Singleton
@Startup
@Lock(LockType.READ)
public class SingletonExample {

@EJB
private StatelessSBExample stlsb;
...
    @Schedule(..........., persistent = false)
    @AccessTimeout(0)
    @Lock(LockType.READ)
    public void call1SB() {
         stlsb.doSomething();
    }

    @Schedule(..........., persistent = false)
    @AccessTimeout(0)
    @Lock(LockType.READ)
    public void call2SB() {
        stlsb.doSomething();
    }
}

My bean is a tradicional EJB Stateless Session Bean: 我的bean是传统的EJB无状态会话Bean:

@Stateless
public class StatelessSBExample {
    public void domSomething() {
    ...
    }
}

Monitoring with visualvm, I realized that some threads are accumulating. 通过visualvm进行监视,我意识到一些线程正在累积。 The application started with Thread Live Peak = 92 and is now 102. And it is increasing. 该应用程序以Thread Live Peak = 92开始,现在是102。并且还在不断增加。 In VisualVM Threads, I have several threads with the status "Park" and "Wait". 在VisualVM线程中,我有几个状态为“ Park”和“ Wait”的线程。 In my Thread Dump I have a lot: 在我的线程转储中,我有很多:

"Thread-42" - Thread t@190
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <71bfce05> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

and

"__ejb-thread-pool13" - Thread t@130
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <5cfe398e> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

Where is my fault? 我的错在哪里 I just want to execute call1SB() and, if it is running, doesnt execute this method again (the same to call2SB) 我只想执行call1SB(),并且如果它正在运行,就不会再次执行此方法(与call2SB相同)

PS I can not use LockType.WRITE because I want to execute call1SB() and call2SB() at the same time (I dont have attributes in my Singleton.. only methods) PS我不能使用LockType.WRITE,因为我想同时执行call1SB()和call2SB()(我的Singleton..only方法中没有属性)

The default EJB locking mechanisms are working fine for common use cases, but they are not very flexible. 缺省的EJB锁定机制在常见的用例中可以很好地工作,但是它们并不十分灵活。 In such a case here I would suggest to use your own Locking mechanism like this: 在这种情况下,我建议使用您自己的锁定机制,如下所示:

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

public void call1SB() {
    if(lock.writeLock().tryLock()) { 
    // Acquires the write lock only if it 
    // is not held by another thread at the time of invocation.
        stlsb.doSomething();
    } // else { return; } // do nothing if already locked
}

Same with a second lock for your second Singleton method. 与第二个Singleton方法的第二个锁相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM