use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.
the class MessageChunkAggregator method getCorrelatorCallback.
@Override
protected EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext) {
return new CollectionCorrelatorCallback(muleContext, storePrefix) {
/**
* This method is invoked if the shouldAggregate method is called and returns true. Once this method returns an aggregated
* message the event group is removed from the router
*
* @param events the event group for this request
* @return an aggregated message
* @throws AggregationException if the aggregation fails. in this scenario the whole event
* group is removed and passed to the exception handler for this componenet
*/
@Override
public CoreEvent aggregateEvents(EventGroup events) throws AggregationException {
PrivilegedEvent[] collectedEvents;
try {
collectedEvents = events.toArray(false);
} catch (ObjectStoreException e) {
throw new AggregationException(events, MessageChunkAggregator.this, e);
}
CoreEvent firstEvent = collectedEvents[0];
Arrays.sort(collectedEvents, eventComparator);
ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
try {
for (PrivilegedEvent event : collectedEvents) {
baos.write(event.getMessageAsBytes(muleContext));
}
final Message.Builder builder = Message.builder(firstEvent.getMessage());
// try to deserialize message, since ChunkingRouter might have serialized the object...
try {
builder.value(muleContext.getObjectSerializer().getInternalProtocol().deserialize(baos.toByteArray()));
} catch (SerializationException e) {
builder.value(baos.toByteArray());
}
// Use last event, that hasn't been completed yet, for continued processing.
return PrivilegedEvent.builder(collectedEvents[collectedEvents.length - 1]).message(builder.build()).session(getMergedSession(events.toArray())).build();
} catch (Exception e) {
throw new AggregationException(events, MessageChunkAggregator.this, e);
} finally {
closeQuietly(baos);
}
}
};
}
use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.
the class EventGroup method getMessageCollectionEvent.
public CoreEvent getMessageCollectionEvent() {
try {
if (size() > 0) {
PrivilegedEvent[] muleEvents = toArray(true);
CoreEvent lastEvent = muleEvents[muleEvents.length - 1];
List<Message> messageList = Arrays.stream(muleEvents).map(event -> event.getMessage()).collect(toList());
final Message.Builder builder = Message.builder().collectionValue(messageList, Message.class);
PrivilegedEvent muleEvent = PrivilegedEvent.builder(lastEvent).message(builder.build()).session(getMergedSession(muleEvents)).build();
return muleEvent;
} else {
return null;
}
} catch (ObjectStoreException e) {
// Nothing to do...
return null;
}
}
use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.
the class AbstractAsyncRequestReplyRequester method retrieveEvent.
private PrivilegedEvent retrieveEvent(String correlationId) throws MuleException {
MultipleRequestReplierEvent multipleEvent = (MultipleRequestReplierEvent) store.retrieve(correlationId);
PrivilegedEvent event = multipleEvent.getEvent();
// TODO MULE-10302 remove this.
if (currentMuleContext.get() == null) {
try {
DeserializationPostInitialisable.Implementation.init(event, muleContext);
} catch (Exception e) {
throw new DefaultMuleException(e);
}
}
return event;
}
use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.
the class AbstractAsyncRequestReplyRequester method process.
@Override
public CoreEvent process(CoreEvent event) throws MuleException {
if (replyMessageSource == null) {
return processNext(event, empty());
} else {
addLock(event);
sendAsyncRequest(event);
PrivilegedEvent resultEvent = receiveAsyncReply(event);
if (resultEvent != null) {
// session properties. See MULE-5852
if (((InternalMessage) resultEvent.getMessage()).getInboundProperty(MULE_SESSION_PROPERTY) != null) {
((PrivilegedEvent) event).getSession().merge(resultEvent.getSession());
}
resultEvent = PrivilegedEvent.builder(event).message(resultEvent.getMessage()).build();
setCurrentEvent(resultEvent);
}
return resultEvent;
}
}
use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.
the class AbstractAsyncRequestReplyRequester method receiveAsyncReply.
private PrivilegedEvent receiveAsyncReply(CoreEvent event) throws MuleException {
String asyncReplyCorrelationId = getAsyncReplyCorrelationId(event);
System.out.println("receiveAsyncReply: " + asyncReplyCorrelationId);
Latch asyncReplyLatch = getLatch(asyncReplyCorrelationId);
// flag for catching the interrupted status of the Thread waiting for a
// result
boolean interruptedWhileWaiting = false;
boolean resultAvailable = false;
PrivilegedEvent result;
try {
if (logger.isDebugEnabled()) {
logger.debug("Waiting for async reply message with id: " + asyncReplyCorrelationId);
}
// how long should we wait for the lock?
if (timeout <= 0) {
asyncReplyLatch.await();
resultAvailable = true;
} else {
resultAvailable = asyncReplyLatch.await(timeout, MILLISECONDS);
}
if (!resultAvailable) {
asyncReplyLatch.await(1000, MILLISECONDS);
resultAvailable = asyncReplyLatch.getCount() == 0;
}
} catch (InterruptedException e) {
interruptedWhileWaiting = true;
} finally {
locks.remove(asyncReplyCorrelationId);
result = responseEvents.remove(asyncReplyCorrelationId);
if (interruptedWhileWaiting) {
Thread.currentThread().interrupt();
return null;
}
}
if (resultAvailable) {
if (result == null) {
// this should never happen, just using it as a safe guard for now
throw new IllegalStateException("Response MuleEvent is null");
}
// Copy event because the async-reply message was received by a different
// receiver thread (or the senders dispatcher thread in case of vm
// with queueEvents="false") and the current thread may need to mutate
// the even. See MULE-4370
setCurrentEvent(result);
return result;
} else {
addProcessed(new ProcessedEvents(asyncReplyCorrelationId, EndReason.FINISHED_BY_TIMEOUT));
if (failOnTimeout) {
notificationFirer.dispatch(new RoutingNotification(event.getMessage(), null, ASYNC_REPLY_TIMEOUT));
throw new ResponseTimeoutException(responseTimedOutWaitingForId((int) timeout, asyncReplyCorrelationId), null);
} else {
return null;
}
}
}
Aggregations