-
Notifications
You must be signed in to change notification settings - Fork 902
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
Can anyone please tell me the purpose of ThreadLocal in DomainEventPublisher? #6
Comments
The DomainEventPublisher (here) is a static and synchronous component where events are published synchronously in the same thread - and subscribed to by the same thread. The ThreadLocal is used to have storage specific to each thread, so different threads can publish at the same time without conflicting. |
I totally missed the original comment. The DomainEventPublisher has a static (class-level) interface for obtaining the instance that belongs to the current thread. However, there are also the instance methods publish(), subscribe(), reset(), that operate on the ThreadLocal instance. In any given thread there is likely only one or a few subscribers registered, so it's not a lot of overhead. Also note that at some point when a thread is assigned to an incoming server request, the subscriber list for the thread must be reset using DomainEventPublisher.instance().reset() so that subscriber(s) from the previous request using the same thread will not be left on the subscriber list. |
Looking at the implementation of the DomainEventPublisher I struggle to understand why do you have the publishing lock. Because you are running one instance per thread there are no potential concurrency issues. Why would you want to have a lock like that? Probably I'm missing some fundamental concept. But it seems to me quite redundant based on the fact that you have one instance per Thread (thanks to ThreadLocal). |
The DomainEventPublisher must protect itself from the subscribers. For On Thu, Oct 24, 2013 at 3:40 AM, Leonardo Fernandez <
|
Yeah! I see. |
Please see Chapter 8 of my book. |
Hum, I am reading this book at now, and thank you very much! It has been translated into Chinese. |
what if it can`t work... |
You don't understand. It's not multi-threaded. Even if it was it would
work, but it's not.
…On Tue, May 8, 2018, 11:24 PM batmanhaha ***@***.***> wrote:
what if
thread_A subscribe(payEventSubscriber)
thread_B publish(payEvent)
it can`t work...
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#6 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACg87DitDyKrtawplyEFUrcon0p0XIygks5twouwgaJpZM4An-9->
.
|
i have known that after reading the code. |
Can anyone please tell me the purpose of ThreadLocal in DomainEventPublisher? Does that mean every thread needs to maintain a list of its own subsriber list?
The text was updated successfully, but these errors were encountered: