Monday, 18 July 2011

Learn Java 7

Java 7  is an upcoming major update to Java, releasing on July 28, its upgrade/add many features.


Upgraded Features
- Java 7 update the XML and Web Service stack to current stable build JAXP 1.4, JAXB 2.2 and JAX-WS 2.2.
- Update the Nimbus Look and Feel (Next Cross Platform UI). Its already added Java 6 Update 10. check here


- Translucent and shaped Windows
       This feature is also included in java 6 update 10.  this features provide to create transparent and any shape window in java. for more info check this



New features in java 7
Small languages changes (Project Coin Part 1)
Project Coin is one of the  project in java 7 for java language spec (JLS). Project Coin have 2 parts. the first part is add to java 7. and next part is comes with java 8 in next year (2012).
  •  Binary literals and underscore in numeric literals
                   now we can assign binary values directly.

int currency=19_00_000;
int binary=0b100000;
    
System.out.println(binary);
System.out.println(currency);
the output of above code snippets is 32 and 1900000
  • Strings in switch statements

now we can use string literals in switch statements.
public static void main(String[] args) {
       String data="java 7";
       
       switch(data)
       {
           case "java":
           case "JAVA":
               System.out.println("Java");
               break;
           case "java 7":
           case "JAVA 7":
               System.out.println("Java 7");
                break;
            default:
                   System.out.println("Otherthan java");
              
       }
        
}
it prints "Java 7".


  •  Multi-catch statements and good re throw. Check Here
  •  try with resource stements. check here
  •  new diamond <> operator. check here

some other features of project coin will come with Java 8 in next year.

NIO 2 (New I/O 2)
        This one of the biggest update in java 7. It fill the gap between  underlaying OS and Java in terms of file manipulation. Oracle provide good tutorial about NIO 2 here

JLayer
     JLayer is a universal decorator for Swing components which enables you to implement various advanced painting effects as well as receive notifications of all AWTEvents generated within its borders. check this tutorial about how to decorate JLayer in existing swing component.

Support for dynamically-typed languages (InvokeDynamic)

       This feature is not for normal developers. Its only for those are porting his language into JVM. Check here for list of languages supported by JVM.

other features
  • Upgrade class-loader architecture
  • Support Uncode 6.0
  • Elliptic-curve cryptography (ECC)
  • JDBC 4.1
  • XRender Pipeline for mordern GPU

The Next Java 8 will give support for Modular JVM and modular programming (Project Jigsaw), Project Coin part 2, Project Lamda, collection literals and very importantly oracle going to merge JRockit into Hotspot VM.



comments are welcomed,





Monday, 11 July 2011

Running Multiple Tomcat Instances on Single Machine

In this post we will see how to run multiple tomcat instances on single machine and under single user account.
We first see the  tomcat directory structure. .





here each folder uses following purpose.

bin -  It contains all binary and script files for running tomcat.

lib - contains all shared libraries used for tomcat

conf - contains configuration information like which port tomcat can bind , etc...

logs - it contain all logging details

temp - this folder tomcat used for temporary files purpose

webapps - this folder is very important. here we put all application war files.

work - If application contain any jsp then jsp is translated and converted into servlet its stores here.


In when run the tomcat its uses 5 environment variables. They are 
CATALINA_HOME, CATALINA_BASE, CATALINA_TMPDIR, JRE_HOME/JAVA_HOME, CLASSPATH

in above list CATALINA_HOME and JAVA_HOME is mandatory environment variables. all others are optional and its can be calculated using CATALINA_HOME.



CATALINA_HOME - this environment variable should point to tomcat base folder, where tomcat binary are  installed/extracted. so based on CATALINA_HOME we can get bin and lib folder

CATALINA_BASE - If we not specified then CATALINA_HOME value is set. This variable pointed to configuration and webapps folder. Based on this variable server uses conf, logs, temp, webapps, work folders.

Usual ways to run the tomcat is only set CATALINA_HOME environment variable. and run the startup.sh script file. this startup.sh file automatically calculate and assign the values of other variables what we are not set.



startup.sh file set the environment variable and  then call catalina.sh file. this files is read CATALINA_BASE value and attach conf i.e $CATALINA_BASE/conf folder and get server.xml. this file is heart of tomcat. it contains all configuration information. like which tomcat uses as shoutdown port, connector post, host name, application folder ,.. for example usually tomcat uses 8080 is a connector port, so we can access http://localhost:8080/ 

if we set the $CATALINA_BASE explicitly then tomcat uses our variable to search and get the server.xml file from our target place, what we specified in CATALINA_BASE. 

this is trick to run multiple tomcat in single machine. we don't change CATALINA_HOME value. we need to change CATALINA_BASE value before start/shutdown the tomcat.


create one folder named "tomcat-instance1" anywhere, and copy conf, temp, webapps  folder from CATALINA_HOME folder and change conf/server.xml file in tomcat-instance1. we need to change 3 port shutdown port, connector port and ajp port.

