-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Whether to consider adding capability to wait to acquire lock #1223
Comments
Hi, thanks for feedback. The best way you can do it is to wrap |
👍 |
Do you have any code which does this already? I'd like to use this functionality too! What i have done is a custom Here's my implementation, along with a Spring Boot test which demonstrates it: class AllCallsAreSerializedLockProvider(private val lockProvider: LockProvider) : LockProvider {
override fun lock(lockConfiguration: LockConfiguration): Optional<SimpleLock> {
var lockObtained: Optional<SimpleLock>
do {
lockObtained = lockProvider.lock(lockConfiguration)
if (lockObtained.isEmpty) TimeUnit.MILLISECONDS.sleep(10)
else break
} while (true)
return lockObtained
}
}
class TestAllCallsAreSerializedLockProvider : BaseIntegrationTest() {
@Autowired
lateinit var lockProvider: LockProvider
val lockExecutorutor: LockingTaskExecutor by lazy { DefaultLockingTaskExecutor(AllCallsAreSerializedLockProvider(lockProvider)) }
val executor = Executors.newFixedThreadPool(64)
@Test
fun `test lock from multiple tests`() {
val blockToExecute: Callable<Unit> = Callable { theFunctionWhichHasAlock() }
val tasks = List(15) { blockToExecute }
val responses = executor.invokeAll(tasks).map { it.get() }
println(responses)
}
fun theFunctionWhichHasAlock() {
val aa = Random().nextInt(64)
println("before lock $aa")
lockExecutorutor.executeWithLock(
Runnable {
println("work start $aa")
TimeUnit.SECONDS.sleep(10)
println("work end $aa")
},
LockConfiguration(/*createdAt*/ Instant.now(), /*name*/ "lockName", /*lockAtMostFor*/ 1.minutes.toJavaDuration(), /*lockAtLeastFor*/ 1.seconds.toJavaDuration()),
)
println("after lock $aa")
}
}
|
now, if cant get lock , will do nothing. whether to consider adding capability to wait lock
The text was updated successfully, but these errors were encountered: