In sink and source (whether built or customized), the basic code is as follows:
...
Channel Channel = Getchannel ();
Transaction Transaction = Channel.gettransaction ();
Event event = null;
Status result = Status.ready;
Transaction.begin ();
...
event = Channel.take ();//getchannelprocessor (). processevent (event), used to sink the latter for source
...
Transaction.commit ();
Transaction.rollback ()
transaction.close ();
...
So some people are going to ask? From the above code, it seems that you just need to get channel, because you need only event = Channel.take () or
Getchannelprocessor (). processevent (event)? Is that right? You can remove transaction to try, the result shows is not good, error!
So why? This is a bit confusing, but actually the channel.take () operation is Transaction.dotake (). That is, the actual put and take operations are done in the transaction, so to use channel must first create transcation to use. The Channel.gettransaction () method is to get (already created) or create (not yet) the transcation,basicchannelsemantics corresponding code as follows:
@Override public
Transaction gettransaction () {
if (!initialized) {
synchronized (this) {
if (!). Initialized) {
initialize ();
initialized = true;
}
}
Basictransactionsemantics transaction = Currenttransaction.get ();//Get Transcation
if (transaction = NULL | | Transaction.getstate () equals (//If transaction does not exist or has been closed to create
BasicTransactionSemantics.State.CLOSED)) {
Transaction = CreateTransaction ()///Create
Currenttransaction.set (transaction);//Assign to Currenttransaction
} return
transaction;
}
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/
The method needs to implement protected abstract basictransactionsemantics in all channel parent class basicchannelsemantics, and then in the channel class that is implemented specifically. CreateTransaction () This abstract method to get the corresponding transaction object. Basicchannelsemantics further encapsulates the Transaction.take () and Transaction.put (event) methods into take () and put (event) methods, These methods are the Channel.take () and Channel.put (event) methods that are exposed to sink or source.
@Override public
Void put (event event) throws Channelexception {
Basictransactionsemantics transaction = Currenttransaction.get ();
Preconditions.checkstate (transaction!= null,
"No transaction exists for this thread");
Transaction.put (event);
}
@Override Public
Event take () throws Channelexception {
Basictransactionsemantics transaction = Currenttransaction.get ();
Preconditions.checkstate (transaction!= null,
"No transaction exists for this thread");
return Transaction.take ();
}
Author: cnblogs, Jiu-mad