# Class GzipInterceptor

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

All Implemented Interfaces:
  `ChannelInterceptor, GzipInterceptorMBean, Heartbeat, MembershipListener`

public class GzipInterceptor extends ChannelInterceptorBase implements GzipInterceptorMBean
Channel interceptor that compresses messages using GZIP before transmission and decompresses received messages. This reduces bandwidth usage for cluster communication.

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from interface ChannelInterceptor

  `ChannelInterceptor.InterceptorEvent`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `DEFAULT_BUFFER_SIZE`
  Default buffer size for compression/decompression operations.
  `static final int`
  `DEFAULT_OPTION_COMPRESSION_ENABLE`
  Default option flag to enable compression.
  `protected static final StringManager`
  `sm`
  StringManager for this class.

  ### Fields inherited from class ChannelInterceptorBase

  `optionFlag`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GzipInterceptor()`
  Default constructor for GzipInterceptor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static byte[]`
  `compress(byte[] data)`
  Compresses the given data using GZIP compression.
  `static byte[]`
  `decompress(byte[] data)`
  Decompresses the given data using GZIP decompression.
  `long`
  `getCompressedSizeRX()`
  Returns the total compressed received data size in bytes.
  `long`
  `getCompressedSizeTX()`
  Returns the total compressed transmitted data size in bytes.
  `int`
  `getCompressionMinSize()`
  Returns the minimum payload size for compression to be enabled.
  `int`
  `getCount()`
  Returns the total message count.
  `int`
  `getCountCompressedRX()`
  Returns the count of compressed received messages.
  `int`
  `getCountCompressedTX()`
  Returns the count of compressed transmitted messages.
  `int`
  `getCountUncompressedRX()`
  Returns the count of uncompressed received messages.
  `int`
  `getCountUncompressedTX()`
  Returns the count of uncompressed transmitted messages.
  `int`
  `getInterval()`
  Returns the number of messages between statistics reports being written to the log.
  `long`
  `getSizeRX()`
  Returns the total received data size in bytes.
  `long`
  `getSizeTX()`
  Returns the total transmitted data size in bytes.
  `boolean`
  `getStatsEnabled()`
  Returns whether the interceptor is configured to collect statistics.
  `long`
  `getUncompressedSizeRX()`
  Returns the total uncompressed received data size in bytes.
  `long`
  `getUncompressedSizeTX()`
  Returns the total uncompressed transmitted data size in bytes.
  `void`
  `messageReceived(ChannelMessage msg)`
  The `messageReceived` is invoked when a message is received.
  `void`
  `report()`
  Writes the current statistics report to the log.
  `void`
  `reset()`
  Resets all statistics counters to zero.
  `void`
  `sendMessage(Member[] destination, ChannelMessage msg, InterceptorPayload payload)`
  The `sendMessage` method is called when a message is being sent to one more destinations.
  `void`
  `setCompressionMinSize(int compressionMinSize)`
  Set the minimum payload size for compression to be enabled.
  `void`
  `setInterval(int interval)`
  If statistics collection is enabled, set the number of messages between statistics reports being written to the log.
  `void`
  `setStatsEnabled(boolean statsEnabled)`
  Configure whether the interceptor collects statistics.

  ### Methods inherited from class ChannelInterceptorBase

  `fireInterceptorEvent, getChannel, getLocalMember, getMember, getMembers, getNext, getOptionFlag, getPrevious, hasMembers, heartbeat, 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`

  ### Methods inherited from interface GzipInterceptorMBean

  `getOptionFlag, setOptionFlag`

- ## Field Details

  - ### sm

    protected static final StringManager sm
    StringManager for this class.

  - ### DEFAULT_BUFFER_SIZE

    public static final int DEFAULT_BUFFER_SIZE
    Default buffer size for compression/decompression operations.
    See Also:
      - Constant Field Values

  - ### DEFAULT_OPTION_COMPRESSION_ENABLE

    public static final int DEFAULT_OPTION_COMPRESSION_ENABLE
    Default option flag to enable compression.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### GzipInterceptor

    public GzipInterceptor()
    Default constructor for GzipInterceptor.

