* will execute the processing of messages within a TransactionTemplate. This template will manage the
* transaction lifecycle for the list of messages associated with this receiver worker.
*/
public void processMessages() throws Exception
{
TransactionTemplate tt = new TransactionTemplate(endpoint.getTransactionConfig(), receiver.getConnector().getMuleContext());
// Receive messages and process them in a single transaction
// Do not enable threading here, but serveral workers
// may have been started
TransactionCallback<?> cb = new TransactionCallback()
{
public Object doInTransaction() throws Exception
{
Transaction tx = TransactionCoordination.getInstance().getTransaction();
if (tx != null)
{
bindTransaction(tx);
}
List<Object> results = new ArrayList<Object>(messages.size());
for (Object payload : messages)
{
payload = preProcessMessage(payload);
if (payload != null)
{
MuleMessage muleMessage = receiver.createMuleMessage(payload, endpoint.getEncoding());
preRouteMuleMessage(muleMessage);
// TODO Move getSessionHandler() to the Connector interface
SessionHandler handler;
if (endpoint.getConnector() instanceof AbstractConnector)
{
handler = ((AbstractConnector) endpoint.getConnector()).getSessionHandler();
}
else
{
handler = new MuleSessionHandler();
}
MuleSession session;
try
{
session = handler.retrieveSessionInfoFromMessage(muleMessage);
}
catch (SerializationException e)
{
// EE-1820 Support message headers generated by previous Mule versions
session = new LegacySessionHandler().retrieveSessionInfoFromMessage(muleMessage);
}
MuleEvent resultEvent;
if (session != null)
{
resultEvent = receiver.routeMessage(muleMessage, session, tx, out);
}
else
{
resultEvent = receiver.routeMessage(muleMessage, tx, out);
}
MuleMessage result = resultEvent == null ? null : resultEvent.getMessage();
if (result != null)
{
payload = postProcessMessage(result);
if (payload != null)
{
results.add(payload);
}
}
}
}
return results;
}
};
try
{
List results = (List) tt.execute(cb);
handleResults(results);
}
finally
{
messages.clear();