IDDD_Samples icon indicating copy to clipboard operation
IDDD_Samples copied to clipboard

Can anyone please tell me the purpose of ThreadLocal in DomainEventPublisher?

Open njzhxf opened this issue 11 years ago • 10 comments

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?

njzhxf avatar May 02 '13 13:05 njzhxf

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.

TorbenRahbekKoch avatar Aug 09 '13 13:08 TorbenRahbekKoch

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.

VaughnVernon avatar Aug 09 '13 16:08 VaughnVernon

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).

leonfs avatar Oct 24 '13 09:10 leonfs

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 .

VaughnVernon avatar Oct 24 '13 12:10 VaughnVernon

Yeah! I see.

gaigeshen avatar Jun 18 '15 09:06 gaigeshen

Please see Chapter 8 of my book.

VaughnVernon avatar Jun 18 '15 13:06 VaughnVernon

Hum, I am reading this book at now, and thank you very much! It has been translated into Chinese.

gaigeshen avatar Jun 18 '15 13:06 gaigeshen

what if
thread_A subscribe(payEventSubscriber) thread_B publish(payEvent)

it can`t work...

batmanhaha avatar May 09 '18 06:05 batmanhaha

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- .

VaughnVernon avatar May 09 '18 06:05 VaughnVernon

i have known that after reading the code. thanks

batmanhaha avatar May 09 '18 06:05 batmanhaha