You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by jcmwright80 <jc...@gmail.com> on 2014/03/06 10:19:24 UTC
Delaying file/email/feed delivery until end of transactional
process
I have an application which has an end of day processing run which is made up
of multiple jobs all running under a single database transaction so that if
any of the individual jobs fail all db updates are rolled back to the start,
my problem is that many of the jobs create files, send emails or send feeds
to web services etc and in the event of the day end process failing these
have already been delivered.
I have been using camel for a while and I think it could be a good candidate
to be the middle man in this process so that my day end jobs all funnel
generated things to a camel route or dynamic router which is paused until
the transactional process completes, delivering the files to their
respective routes if the end of day run was successful or deleting
everything on failure.
Can anyone suggest a sensible way to achieve this goal using camel or
suggest a better alternative if you don’t think Camel should be used in this
way?
Thanks
Jim
--
View this message in context: http://camel.465427.n5.nabble.com/Delaying-file-email-feed-delivery-until-end-of-transactional-process-tp5748376.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Delaying file/email/feed delivery until end of transactional process
Posted by Henryk Konsek <he...@gmail.com>.
Hi Jim,
> I have an application which has an end of day processing run which is made up
> of multiple jobs all running under a single database transaction so that if
> any of the individual jobs fail all db updates are rolled back to the start,
> my problem is that many of the jobs create files, send emails or send feeds
> to web services etc and in the event of the day end process failing these
> have already been delivered.
If you are using Spring transactions, you can delay the moment of
sending e-mails and stuff like this after transaction commit with
TransactionSynchronizationManager API callback.
TransactionSynchronization txCallback = new TransactionSynchronizationAdapter(){
@Override
public void afterCompletion(int status) {
if(status == STATUS_COMMITTED){
emailSenderService.send(new Email(...));
}
}
};
TransactionSynchronizationManager.registerSynchronization(txCallback);
Keep in mind however that if your application crashes between commit
and execution of the callback, then e-mail won't be sent at all.
Assuming that you want to be sure that you've sent exactly one e-mail
per committed transaction, you need to use a bit more complex method.
In general you need to convert non-transactional email event into a
transactional resource (like DB row or JMS message). For example:
a) poll database for the committed data without "EMAIL_SENT" flag
b) in the same transaction, for the committed data without
"EMAIL_SENT" flag, enqueue e-mail send request to the ActiveMQ queue
c) in the same transaction mark data you polled from the DB with the
"EMAIL_SENT" flag
d) in the other transaction read messages from the ActiveMQ queue and
send an e-mail for each
The Camel route for the above recipe might look like as follows:
from("sql:select" + FIND_COMMITTED_DATA_WITHOUT_EMAIL_QUERY).
transacted(true).
to("jms:EmailsToSend").
to("sql:update" + FLAG_COMMITTED_DATA_WITH_EMAIL_SENT_FLAG);
from("jms:EmailsToSend").
transacted(true).
to("smtp:...");
Answering your next questions - you don't have to use XA transactions
here, but it would require you to involve idempotent consumer [2] into
the route.
Cheers.
[1] http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/transaction/support/TransactionSynchronizationManager.html
[2] http://camel.apache.org/idempotent-consumer.html
--
Henryk Konsek
http://henryk-konsek.blogspot.com