DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Role of Data Annotation Services in AI-Powered Manufacturing
  • Annotating Data at Scale in Real Time
  • Data Annotation's Essential Role in Machine Learning Success
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues

Trending

  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • AI Speaks for the World... But Whose Humanity Does It Learn From?
  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  • Simpler Data Transfer Objects With Java Records
  1. DZone
  2. Data Engineering
  3. Data
  4. Adding Gzip Compression in CXF APIs and Interceptors

Adding Gzip Compression in CXF APIs and Interceptors

By 
Shan Arshad user avatar
Shan Arshad
·
Nov. 22, 14 · Interview
Likes (1)
Comment
Save
Tweet
Share
13.1K Views

Join the DZone community and get the full member experience.

Join For Free

Nowadays it has become mandatory to Gzipping the APIs response due to huge amount of data we are sending in response. It saves network bandwidth and delivery time and off course space over the internet.

While using CXF; it provides an option to use the Gzip Compression in no of ways.

  1. Blueprint
  2. Annotation

Blueprint:

<bean id="gZipInterceptor" class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor" />
<jaxrs:server id="rsServer" address="/gZip">
<jaxrs:outInterceptors>
<ref component-id="gZipInterceptor" />
</jaxrs:outInterceptors>
</jaxrs:server>

Annotation:

First you need to register the GZIPOutInterceptor in out interceptors list. For that you need to hook into CXF initialization classes.

public class InterceptorManager extends AbstractFeature {

private static final Logger LOGGER = Logger.getLogger( "simcore" );
private static final Interceptor< Message > GZIP = new GZIPOutInterceptor();
//private static final Interceptor< Message > GZIP = new GZIPOutInterceptor(512);

/* (non-Javadoc)
 * @see org.apache.cxf.feature.AbstractFeature#initializeProvider(org.apache.cxf.interceptor.InterceptorProvider, org.apache.cxf.Bus)
 */
@Override
protected void initializeProvider( InterceptorProvider provider, Bus bus ) {
/**
 * Adding Gzip interceptor to all outbound requests/responses
 */
LOGGER.debug( " ##############  Adding Gzip as OUT Interceptor ##############" );

provider.getOutInterceptors().add( GZIP );

}
}

GZIPOutInterceptor comes with an option to set the Threshold value as no of Bytes. If response size will be below this threshold value then it will not be compressed. It is extremely useful when we will be sending empty lists and status messages/codes only. Because compressing those small responses will be overhead at server side.

But there is another factor which is no of users requesting the response. So set this value by thinking over all the cases in mind.

@GZIP

Now we can use this annotation on any of our web-services controller to implement compression on all the APIs provided in that class.

@WebService
@Consumes ( { MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } )
@Produces ( MediaType.APPLICATION_JSON )
@GZIP
public interface WebServicesController {

        @GET
@Path ( "/myGzipData" )
@Produces ( { MediaType.APPLICATION_JSON } )
Response getZipData( );

}

Moreover we can set different parameters in Gzip annotation.

@GZIP ( force = true, threshold = 512 )
Annotation Delivery (commerce) Hook Data (computing) Bandwidth (computing) Overhead (computing) Factor (programming language) Internet (web browser) Space (architecture)

Published at DZone with permission of Shan Arshad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Role of Data Annotation Services in AI-Powered Manufacturing
  • Annotating Data at Scale in Real Time
  • Data Annotation's Essential Role in Machine Learning Success
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!