The relationship between channel and transaction in Flume-ng

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.