Code Monkey home page Code Monkey logo

Comments (10)

TorbenRahbekKoch avatar TorbenRahbekKoch commented on June 25, 2024

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.

from iddd_samples.

VaughnVernon avatar VaughnVernon commented on June 25, 2024

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.

from iddd_samples.

leonfs avatar leonfs commented on June 25, 2024

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

from iddd_samples.

VaughnVernon avatar VaughnVernon commented on June 25, 2024

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//issues/6#issuecomment-26978779
.

from iddd_samples.

gaigeshen avatar gaigeshen commented on June 25, 2024

Yeah! I see.

from iddd_samples.

VaughnVernon avatar VaughnVernon commented on June 25, 2024

Please see Chapter 8 of my book.

from iddd_samples.

gaigeshen avatar gaigeshen commented on June 25, 2024

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

from iddd_samples.

batmanhaha avatar batmanhaha commented on June 25, 2024

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

it can`t work...

from iddd_samples.

VaughnVernon avatar VaughnVernon commented on June 25, 2024

from iddd_samples.

batmanhaha avatar batmanhaha commented on June 25, 2024

i have known that after reading the code.
thanks

from iddd_samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.