Interface Transaction
- All Known Implementing Classes:
AbstractTransaction
,HttpClientTransaction
,SocketClientTransaction
Provides a transaction for performing site-to-site data transfers.
A Transaction is created by calling the
createTransaction(TransferDirection)
method of a
SiteToSiteClient
. The
resulting Transaction can be used to either send or receive data but not
both. A new Transaction must be created in order perform the other operation.
The general flow of execute of a Transaction is as follows:
- Create the transaction as described above.
- Send data via the
send(DataPacket)
method or receive data via thereceive()
method. This method will be called 1 or more times. In the case of receive, this method should be called until the method returnsnull
, signifying that the remote instance is finished sending data. Note:receive()
should not be called a second time without first fully consuming the stream from the previous Packet that was received. - Confirm the transaction via the
confirm()
method. - Either complete the transaction via the
invalid reference
#complete(boolean)
invalid reference
#cancel()
It is important that the Transaction be terminated in order to free the resources held by the Transaction. If a Transaction is not terminated, its resources will not be freed and if the Transaction holds connections from a connection pool, the connections in that pool will eventually become exhausted. A Transaction is terminated by calling one of the following methods:
-
invalid reference
#complete(boolean)
-
invalid reference
#cancel()
error()
If at any point an IOException is thrown from one of the methods of the
Transaction, that Transaction is automatically closed via a call to
error()
.
The Transaction class should not be assumed to be thread-safe.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Cancels this transaction, indicating to the sender that the data has not been successfully received so that the sender can retry or handle however is appropriate.complete()
Completes the transaction and indicates to both the sender and receiver that the data transfer was successful.void
confirm()
Confirms the data that was sent or received by comparing CRC32's of the data sent and the data received.void
error()
Sets the TransactionState of the Transaction toTransaction.TransactionState.ERROR
, and closes the Transaction.getState()
receive()
Retrieves information from the remote NiFi instance, if any is available.void
Sends the given byte array as the content of aDataPacket
along with the provided attributesvoid
send
(DataPacket dataPacket) Sends information to the remote NiFi instance.
-
Method Details
-
send
Sends information to the remote NiFi instance.- Parameters:
dataPacket
- the data packet to send- Throws:
IOException
- if unable to send
-
send
Sends the given byte array as the content of aDataPacket
along with the provided attributes- Parameters:
content
- to sendattributes
- of the content- Throws:
IOException
- if unable to send
-
receive
Retrieves information from the remote NiFi instance, if any is available. If no data is available, will returnnull
. It is important to consume all data from the remote NiFi instance before attempting to callconfirm()
. This is because the sender is always responsible for determining when the Transaction has finished. This is done in order to prevent the need for a round-trip network request to receive data for each data packet.- Returns:
- the DataPacket received, or
null
if there is no more data to receive. - Throws:
IOException
- if unable to receive
-
confirm
Confirms the data that was sent or received by comparing CRC32's of the data sent and the data received.
Even if the protocol being used to send the data is reliable and guarantees ordering of packets (such as TCP), it is still required that we confirm the transaction before completing the transaction. This is done as "safety net" or a defensive programming technique. Mistakes happen, and this mechanism helps to ensure that if a bug exists somewhere along the line that we do not end up sending or receiving corrupt data. If the CRC32 of the sender and the CRC32 of the receiver do not match, an IOException will be thrown and both the sender and receiver will cancel the transaction automatically.
If the
TransferDirection
of this Transaction is RECEIVE, this method will throw an Exception unless all data from the remote instance has been consumed (i.e., a call toreceive()
returnsnull
).If the
TransferDirection
of this Transaction is SEND, calling this method dictates that no more data will be sent in this transaction. I.e., there will be no more calls tosend(DataPacket)
.- Throws:
IOException
- if unable to confirm transaction
-
complete
Completes the transaction and indicates to both the sender and receiver that the data transfer was successful.
- Returns:
- a TransactionCompletion that contains details about the Transaction
- Throws:
IOException
- if unable to complete
-
cancel
Cancels this transaction, indicating to the sender that the data has not been successfully received so that the sender can retry or handle however is appropriate.
- Parameters:
explanation
- an explanation to tell the other party why the transaction was canceled.- Throws:
IOException
- if unable to cancel
-
error
void error()Sets the TransactionState of the Transaction to
Transaction.TransactionState.ERROR
, and closes the Transaction. The underlying connection should not be returned to a connection pool in this case. -
getState
- Returns:
- the current state of the Transaction.
- Throws:
IOException
- ioe
-
getCommunicant
Communicant getCommunicant()- Returns:
- a Communicant that represents the other side of this Transaction (i.e., the remote NiFi instance)
-