Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010. Axon Framework
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.axonframework.eventhandling;

Provides details about the current status of an event handling transaction. This method is typically accessed through the beforeTransaction and TransactionManager.afterTransaction(org.axonframework.eventhandling.TransactionStatus) methods on TransactionManager, but may also be obtained through the static current() method.

All instance methods in this class are meant to be used in a single thread and are therefore not thread-safe. The static methods are thread-safe.

Author(s):
Allard Buijze
Since:
0.3
 
 public class TransactionStatus {
 
 
     private int eventsProcessedSinceLastYield = 0;
     private int eventsProcessedInTransaction = 0;
     private int maxTransactionSize = 50;
     private Throwable exception;
     private long retryInterval = 5000;

    
Initialize a TransactionStatus instance with default settings.
 
     protected TransactionStatus() {
         // construction limited to this package
     }

    
Returns the TransactionStatus object related to a transaction running on the current thread. Returns null if no transaction is running on the current thread.

Returns:
the currently active TransactionStatus, or null if none is present.
 
     public static TransactionStatus current() {
         return .get();
     }

    
Clears the TransactionStatus related to the current thread.
 
     protected static void clear() {
         .remove();
     }

    
Sets the TransactionStatus object related to the transaction running in the current thread. If a previous value exists, it is overwritten.

Parameters:
newStatus The TransactionStatus for the current transaction
 
     protected static void set(TransactionStatus newStatus) {
         .set(newStatus);
     }

    
Returns the number of events processed (so far) in the current transaction.

Returns:
the number of events processed (so far) in the current transaction.
 
     public int getEventsProcessedInTransaction() {
         return ;
     }

    
Returns the number of events processed (so far) since the scheduler last yielded to other threads. If the scheduler never yielded, it indicates the total number of events processed.

Returns:
the number of events processed (so far) since the scheduler last yielded
 
     public int getEventsProcessedSinceLastYield() {
         return ;
     }

    
Sets the YieldPolicy for the current transaction. Defaults to YIELD_AFTER_TRANSACTION.

Parameters:
yieldPolicy The YieldPolicy to use for the current transaction
    public void setYieldPolicy(YieldPolicy yieldPolicy) {
        this. = yieldPolicy;
    }

    
Returns the YieldPolicy applicable to the current transaction.

Returns:
the YieldPolicy applicable to the current transaction
    public YieldPolicy getYieldPolicy() {
        return ;
    }

    
Forces the EventProcessingScheduler to immediately yield to other schedulers after processing this event. The current transaction will be closed normally.
    public void requestImmediateYield() {
        requestImmediateCommit();
    }

    
Requests the EventProcessingScheduler to commit the transaction immediately. Note that if this method is called before any events have been processed, the transaction will close without processing any events.
    public void requestImmediateCommit() {
    }

    
Returns the maximum number of events that may be processed inside the current transaction.

Returns:
the maximum number of events in the current transaction
    public int getMaxTransactionSize() {
        return ;
    }

    
Sets the maximum number of events to process inside the current transaction. The scheduler will commit a transaction if this number (or more) events have been processed inside the current transaction.

Defaults to the number of events in the queue at the moment the transaction started.

Parameters:
maxTransactionSize The number of events to process in the current transaction
    public void setMaxTransactionSize(int maxTransactionSize) {
        this. = maxTransactionSize;
    }

    
Sets the retry policy for the current transaction. Is set to SKIP_FAILED_EVENT by default.

Typically, exceptions are caused by programming errors or the underlying processing environment that the event handler uses, such as a database. In the former, there is no point in retrying the event processing. It would cause an unlimited loop in event processing, making an application vulnerable to a poisonous message attack. In the latter case, the event listener should identify which exception is transitive (i.e. might make a chance when retried), and which is not. If an exception is transitive, either RETRY_TRANSACTION or RETRY_LAST_EVENT should be chosen.

Furthermore, the policy choice should be based on the effect of a transaction rollback. Some data sources, such as databases, roll back the entire transaction. In that case, choose RETRY_TRANSACTION policy. If a rollback on the underlying data source only rolls back the last modification, choose RETRY_LAST_EVENT.

If failed events should be ignored altogether, choose the SKIP_FAILED_EVENT policy.

These policies may be set in both the beforeTransaction() and afterTransaction methods. The latter would allow you to change policy based on the exact type of exception encountered.

Parameters:
retryPolicy the retry policy to apply when a transaction fails.
    public void setRetryPolicy(RetryPolicy retryPolicy) {
        this. = retryPolicy;
    }

    
Returns the retry policy for the current transaction

Returns:
the retry policy for the current transaction
See also:
setRetryPolicy(org.axonframework.eventhandling.RetryPolicy)
    public RetryPolicy getRetryPolicy() {
        return ;
    }

    
Record the fact that an event has been processed. This will increase the number of events processed in current transaction as well as the number of events since last yield.
    protected void recordEventProcessed() {
    }

    
Resets the event count for current transaction to 0 and sets the YieldPolicy to the default value (YIELD_AFTER_TRANSACTION).
    protected void resetTransactionStatus() {
         = 0;
         = null;
    }

    
Indicates whether or not the maximum amount of events have been processed in this transaction.

Returns:
true if the maximum amount of events was handled, otherwise false.
    protected boolean isTransactionSizeReached() {
    }

    
Returns the current retry interval. This is the number of milliseconds processing should wait before retrying this transaction. Defaults to 5000 milliseconds.

Note that negative values will result in immediate retries, making them practically equal to a value of 0.

Returns:
the current retry interval
    public long getRetryInterval() {
        return ;
    }

    
Sets the retry interval for the current transaction. his is the number of milliseconds processing should wait before retrying this transaction. Defaults to 5000 milliseconds.

Parameters:
retryInterval the number of milliseconds to wait before retrying the transaction
    public void setRetryInterval(long retryInterval) {
        this. = retryInterval;
    }

    
Indicates whether the current transactional batch is executed successfully. If a batch is currently in progress, this will indicate if an error has been discovered so far.

Returns:
whether the current transaction is successful or not.
    public boolean isSuccessful() {
        return  == null;
    }

    
Returns the exception that caused the transaction to be marked as failed. Returns null if transaction is successful. Use isSuccessful() to find out if transaction was successful or not.

Returns:
the exception that caused the transaction to fail
    public Throwable getException() {
        return ;
    }

    
Mark the current transaction as failed.

Parameters:
cause the exception that caused the transaction to fail
    protected void markFailed(Throwable cause) {
        this. = cause;
    }
New to GrepCode? Check out our FAQ X