Wednesday, July 8, 2009

Date Parsing And Date Formatting In JSP Using JSTL

Many times we come across a situation where we want to create java.util.Date object in JSP with the String date. JSTL provides you parseDate tag for parsing the date string and creating date object.

First you have to add formatting tag lib into your project.

put <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/formatting"%> on the top of your JSP.

Say you have date string in a variable dateStr. Then to make date object out of it you have to write following code.

<fmt:parseDate var="dateObj" value="${dateStr}" type="DATE" pattern="yyyy-MM-dd"/>

This will parse the given date string with the given pattern and create java.util.Date object. you can access the date object with the variable name you have provided in var attribute.

If you want to format the date object then you can use formatDate tag.

<fmt:formatDate value="${dateObj}" pattern="MMM dd, yyyy"/>

note: For pattern string please check java website.

tag will print the formatted date on the page. So no need to put this into tag.

You can also use var attribute to store formatted string into variable.





Saturday, July 4, 2009

jboss clustering using apache web server and mod_jk

JBoss application server provides inbuilt support for clustering 2 or more JBoss servers. You just need to do few settings in JBoss server configuration XML files.

First of all you need to get JBoss application server downloaded from JBoss website. Then you need apache web server to work as a front end server for all clustered nodes. You also need mod_jk as a connector between apache web server and JBoss.

Assuming that there are 2 JBoss nodes that has to be clustered. Node1 server name is node1.server and node2 server is named as node2.server. Following is the detail:

Node 1:
Server Name: ServerNode1
IP/Host Name: Machine1

Node 2:
Server Name: ServerNode2
IP/Host Name: Machine2

Now to make these 2 JBoss server as clustered you have to edit "%JBOSS_HOME%/deploy/jboss-web.deployer/server.xml"

You have to add jvmRoute=”ServerNode1” attribute in Engine tag.

<Engine name=”jboss.web” defaultHost=”localhost” jvmRoute=”ServerNode1”>

You have to do same changes in all clustered JBoss servers.

So in our case second server config file will have jvmRoute = "ServerNode2" value.

After this make sure that mod_jk is set to true in jboss-service.xml.

<attribute name="UseJK">true<attribute>

That is all about setting both nodes in cluster. Now lets see how to use apache web server as front server for these clustered nodes. For apache web server let's assume following configuration:

Apache Web Server:
IP/Host Name: ServerNodeManager (Remember it will just behave like node manager It is not same like other node managers available in market. e.g. Weblogic Server Node Manager.)

Now just install mod_jk in apache web server.

Put mod_jk module in apache web server modules folder.

Now modify configuration file (httpd.conf) of apache web server.

# This will load mod_jk module in apache web server
LoadModule jk_module modules/mod_jk.so

# This is to tell all information about clustered nodes. This file is used by mod_jk.
JkWorkersFile conf/workers.properties

# To set the log file
JkLogFile logs/mod_jk.log

# To set log level
JkLogLevel debug

# Mounting workers file with mod_jk
JkMountFile conf/workers.properties

# To redirect all requested URL's to JBoss servers.
JkMount /* jboss

Once you are done with these settings, create a new file with name workers.properties. Enter following entries into it. Save this file in conf folder on apache web server.

worker.list=jboss, jkstatus

# First Node
worker.ServerNode1.type=ajp13
worker.ServerNode1.host=Machine1
worker.ServerNode11.port=8009
worker.ServerNode1.lbfactor=1
worker.ServerNode1.disabled=false
worker.ServerNode1.socket_timeout=10
worker.ServerNode1.connect_timeout=20000

# Second Node
worker.ServerNode2.type=ajp13
worker.ServerNode2.host=Machine2
worker.ServerNode2.port=8009
worker.ServerNode2.lbfactor=1
worker.ServerNode2.disabled=false
worker.ServerNode2.socket_timeout=10
worker.ServerNode2.connect_timeout=20000

# Load balancer
worker.jboss.type=lb
worker.jboss.balance_workers=ServerNode1,ServerNode2
worker.jboss.sticky_session=true
worker.jboss.sticky_session_force=false
#worker.jboss.method=R
#worker.jboss.lock=P

worker.jkstatus.type=status

Now restart apache web server and all jboss nodes.

Treat this web server as you single point of contact. Mod_jk will internally take care of load balancing you request to each node.