Wednesday, August 13, 2008

Symbolic Links

There are two concepts of `link' in Unix, usually called hard link and
soft link.

A soft link (or symbolic link, or symlink) :it is a small special file that contains a pathname. Thus,
soft links can point at files on different filesystems

Command

To create Soft link
ln -s [target] [linkname]

Delete links
rm [link name]

Display Links
ls -l

A hard link is just a name for a file. (And a file can have several names). It is deleted from disk only when the last name is removed. Hard link can be created without the -s option.

The major use of links would be to avoid redundancy and it saves a lot of maintenance headaches.A typical case would be to avoid redudant image copy into multiple locations . Instead create symbolic links. Changing one image serves the need , if not links can be overridden with location specific image.

Monday, August 11, 2008

Direct Web Remoting (DWR)

Direct Web Remoting (DWR) exports java code to the browser. It consists of:
A java servlet running on the server that processes requests and sends responses back to the browser

Javascript running in the browser sends requests and can dynamically update the webpage

Methods for installtion

1 .Download the DWR jar
http://directwebremoting.org/dwr/download

2. Addition into web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

3.Add dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
</create>
<create creator="new" javascript="Demo">
<param name="class" value="your.java.Bean"/>
</create>
</allow>
</dwr>


The DWR config file defines what classes DWR can create and remote for use by Javascript
http://localhost:8080/[YOUR-WEBAPP]/dwr/ will show all accesible java methods

Include links to the javascript files that make the magic happen:
<script src='/[YOUR-WEBAPP]/dwr/interface/[YOUR-SCRIPT].js'></script>
<script src='/[YOUR-WEBAPP]/dwr/engine.js'></script>

DWR generates Javascript code that is similar to the Java code that exported using dwr.xml

Example

public class Remote {
public String getData(int index) { ... }
}

<script type="text/javascript"
src="[WEBAPP]/dwr/interface/Remote.js"> </script>
<script type="text/javascript"
src="[WEBAPP]/dwr/engine.js"> </script>

Remote.getData(42, function(str) { alert(str); });

It also helps in calling Meta-Data Objects ,handling Timeouts and Handling Errors and creating Creating Javascript objects to match Java objects.
Hence the java objects can be mapped to javascript and exchanged vise versa.

For more info refer : directwebremoting.org/ -

Wednesday, August 6, 2008

Web Performance Techniques

1. Zipping the static content(images,css,javascript) and page response.

This can be achived by using the following configuration on the apache vhost configuration

For static content

<FilesMatch "\.(html|htm|js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>

For page response ending with .do requests

<LocationMatch "\.(do)$">
SetOutputFilter DEFLATE
</LocationMatch>


2. Adding expires header

<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Expires "Wed, 31 Dec 2008 23:59:59 GMT"
</FilesMatch>

Expires also can be set as access plus x days

By adding this all images with the above extensions will not be requested.

If the above is missing all requests are made and images are downloaded for every requests.

if the browser cache is enabled , still Get requests are made and if the response is 304 meaning file has not changed the image will not be downloaded else
will download the image.

3. KeepAlive On

KeepAliveTimeout 15

MaxKeepAliveRequests 100

By setting this on httpd.conf the connection once obtained will not be disconnected until 15 seconds. Hence the multiple requests on a page will be faster.
The disadvantage could be , if the requests are very high the latter once will not get a connection until these are closed.


The cache setting will override the expires header setting, if it is less than the time interval for expires header.

Must use Addons With FireFox

FireBug - Shows request basis data along with headers and allows easy debugging of html content.

Web developer - Comes with lot of web development features like enable/disable cache/image/css/form data/proxy etc.

Live HTTP headers - Gives all trhe details required with headers.

Tamper Data -gives header data and also allows to tamper the data.

Tuesday, August 5, 2008

Replacing RMI with Spring Remoting

Custom classes used

com.px.site.db.IExampleService - Service Interface

com.px.site.db.ExampleService - - Service Implementation

EXAMPLE_SERVICE_URL - urel (Ex :htpp://10.x.x.x/context/ExampleDetails

context - is the context name under jetty/tomcat where the remote service is running

All other classes are from the spring jar

Service Code(Server)

The bean is instantiated in the service side using the following entry in the spring xml

<bean id="exampleProxy" class="com.px.site.db.ExampleService"/>

The implementation is provided to the main applicatuion calling the following url

<bean name="/ExampleDetails" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="exampleProxy"/>
<property name="serviceInterface" value="com.px.site.db.IExampleService"/>
</bean>

The the application can get a reference using the url --> htpp://10.x.x.x/context/ExampleDetails


Main Application Code(client)

The service reference is obtained in the main application by adding the following in the spring xml where
EXAMPLE_SERVICE_URL is htpp://10.x.x.x/context/ExampleDetails obtained from any of the config parameters


<bean id="exampleService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="${EXAMPLE_SERVICE_URL}"/>
<property name="serviceInterface" value="com.px.site.db.IExampleService"/>
</bean>


Now this service if needed can be used directly in any action or injected into any main service.

<bean id="application" class="com.examples.main.application">
<property name="exampleService" ref="exampleService"/>
</bean>

Them main advantage of this is a application can speak to any service which is remotely located.

Monday, August 4, 2008

Spring timer tasks for Threads

Create the bean which should be called on regular intervals.

This bean should extend the TimerTask and should have the run method which will be called on timely basis.

<bean id="example" class="com.examples.foo">

Make the bean a task and provide the time interval when it needs to be called. The time could be picked from property files.

<bean id="exampleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="period" value="${exampleTime}">
<property name="timerTask" ref="example">
</property>

Add the tasks as part of Timerfactory which will be called based on the time given.

<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="exampleTask">
</ref>
</list>
</property>

All the above entries will be part of the spring xml.More then one tasks can be added to the timerfactory
</bean></property></bean></bean>

Prototip for easy and Fancy tool Tips

Just came across Prototip which helps to create javascript based tool tips with ease

http://www.nickstakenburg.com/projects/prototip2/.

Could not work more on it as we needed something which supports from IE 5+ versions. Protip supports IE 6+ and Firefox 2+

Running Parallel Db queries on huge Databases.

When the size of the table is very high and the column you are looking for is not indexed then the best way to run the query would be by parallel execution.

This really speeds up the process . But the catch here is that the CPU utilization is high and in production systems this may end up in locks.

This technique can be safely used in backup db servers.

Example

SELECT /*+ parallel(huge_table,4 ) */ * FROM huge_table;

where 4 is the degree of parallelism.The degree of parallelism depends on the capacity of the machines.

Friday, August 1, 2008

Loading multiple property files using Spring

While loading multiple files using spring the following property needs to be added
into bean of the first xml which gets loaded.


< name="ignoreUnresolvablePlaceholders" value="true"/>

Else the subsequent property files will not be loaded.

Some useful unix commands i came across today

vmstat [pamam1] [param2]

shows the server statics along with swap space, cache,io and cpu utilization.

this stats are shown [param2] number of times with [param1] duration in between.

top

this command also shows the server statistics in detail with
contribution from each process.
This also shows the memory used,cpu utilization and command used for each thread.

finger

Shows the user who are currently logged into the node.

Simplest way to Configure Logging using Log4j

Ensure the log4j.jar & log4j.properties are in the classpath

Add the following into the required classes where logging is needed , so that log shows file name for each logger statement .

private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger([classname].class);

folowed by logger.[info,debug,error,fatal etc]("logger message");

Contents in Log 4j.properties

log4j.rootLogger=ON, ROOT[enabling the root logger]
log4j.appender.ROOT=org.apache.log4j.DailyRollingFileAppender[saving logs on the daily basis with date appended]
log4j.appender.ROOT.File=/a/b/c.log[root log file path]
log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout
log4j.appender.ROOT.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p [%t] %c{2} - %m%n [pattern for display in each line]

#FILE APPENDER [another logger file ]
log4j.appender.F2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.F2.File=/a/b/d.log
log4j.appender.F2.layout=org.apache.log4j.PatternLayout
log4j.appender.F2.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss } %-5p [%t] %c{2} - %m%n


log4j.logger.org.apache=ERROR[apache specific errors to be shown on both logger files]
log4j.logger.com.example=DEBUG,F2[for com.example modules debug status messages to be shown in second logger file]

For Log4j debugging
Append -Dlog4j.debug in JAVA_OPTS in catalina.sh which will enable detailed logs in catalina.out showing the logger configerations.

 
Free Domain Names @ .co.nr!