Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, August 5, 2017

Jetty Server 9.3 Configuration for Gzip Handler

What is Gzip Handler?
The Jetty GzipHandler is a compression handler that you can apply to any dynamic resource (servlet). Gzip fixes many of the bugs in commonly available compression filters: it works with asynchronous servlets and handles all ways to set content length. It has been tested with Jetty continuations and suspending requests. 
Compressing the content can greatly improve the network bandwidth usage, but at the cost of memory and CPU cycles. The DefaultServlet is capable of serving pre-compressed static content, which saves memory and CPU. By default, the GzipHandler will check to see if pre-compressed content exists, and pass the request through to be handled by the DefaultServlet.

Gzip Rules: 
GzipHandler will gzip the content of a response if:
It is mapped to a matching path
The request method is configured to support gzip
The request is not from an excluded User-Agent
accept-encoding header is set to gzip
The response status code is >=200 and <300 p="">
The content length is unknown or more than the minGzipSize initParameter or the minGzipSize is 0(default)
The content-type does not match an excluded mime-type
No content-encoding is specified by the resource

Implementation:
1. Remove Gzip Filter from application web.xml file
First of all, comment any Gzip filter tag given in the deployed application web.xml file as Gzip filter is depreciated in Jetty Server v9.3: 
Location:
<jetyy-server>\webapps\ROOT\WEB-INF\web.xml


<!-- <filter>

    <filter-name>GzipFilter</filter-name>

    <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>

     <async-supported>true</async-supported>

  </filter>

  <filter-mapping>

    <filter-name>GzipFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>  -->
2. Add Gzip Handler parameter in jetty server 

Set Gzip Handler parameter true in servlet tag of Jetty Server’s webdefault.xml:
Location:
<jetty-server>\etc\webdefault.xml


  <init-param>
      <param-name>gzip</param-name>
      <param-value>true</param-value>
  </init-param>

3. Modify configuration of Jetty Server Gzip Handler

We can modify configuration of Gzip Handler through jetty-gzip.xml file available at jetty server as shown below:
Location:
<jetty-server>\etc\jetty-gzip.xml



<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call name="insertHandler">
    <Arg>
      <New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
 <Set name="minGzipSize"><Property name="jetty.gzip.minGzipSize" deprecated="gzip.minGzipSize" default="2048"/></Set>

 <Set name="checkGzExists"><Property name="jetty.gzip.checkGzExists" deprecated="gzip.checkGzExists" default="false"/></Set>

 <Set name="compressionLevel"><Property name="jetty.gzip.compressionLevel" deprecated="gzip.compressionLevel" 
default="-1"/>
</Set>
 <Set name="excludedAgentPatterns">
   <Array type="String">
     <Item><Property name="jetty.gzip.excludedUserAgent" deprecated="gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item>
   </Array>
 </Set>

<Set name="includedMethods">
   <Array type="String">
     <Item>GET</Item>
        <Item>POST</Item>
   </Array>
