# Class TwoPhaseCommitInterceptor

java.lang.Object
org.apache.catalina.tribes.group.ChannelInterceptorBase
org.apache.catalina.tribes.group.interceptors.TwoPhaseCommitInterceptor

All Implemented Interfaces:
  `ChannelInterceptor, Heartbeat, MembershipListener`

public class TwoPhaseCommitInterceptor extends ChannelInterceptorBase
Two-phase commit interceptor that ensures reliable message delivery.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static final record`
`TwoPhaseCommitInterceptor.MapEntry`
Entry that holds a message, its ID, and the timestamp of arrival.

### Nested classes/interfaces inherited from interface ChannelInterceptor

`ChannelInterceptor.InterceptorEvent`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected boolean`
`deepclone`
Whether to deep clone messages before storage.
`protected long`
`expire`
Message expiration time in milliseconds.
`protected final Map<UniqueId, TwoPhaseCommitInterceptor.MapEntry>`
`messages`
Map of pending messages keyed by their unique ID.
`protected static final StringManager`
`sm`
The string manager for this package.

### Fields inherited from class ChannelInterceptorBase

`optionFlag`

## Constructor Summary

Constructors
Constructor
Description
`TwoPhaseCommitInterceptor()`
Constructs a new TwoPhaseCommitInterceptor.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`getDeepclone()`
Returns whether deep cloning is enabled.
`long`
`getExpire()`
Returns the message expiration time in milliseconds.
`void`
`heartbeat()`
The `heartbeat()` method gets invoked periodically to allow interceptors to clean up resources, time out object and perform actions that are unrelated to sending/receiving data.
`void`
`messageReceived(ChannelMessage msg)`
The `messageReceived` is invoked when a message is received.
`void`
`sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload)`
The `sendMessage` method is called when a message is being sent to one more destinations.
`void`
`setDeepclone(boolean deepclone)`
Sets whether deep cloning should be used.
`void`
`setExpire(long expire)`
Sets the message expiration time in milliseconds.

### Methods inherited from class ChannelInterceptorBase

`fireInterceptorEvent, getChannel, getLocalMember, getMember, getMembers, getNext, getOptionFlag, getPrevious, hasMembers, memberAdded, memberDisappeared, okToProcess, setChannel, setNext, setOptionFlag, setPrevious, start, stop`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### sm

protected static final StringManager sm
The string manager for this package.

### messages

protected final Map<UniqueId, TwoPhaseCommitInterceptor.MapEntry> messages
Map of pending messages keyed by their unique ID.

### expire

protected long expire
Message expiration time in milliseconds.

### deepclone

protected boolean deepclone
Whether to deep clone messages before storage.

## Constructor Details

### TwoPhaseCommitInterceptor

public TwoPhaseCommitInterceptor()
Constructs a new TwoPhaseCommitInterceptor.

## Method Details

### sendMessage

public void sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload) throws ChannelException
Description copied from interface: `ChannelInterceptor`
The `sendMessage` method is called when a message is being sent to one more destinations. The interceptor can modify any of the parameters and then pass on the message down the stack by invoking `getNext().sendMessage(destination,msg,payload)`.

Alternatively the interceptor can stop the message from being sent by not invoking `getNext().sendMessage(destination,msg,payload)`.

If the message is to be sent asynchronous the application can be notified of completion and errors by passing in an error handler attached to a payload object.

The ChannelMessage.getAddress contains Channel.getLocalMember, and can be overwritten to simulate a message sent from another node.

Specified by:
  `sendMessage` in interface `ChannelInterceptor`
Overrides:
  `sendMessage` in class `ChannelInterceptorBase`
Parameters:
  `destination` - Member[] - the destination for this message
  `msg` - ChannelMessage - the message to be sent
  `payload` - InterceptorPayload - the payload, carrying an error handler and future useful data, can be null
Throws:
  `ChannelException` - if a serialization error happens.
See Also:
  - `ErrorHandler`
  - `InterceptorPayload`

### messageReceived

public void messageReceived(ChannelMessage msg)
Description copied from interface: `ChannelInterceptor`
The `messageReceived` is invoked when a message is received. `ChannelMessage.getAddress()` is the sender, or the reply-to address if it has been overwritten.

Specified by:
  `messageReceived` in interface `ChannelInterceptor`
Overrides:
  `messageReceived` in class `ChannelInterceptorBase`
Parameters:
  `msg` - ChannelMessage

### getDeepclone

public boolean getDeepclone()
Returns whether deep cloning is enabled.

Returns:
  true if deep cloning is enabled

### getExpire

public long getExpire()
Returns the message expiration time in milliseconds.

Returns:
  the expiration time

### setDeepclone

public void setDeepclone(boolean deepclone)
Sets whether deep cloning should be used.

Parameters:
  `deepclone` - true to enable deep cloning

### setExpire

public void setExpire(long expire)
Sets the message expiration time in milliseconds.

Parameters:
  `expire` - the expiration time

### heartbeat

public void heartbeat()
Description copied from interface: `ChannelInterceptor`
The `heartbeat()` method gets invoked periodically to allow interceptors to clean up resources, time out object and perform actions that are unrelated to sending/receiving data.

Specified by:
  `heartbeat` in interface `ChannelInterceptor`
Specified by:
  `heartbeat` in interface `Heartbeat`
Overrides:
  `heartbeat` in class `ChannelInterceptorBase`