use of org.motechproject.event.MotechEvent in project motech by motech.
the class ServerEventRelayTest method shouldThrowExceptionWhenCallbackServiceOfTheGivenNameIsNotFound.
@Test(expected = CallbackServiceNotFoundException.class)
public void shouldThrowExceptionWhenCallbackServiceOfTheGivenNameIsNotFound() throws Exception {
MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER);
motechEvent.setCallbackName(TEST_SERVICE_CALLBACK);
setUpListeners(SUBJECT, eventListener);
eventRelay.relayQueueEvent(motechEvent);
verify(eventListener).handle(motechEvent);
verify(callbackService).successCallback(motechEvent);
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class ServerEventRelayTest method shouldStopRetryingEventHandlingAfterMaxRedeliveryCountIsHitWhenRelyingTopicEvent.
@Test
public void shouldStopRetryingEventHandlingAfterMaxRedeliveryCountIsHitWhenRelyingTopicEvent() {
final BooleanValue handled = new BooleanValue(false);
when(motechEventConfig.getMessageMaxRedeliveryCount()).thenReturn(2);
when(eventListener.getIdentifier()).thenReturn("retrying");
doThrow(new RuntimeException()).doThrow(new RuntimeException()).doThrow(new RuntimeException()).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
handled.setValue(true);
return null;
}
}).when(eventListener).handle(any(MotechEvent.class));
setUpListeners(SUBJECT, eventListener);
eventRelay.relayTopicEvent(new MotechEvent(SUBJECT));
verify(eventListener, times(3)).handle(any(MotechEvent.class));
assertFalse(handled.getValue());
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class ServerEventRelayTest method shouldNotifyCallbackServiceOnSuccessfulEventHandling.
@Test
public void shouldNotifyCallbackServiceOnSuccessfulEventHandling() throws Exception {
MotechEvent motechEvent = createEvent(LISTENER_IDENTIFIER);
motechEvent.setCallbackName(TEST_SERVICE_CALLBACK);
setUpListeners(SUBJECT, eventListener);
when(bundleContext.getServiceReferences(EventCallbackService.class, null)).thenReturn(Arrays.asList(serviceReference));
when(bundleContext.getService(serviceReference)).thenReturn(callbackService);
when(callbackService.getName()).thenReturn(TEST_SERVICE_CALLBACK);
eventRelay.relayQueueEvent(motechEvent);
verify(eventListener).handle(motechEvent);
verify(callbackService).successCallback(motechEvent);
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class MotechEventHeaderMapper method fromHeaders.
/**
* {@inheritDoc}. Additionally sets <code>AMQ_SCHEDULED_DELAY</code> using
* <code>MotechEventConfig</code> variables.
*/
@Override
public void fromHeaders(MessageHeaders messageHeaders, Message message) {
super.fromHeaders(messageHeaders, message);
try {
MotechEvent motechEvent = (MotechEvent) ((ActiveMQObjectMessage) message).getObject();
if (motechEvent.isInvalid()) {
long redeliveryCount = motechEvent.getMessageRedeliveryCount();
Double delay = motechEventConfig.getMessageRedeliveryDelay() * MILLIS_PER_SEC * ((Math.pow(2, redeliveryCount - 1)));
LOGGER.debug("Redelivering " + motechEvent + " after " + delay + " millis.");
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay.longValue());
}
} catch (JMSException e) {
LOGGER.error("Failed to set header", e);
}
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class EventBundleIT method testEventWithTypedPayloadAndMetadata.
@Test
public void testEventWithTypedPayloadAndMetadata() throws Exception {
final ArrayList<MotechEvent> receivedEvents = new ArrayList<>();
registry.registerListener(new EventListener() {
@Override
public void handle(MotechEvent event) {
receivedEvents.add(event);
synchronized (waitLock) {
waitLock.notify();
}
}
@Override
public String getIdentifier() {
return "event";
}
}, "event");
Map<String, Object> params = new HashMap<>();
params.put("foo", new TestEventPayload());
Map<String, Object> metadata = new HashMap<>();
metadata.put("meta", new TestEventPayload());
metadata.put("theNumberSeven", 7);
wait2s();
MotechEvent testEvent = new MotechEvent("event", params);
testEvent.setMetadata(metadata);
eventRelay.sendEventMessage(testEvent);
wait2s();
assertEquals(1, receivedEvents.size());
MotechEvent receivedEvent = receivedEvents.get(0);
assertTrue(receivedEvent.getParameters().get("foo") instanceof TestEventPayload);
assertEquals(2, receivedEvent.getMetadata().size());
assertTrue(receivedEvent.getMetadata().containsKey("meta"));
assertTrue(receivedEvent.getMetadata().containsKey("theNumberSeven"));
assertTrue(receivedEvent.getMetadata().get("meta") instanceof TestEventPayload);
assertEquals(7, receivedEvent.getMetadata().get("theNumberSeven"));
}
Aggregations