</Set>  
<Set name="includedPaths">
   <Array type="String">
     <Item>/*</Item>   
   </Array>
</Set>
</New>
</Arg>
       </Call>
   </Configure>


Where minGzipSize denotes
Content will only be compressed if content length is either unknown or greater than minGzipSize.
checkGzExists
True by default. If set to false, the handler will not check for pre-compressed content.
compressionLevel
The compression level used for deflate compression. (0-9).
includedMethods
List of HTTP methods to compress. If not set, only GET requests are compressed.
excludedAgentPatterns
A list of regex patterns for User-Agent names from which requests should not be compressed.
includedPaths
List of paths to consider for compression.


4. Activation of Gzip Module

In Jetty Server 9.3, Gzip module is given in the name of gzip.mod(jetty-server-location\modules\gzip.mod) which we need to activate first before Jetty server start. And for activation there is a command given --add-to-start through which you can activate any module of jetty server.
First open command prompt from jetty server installed location and hit below command:
Syntax: java -jar start.jar --add-to-start=gzip


Now we have activated the GzipHandler to configuration of the server successfully.
After that you can start jetty server as per your normal stat up process.


5. Listing Active Modules of Jetty Server

If you want to check which module is running or activated at jetty server, --list-modules command would be given at jetty server startup time.   
Syntax: java -jar start.jar --list-modules


Above listing shows activated modules on Jetty Server 9.3 and this will give a brief about every activated module as well.


6. Checking Gzip Handler at Network Layer

You can check gzip activation at Network tab of browser by just Click on User small request rows button like as shown below:


Now you can check that size of response data reduced from 67.9 MB to 1.4 MB as expected which due to gzip handler activation.



When you click on any request like connect above and select Headers Tab,
Content-Encoding is also activated with gzip.



Reference: http://www.eclipse.org/jetty/documentation/9.3.x/gzip-filter.html

Sunday, October 9, 2016

Class and Interface

Although I want to write full article for a topic in JAVA but today I felt, some times small tips are more important than a full page theory when you want to revise quickly like for an interview ;)

I’m writing here few notes which I have collected while reading Java Books and hope these would be helpful for you too.

Final Classes:
  • String Class can’t be sub classed.
  • Final class obliterates a key benefit of OOP – Extensibility.
  • Final class uses for safety and security.
Abstract Classes:
  • In abstract class, method marked abstract end in a semicolon(;) rather than curly braces.
  • If you change a method from abstract to non-abstract then you need to change the semicolon at the end of the method declaration into a curly braces pair.
Interface:
  • All the interface method must be implemented and must be public and abstract(implicitly it is already done).
  • An interface is an 100% abstract class.
  • But an abstract class can have abstract or non-abstract methods, while an interface can have only abstract methods (this is the difference).
  • All variable defined in an interface must be public , static and final it means interface can declare only constants, not instance variables.
  • Interface methods must not be static as interfaces defines instance methods.
  • You can’t change the value of constant or variable defined in interface from implementing class, it will give compiler error.
Methods of Class Object:
  • Boolean equals(object obj)
  • void finalize()
  • int hashcode()
  • final void notify()
  • final void notifyall()
  • final void wait()
  • String toString()
toString() method:
  • This method simply spit out object state(or in other words) get the current values of the important instance variable.
  • When you pass an object reference to System.out.println() method it will call object.toString() method implicitly.
Hope you liked the tips, I will give move tips in my next post so don’t forget to give your feedback/comments as it motivates me to write more.
C YA BUDDY!!!

Lambda Expression Java 8

After long time I again found my old craze to write on Technical things. So today’s topic is Lambda Expression Java 8 which is very much hyped now a days.
Here are some important points for Lambda Expression:

Lambda Expression Java 8

  • The target type of Lambda Expression is the type of the context in which the Lambda Expression appears.
    For Example: A local variable that it’s assigned to or a method parameter that it’s gets passed into.
  • Although you haven’t declare the variable as final you still can’t use them as non-final variable. If they are to be used in Lambda Expression. If you do use them as a non-final variables, then the compiler will show as error.
  • Lambda Expression capture values not variables.
  • Lambda Expression are statically typed, so lets investigate the types of Lambda Expressions themselves. These types are called Functional interfaces.
  • A functional interface is an interface with a single abstract method that is used as the type of a Lambda Expression.
Important Functional Interfaces in Java
Interface Arguments Return
Predicate T Boolean
Consumer T Void
Function T R
Supplier None T
Unary Operator T T
Binary Operator (T,T) T
  • In the same way that Java 7 allowed to leave out the generic types for a constructor. Java 8 allows to leave out the types for whole parameters of Lambda Expression.
Type Inference:
Javac looks for information close to your Lambda Expression and use this information to figure out what would be correct type. It still typed checked and provide all the safety that you are used to, but you don’t have to state or write the types explicitly. This is called Type Inference.
Okey, today I am trying to approach new fundamentals for technologies and delivering in the form of notes. Feel free to comments or ask questions it will motivate me to indulge more with you ;)


Saturday, October 8, 2016

Quartz Scheduler with Spring 3


Quartz Scheduler with Spring 3:

For achieving scheduled task in spring 3 with the help of Quartz, we need following jars :

Spring 3.0
Quartz 1.8.6

Step 1:

First, add defines quartz details in context file of spring i.e. spring-context.xml as shown below:


<bean id="jobA" class="com.rockmesolid.jobs.JobA" />

<!-- Quartz Job -->
<bean name="JobA" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.rockmesolid.jobs.JobA" />
</bean>

<!-- Cron Trigger, run every 5 seconds -->
<bean id="cronTriggerJobA"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="JobA" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerJobA" />
</list>
</property>
</bean>


As you can see tag defines the bean class for the JobA.

<bean name=”JobA” class=”org.springframework.scheduling.quartz.JobDetailBean”>


It says Quartz bean class which will work with JobA class of property name=”jobClass”.
Next step is to set CronTriggerBean which has property of cronExpression


<property name=”cronExpression” value=”0/5 * * * * ?” />


Here “0/5 * * * * ?” defines CronTrigger will hit JobA class at every 5 Second.
If you want to hit it at a particular time on everyday like 10:13 pm everyday then cronExpression would be like “*10 13 * ? *”


<bean class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>

 

This set a reference bean name as cronTriggerJobA for Quartz SchedulerFactory.

Step 2:
Now definition of JobA is:


package com.rockmesolid.jobs;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.quartz.QuartzJobBean;

import org.springframework.web.context.support.SpringBeanAutowiringSupport;

public class JobA extends QuartzJobBean {

@Override

protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); // for calling or inject bean of current context of application

System.out.println("Job A is runing");

try {

// to perform some actions

} catch (Exception e) {

e.printStackTrace();

}

}

}


Here as we can see JobA extends QuartzJobBean for calling executeInternal method which passes JobExecutionContext as an argument.
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); is the most important line to call or inject bean of current context of application into the JobA class. After this line you can use any of your application bean class into JobA class and perform any action like business logic, db connection etc.
Hope it will help. Really it’s easy.