Skip to content

Commit

Permalink
Fix dispatcher for custom event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Jun 5, 2018
1 parent 7d1136a commit b889b3c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package uk.gov.justice.framework.tools.replay;

import static javax.transaction.Transactional.TxType.REQUIRES_NEW;
import static uk.gov.justice.services.core.annotation.Component.EVENT_LISTENER;
import static uk.gov.justice.services.core.annotation.ServiceComponentLocation.LOCAL;

import uk.gov.justice.services.core.dispatcher.Dispatcher;
import uk.gov.justice.services.core.dispatcher.DispatcherCache;
import uk.gov.justice.services.core.extension.ServiceComponentFoundEvent;
import uk.gov.justice.services.messaging.JsonEnvelope;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.transaction.Transactional;


@ApplicationScoped
public class TransactionalEnvelopeDispatcher {

@Inject
DispatcherCache dispatcherCache;

private Dispatcher dispatcher;

@PostConstruct
public void init() {
dispatcher = dispatcherCache.dispatcherFor(EVENT_LISTENER, LOCAL);
void register(@Observes final ServiceComponentFoundEvent event) {
dispatcher = dispatcherCache.dispatcherFor(event.getComponentName(), LOCAL);
}

@Transactional(REQUIRES_NEW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import uk.gov.justice.services.core.dispatcher.Dispatcher;
import uk.gov.justice.services.core.dispatcher.DispatcherCache;
import uk.gov.justice.services.core.extension.ServiceComponentFoundEvent;
import uk.gov.justice.services.messaging.JsonEnvelope;

import org.junit.Test;
Expand All @@ -29,14 +30,16 @@ public class TransactionalEnvelopeDispatcherTest {

@Test
public void shouldDispatchEnvelope() {
Dispatcher dispatcher = mock(Dispatcher.class);
final Dispatcher dispatcher = mock(Dispatcher.class);
final ServiceComponentFoundEvent serviceComponentFoundEventClass = mock(ServiceComponentFoundEvent.class);

when(serviceComponentFoundEventClass.getComponentName()).thenReturn(EVENT_LISTENER);
when(dispatcherCache.dispatcherFor(EVENT_LISTENER, LOCAL)).thenReturn(dispatcher);
transactionalEnvelopeDispatcher.init();

transactionalEnvelopeDispatcher.register(serviceComponentFoundEventClass);

final JsonEnvelope envelope = envelope().build();
transactionalEnvelopeDispatcher.dispatch(envelope);
verify(dispatcher).dispatch(envelope);

}
}

0 comments on commit b889b3c

Please sign in to comment.