shutdown port - this port is used for shutdown the tomcat. when we call the shutdown.sh script they send signal to shutdown port. this port listen by tomcat java process. if signal is received the that process then its cleanup and exit by itself.

connector Port -This port is actual port to expose the application to outside client. 

ajp port - this port is used to apache httpd server  communicate to tomcat. this port used when we setup load balanced server.

see the sample server.xml file
.... ..


.....
 


so we change these three port to different number, because once this port is binded  then other process can't bind it again. so wee bind different port. so tomcat-instance1/conf/server.xml file i configured server port =8105, connector port = 8181, ajp port = 8109.

.....
 


now we can create two script file for startup and shutdown the tomcat-instance1.
startup-instance1.sh
export CATALINA_BASE= /home/ramki/tomcat-instance1
cd $CATALINA_HOME/bin
./startup.sh





shutdown-instance1.sh
export CATALINA_BASE= /home/ramki/tomcat-instance1
cd $CATALINA_HOME/bin
./shutdown.sh


here we explicitly set the CATALINA_BASE variable and point to new tomcat-instance1
the we go to CATALINA_HOME/bin folder because all binary for running tomcat is still present in CATALINA_HOME folder then startup/shutdown the script.

Based on above technique we can create many instance folder and change conf/server.xml file port values and run that instance with own newly created script files.

Update 1:
     Java Experience created a small utility for automatically generate the multiple tomcat instances in Windows/Linux Systems. Check Here

see the latest post :
                              Understanding Virtual Host in Tomcat

see the screen cast for running mutiple instance of tomcat



comments are welcomed

Tuesday, 5 July 2011

Understanding Java Server Faces 2.0 Part 3

This is Third  part of JSF 2.0 discussion. Please visit part one  and part two

Update : New Post JasperReports in JSF

In this post we will discuss JSF with AJAX. In JSF 2.0 gives full support for ajax integration in web application. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page. so with help of ajax we will make more interactive page.   JSF 2.0 Faclets provide <f:ajax> tag for achieve ajax in web application without writing client side java script ajax code. JSF 2.0 takes care all of this.


JSF and ajax integration is one of the greatest feature. and no other framework provide this much easy ajax integration in web application.


Consider we have one simple web application it prompt the name and click submit button its display the name in same page in some where .


we will see this example without ajax 


index.xhtml


<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
      <center>
        <h1>JSF 2.0 Web Application</h1><br/>

        <h:form>
          Enter Your Name 
          <h:inputText value="#{demoBean.userName}"/>
          <h:commandButton value="Submit"/><br/>

          <h2>
             <h:outputLabel 
                    value="#{demoBean.sayHello()}"/>
          </h2>
       </h:form>
     </center>
    </h:body>
</html>


DemoBean.java




@ManagedBean
@SessionScoped
public class DemoBean {

    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String sayHello() {
        return "Hello, Mr." + userName;
    }
}

here when we click the submit button then entire page is refreshed, ie. complete page is downloaded from server and displayed in browser.

Integrate Ajax in JSF
     JSF 2.0 provides full ajax support. so we no need to write client side java script for ajax. JSF 2.0 container generate client side java script, if we use <f:ajax> tag.



If we use <f:ajax> tag then JSF 2.0 container attached one java script file in generated response code.
that java script handles all ajax related events/actions.

now we change above example, add ajax in jsf
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
  <h:head>
        <title>Facelet Title</title>
  </h:head>
  <h:body>
   <center>
     <h1>JSF 2.0 Web Application</h1><br/><br/><br/>

      <h:form>
         Enter Your Name 
         <h:inputText id="user_name" value="#{demoBean.userName}"/>
         <h:commandButton value="Submit">
            <f:ajax execute="user_name" render="display"/>
         </h:commandButton><br/>

         <h2>  
           <h:outputLabel id="display" value="#{demoBean.sayHello()}"/>
         </h2>
       </h:form>
     </center>
    </h:body>
</html>

here each component need id attribute. to distinguish between various components in page. <f:ajax> tags have some attributes. we nested inside the command button. so ajax event occur when command button clicked. Browser send ajax request to server. JSF 2.0 container proceeds the request and send partial response back to browser. JSF supplied java script takes care to parse the partial response and applied to DOM tree.

this is partial response returned by container


Hello, Mr.541-4160405675642876717:7702620445641245519

we will see <f:ajax> tag attributes

execute - while send the ajax request what are the parameters(or form fields) attach with request. here some predefined values are
              @all - all components value in the page are submitted,
              @this- only one component where we attach ajax tag that component value is submitted,
              @form -  current form (where ajax tag attached) fields are submitted

render - what are components we need to refresh (or update). here i mentioned 'display'. so only update the component whose id is display.

event - when ajax request should happen. default value is click, when we attached with command Button. or default value is change ,when attache with input text. here possible values are blur, change, valueChange, click, dblclick, focus, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, mouseup, select.


now we modify the index.xhtml to call ajax event for every key up event.

     

comments are welcomed





Related Posts Plugin for WordPress, Blogger...