- ## 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

  - ### compress

    public static byte[] compress(byte[] data) throws IOException
    Compresses the given data using GZIP compression.
    Parameters:
      `data` - The data to compress
    Returns:
      The compressed data
    Throws:
      `IOException` - If a compression error occurs

  - ### decompress

    public static byte[] decompress(byte[] data) throws IOException
    Decompresses the given data using GZIP decompression.
    Parameters:
      `data` - The compressed data to decompress
    Returns:
      The decompressed data
    Throws:
      `IOException` - If a decompression error occurs

  - ### report

    public void report()
    Description copied from interface: `GzipInterceptorMBean`
    Writes the current statistics report to the log.
    Specified by:
      `report` in interface `GzipInterceptorMBean`

  - ### getCompressionMinSize

    public int getCompressionMinSize()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the minimum payload size for compression to be enabled.
    Specified by:
      `getCompressionMinSize` in interface `GzipInterceptorMBean`
    Returns:
      The minimum payload size in bytes

  - ### setCompressionMinSize

    public void setCompressionMinSize(int compressionMinSize)
    Description copied from interface: `GzipInterceptorMBean`
    Set the minimum payload size for compression to be enabled. A value of zero or less means compression will always be used. If not explicitly configured, a default of zero will be used.
    Specified by:
      `setCompressionMinSize` in interface `GzipInterceptorMBean`
    Parameters:
      `compressionMinSize` - The new minimum payload size

  - ### getStatsEnabled

    public boolean getStatsEnabled()
    Description copied from interface: `GzipInterceptorMBean`
    Returns whether the interceptor is configured to collect statistics.
    Specified by:
      `getStatsEnabled` in interface `GzipInterceptorMBean`
    Returns:
      `true` if statistics collection is enabled, otherwise `false`

  - ### setStatsEnabled

    public void setStatsEnabled(boolean statsEnabled)
    Description copied from interface: `GzipInterceptorMBean`
    Configure whether the interceptor collects statistics.
    Specified by:
      `setStatsEnabled` in interface `GzipInterceptorMBean`
    Parameters:
      `statsEnabled` - `true` to enable statistics collections, otherwise `false`

  - ### getInterval

    public int getInterval()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the number of messages between statistics reports being written to the log.
    Specified by:
      `getInterval` in interface `GzipInterceptorMBean`
    Returns:
      The interval between statistics reports

  - ### setInterval

    public void setInterval(int interval)
    Description copied from interface: `GzipInterceptorMBean`
    If statistics collection is enabled, set the number of messages between statistics reports being written to the log. A value of zero or less means no statistics reports are written.
    Specified by:
      `setInterval` in interface `GzipInterceptorMBean`
    Parameters:
      `interval` - The new interval between reports

  - ### getCount

    public int getCount()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total message count.
    Specified by:
      `getCount` in interface `GzipInterceptorMBean`
    Returns:
      Total number of messages processed

  - ### getCountCompressedTX

    public int getCountCompressedTX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the count of compressed transmitted messages.
    Specified by:
      `getCountCompressedTX` in interface `GzipInterceptorMBean`
    Returns:
      Number of compressed TX messages

  - ### getCountUncompressedTX

    public int getCountUncompressedTX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the count of uncompressed transmitted messages.
    Specified by:
      `getCountUncompressedTX` in interface `GzipInterceptorMBean`
    Returns:
      Number of uncompressed TX messages

  - ### getCountCompressedRX

    public int getCountCompressedRX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the count of compressed received messages.
    Specified by:
      `getCountCompressedRX` in interface `GzipInterceptorMBean`
    Returns:
      Number of compressed RX messages

  - ### getCountUncompressedRX

    public int getCountUncompressedRX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the count of uncompressed received messages.
    Specified by:
      `getCountUncompressedRX` in interface `GzipInterceptorMBean`
    Returns:
      Number of uncompressed RX messages

  - ### getSizeTX

    public long getSizeTX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total transmitted data size in bytes.
    Specified by:
      `getSizeTX` in interface `GzipInterceptorMBean`
    Returns:
      Total TX size in bytes

  - ### getCompressedSizeTX

    public long getCompressedSizeTX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total compressed transmitted data size in bytes.
    Specified by:
      `getCompressedSizeTX` in interface `GzipInterceptorMBean`
    Returns:
      Total compressed TX size in bytes

  - ### getUncompressedSizeTX

    public long getUncompressedSizeTX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total uncompressed transmitted data size in bytes.
    Specified by:
      `getUncompressedSizeTX` in interface `GzipInterceptorMBean`
    Returns:
      Total uncompressed TX size in bytes

  - ### getSizeRX

    public long getSizeRX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total received data size in bytes.
    Specified by:
      `getSizeRX` in interface `GzipInterceptorMBean`
    Returns:
      Total RX size in bytes

  - ### getCompressedSizeRX

    public long getCompressedSizeRX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total compressed received data size in bytes.
    Specified by:
      `getCompressedSizeRX` in interface `GzipInterceptorMBean`
    Returns:
      Total compressed RX size in bytes

  - ### getUncompressedSizeRX

    public long getUncompressedSizeRX()
    Description copied from interface: `GzipInterceptorMBean`
    Returns the total uncompressed received data size in bytes.
    Specified by:
      `getUncompressedSizeRX` in interface `GzipInterceptorMBean`
    Returns:
      Total uncompressed RX size in bytes

  - ### reset

    public void reset()
    Description copied from interface: `GzipInterceptorMBean`
    Resets all statistics counters to zero.
    Specified by:
      `reset` in interface `GzipInterceptorMBean`