IDDD_Samples
IDDD_Samples copied to clipboard
Can anyone please tell me the purpose of ThreadLocal in DomainEventPublisher?
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 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 example, when a subscriber's handle() method has been called back on, the handle() method could attempt to register another subscriber. Even though it's all happening on a single thread, that would cause a ConcurrentModificationException on the collection.
On Thu, Oct 24, 2013 at 3:40 AM, Leonardo Fernandez < [email protected]> wrote:
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).
— Reply to this email directly or view it on GitHubhttps://github.com/VaughnVernon/IDDD_Samples/issues/6#issuecomment-26978779 .
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
thread_A subscribe(payEventSubscriber)
thread_B publish(payEvent)
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 [email protected] 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 https://github.com/VaughnVernon/IDDD_Samples/issues/6#issuecomment-387633272, or mute the thread https://github.com/notifications/unsubscribe-auth/ACg87DitDyKrtawplyEFUrcon0p0XIygks5twouwgaJpZM4An-9- .
i have known that after reading the code. thanks