use of won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent in project webofneeds by researchstudio-sat.
the class EventBotActionUtils method makeAndSubscribeRemoteResponseListener.
/**
* Creates a listener that waits for the remote response to the specified message. If a SuccessResponse is received,
* the successCallback is executed, if a FailureResponse is received, the failureCallback is executed.
* @param outgoingMessage
* @param successCallback
* @param failureCallback
* @param context
* @return
*/
public static EventListener makeAndSubscribeRemoteResponseListener(final WonMessage outgoingMessage, final EventListener successCallback, final EventListener failureCallback, EventListenerContext context) {
// create an event listener that processes the remote response to the wonMessage we're about to send
EventListener listener = new ActionOnFirstEventListener(context, OriginalMessageUriRemoteResponseEventFilter.forWonMessage(outgoingMessage), new BaseEventBotAction(context) {
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
if (event instanceof SuccessResponseEvent) {
successCallback.onEvent(event);
} else if (event instanceof FailureResponseEvent) {
failureCallback.onEvent(event);
}
}
});
context.getEventBus().subscribe(SuccessResponseEvent.class, listener);
context.getEventBus().subscribe(FailureResponseEvent.class, listener);
return listener;
}
use of won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent in project webofneeds by researchstudio-sat.
the class ExecuteSendMessageCommandAction method doRun.
/**
* Constructs the message via <code>createWonMessage</code> and registers a listener for the response from the
* local WoN node and, if <code>messageIsSenttoRemoteNode</code> is true, a listener for the response from the
* remote WoN node.
* @param event
* @param executingListener
*/
@Override
public final void doRun(Event event, EventListener executingListener) {
T messageCommandEvent = (T) event;
try {
// create the message
WonMessage message = createWonMessage(messageCommandEvent);
if (message == null) {
// assume that the implementation class handles logging and error event creation.
return;
}
// register listeners for the ResponseMessage generated by the own WoN node
EventBotActionUtils.makeAndSubscribeResponseListener(message, responseEvent -> {
if (responseEvent instanceof SuccessResponseEvent) {
SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
logger.debug(makeLogMessageString(event) + " succeeded on local WoN node");
Event eventToPublish = createLocalNodeSuccessEvent(messageCommandEvent, message, successEvent);
if (eventToPublish != null) {
getEventListenerContext().getEventBus().publish(eventToPublish);
}
}
}, responseEvent -> {
if (responseEvent instanceof FailureResponseEvent) {
FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
logger.info(makeLogMessageString(event) + " failed on local WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
Event eventToPublish = createLocalNodeFailureEvent(messageCommandEvent, message, failureEvent);
if (eventToPublish != null) {
getEventListenerContext().getEventBus().publish(eventToPublish);
}
}
}, getEventListenerContext());
// register listeners for the ResponseMessage generated by the remote WoN node
if (messageIsSentToRemoteNode) {
EventBotActionUtils.makeAndSubscribeRemoteResponseListener(message, responseEvent -> {
if (responseEvent instanceof SuccessResponseEvent) {
SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
logger.debug(makeLogMessageString(event) + " succeeded on remote WoN node");
Event eventToPublish = createRemoteNodeSuccessEvent(messageCommandEvent, message, successEvent);
if (eventToPublish != null) {
getEventListenerContext().getEventBus().publish(eventToPublish);
}
}
}, responseEvent -> {
if (responseEvent instanceof FailureResponseEvent) {
FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
logger.info(makeLogMessageString(event) + " failed on remote WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
Event eventToPublish = createRemoteNodeFailureEvent(messageCommandEvent, message, failureEvent);
if (eventToPublish != null) {
getEventListenerContext().getEventBus().publish(eventToPublish);
}
}
}, getEventListenerContext());
}
// send the message
getEventListenerContext().getWonMessageSender().sendWonMessage(message);
if (logger.isDebugEnabled()) {
logger.debug(makeLogMessageString(event));
}
} catch (Exception e) {
logger.warn("error executing messageCommandEvent: ", e);
getEventListenerContext().getEventBus().publish(createMessageNotSentEvent(messageCommandEvent, e.getMessage()));
}
}
use of won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent in project webofneeds by researchstudio-sat.
the class EventBotActionUtils method makeAndSubscribeResponseListener.
// ************************************************ EventListener ***************************************************
/**
* Creates a listener that waits for the response to the specified message. If a SuccessResponse is received,
* the successCallback is executed, if a FailureResponse is received, the failureCallback is executed.
* @param outgoingMessage
* @param successCallback
* @param failureCallback
* @param context
* @return
*/
public static EventListener makeAndSubscribeResponseListener(final WonMessage outgoingMessage, final EventListener successCallback, final EventListener failureCallback, EventListenerContext context) {
// create an event listener that processes the response to the wonMessage we're about to send
EventListener listener = new ActionOnFirstEventListener(context, OriginalMessageUriResponseEventFilter.forWonMessage(outgoingMessage), new BaseEventBotAction(context) {
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
if (event instanceof SuccessResponseEvent) {
successCallback.onEvent(event);
} else if (event instanceof FailureResponseEvent) {
failureCallback.onEvent(event);
}
}
});
context.getEventBus().subscribe(SuccessResponseEvent.class, listener);
context.getEventBus().subscribe(FailureResponseEvent.class, listener);
return listener;
}
Aggregations