Thursday, October 28, 2010

How to put limitation of time on a method call

Lets say, you are reading a website, and you want that, if you get a response within 3 seconds then its ok..
otherwise, you want to give a message to user that internet is tooo slow...how will you do it...

I always wondered it...today found a solution...Not tooo complex, its just that, i was not able to develop this logic using my own knowledge of java. :)

This is just an example:
Declare a private Thread mainThread;

final int factorialOf = 1 + (int) (30000 * Math.random());
System.out.println("computing " + factorialOf + '!');

Thread testThread = new Thread() {

    public void run() {
        System.out.println(factorialOf + "! = " + Utils.computeFactorial(factorialOf));
        //Now run method has completed its execution
        // so it should interrupt the main Thread
        mainThread.interrupt();
    }
};
mainThread=Thread.currentThread();
testThread.start();
try
{
    Thread.sleep(1000);
}
catch(InterruptedException e)
{
    // If we reach here, that means sleep was interrrupted before 1000 ms.
}
testThread.interrupt();

if (testThread.isInterrupted()) {
    throw new TimeoutException("the test took too long to complete");
}

PS: Though this should work, but some time is being taken by the statements which create new thread.


Some other/better methods are also given here : http://javakafunda.blogspot.in/2012/05/how-to-put-time-restriction-on-method.html

Sunday, October 24, 2010

Add zooming effect to images on your website

You must have wondered, how to add zooming effect to an image.
Its pretty simple to do....

A simple attribute of <img> has the capbility of doing it

<img src="http://www.celebrity-pictures.ca/Celebrities/Katrina-Kaif/Katrina-Kaif-i105058.jpg" 

onmousedown="this.height=450;this.width=600" 

onmouseup="this.height=150;this.width=200" width=200 height=150>

Output :





Here are few of the event handlers :

Attribute Description
onabort Script to be run when loading of an image is interrupted
onclick Script to be run on a mouse click
ondblclick Script to be run on a mouse double-click
onmousedown Script to be run when mouse button is pressed
onmousemove Script to be run when mouse pointer moves
onmouseout Script to be run when mouse pointer moves out of an element
onmouseover Script to be run when mouse pointer moves over an
element
onmouseup Script to be run when mouse button is released
onkeydown Script to be run when a key is pressed
onkeypress Script to be run when a key is pressed and released
onkeyup Script to be run when a key is released

If you want a effect that gradually grows the image, you can refer to the link : http://javascript.internet.com/image-effects/growing-image.html

Wednesday, October 13, 2010

Rules about classpath

Source : http://mindprod.com/jgloss/classpath.html

Rules About Classpaths

  1. Simply copying or moving your jar files to the ext directory pointed to by the system property java.ext.dirs =C:\Program Files\java\jre6\lib\ext automatically puts them on the classpath without having to mention them explicitly. This is a great way to prune back an overblown classpath. It is safest to put your jars in all the ext directories:
    where to look for ext directories :
    You never know for sure where your javac.exe or java.exe is going to look. Grrr. You can change the location of the
    java.exe -Djava.ext.dirs=C:\mylibs mypackage.MyClass
    See the ext dirs entry for details.

Classpath Gotchas

Source :  http://mindprod.com/jgloss/classpath.html


  • If you use the -jar option, java.exe ignores the classpath. It will only look in that jar!! Try using the manifest Class-Path instead to get Java to look in auxiliary jars.
  • The elements of the classpath are platform specific OS directory names (with \ or / separators) or jar file names. The package/classnames on the command line are not; they are fully-qualified dotted names. Don’t try to concoct a chimera class name such as C:/com.mindprod.mypackage.MyClass
  • If you want a jar on the classpath, you must put the jar file name on the classpath, not just the name of the directory where the jar lives. This means you need whacking long classpaths, with every jar in a common directory each added to the classpath individually. Perhaps one day Sun will implement a JARPATH. In would not be hard to build a wrapper around javac.exe and Java.exe that faked in a JARPATH. In the meantime you

    rem how to fake jarpath
    rem separate jarnames with File.pathSeparatorChar, i.e. ; in Windows : in Unix
    java.exe -Djava.ext.dirs=usualextdir;anotherdir ...
    To add to the usual extdir. In Java version 1.6 you can use a wildcard to include all jars in a directory.
    rem use of wildcard in classpath to add all jars in C:\jardir and E:\morejars
    java.exe -classpath C:\jardir\*;E:\morejars\*
  • Beware of inserting extra semicolons (in general, FilepathSeparatorChar chars) in your CLASSPATH. Everything to the right will be ignored!, leading you to pull out your remaining hair in frustration wondering why java.exe can’t find the classes. You must have exactly one semicolon between elements, no lead/trail/extras.
  • Beware. Putting D: on the classpath is not the same thing as putting D:\. The first put the current directory of D: on the classpath. The second puts the root directory. They are often the same, but not always

Friday, October 8, 2010

Clustering J2EE Applications

All mission critical applications need to be have high Availability and Scalability features built-in. Any incident or outage in such applications can have huge implications – like business loss or legal issues faced by the company. Clustering helps in application scalability (through load balancing) as well as high availability (through failover).


Clustering J2EE Applications
Never assume that stand-alone applications can be transmit transparently to a cluster structure.
Most of the leading application server vendors like IBM (Websphere) and Oracle (BEA Weblogic) support clustering their servers and provide for built-in load balancing among them.



Tuesday, October 5, 2010

Generate QJML from Java Bean class...

Quick tool by IBM is a very useful tool for creating parsing logic for conversion from XML to java.

Have a look at the following link...

http://www.ibm.com/developerworks/library/x-quick/index.html

But this converts from DTD -> QJML -> Java.

I have eliminiated the need for creating DTD.
You can create QJML directly from java Bean class if you have.
Just provide the java class and it will generate 90% code for QJML.
You can use it with minor tweeking...