Thursday, March 08, 2007

Load a resource file in ear environment

Three issues
1. the properties files must be in the class path of the application module trying to read it
2. you have to use the correct class loader (Thread.currentThread().getContextClassLoader()).
3. call classloader's getResource(String propResource) method.

It will not work by simply using ClassLoader.getSystemResource() method.

Thursday, March 01, 2007

two tips

Regular Expressions:
using $1, $2.. to match find result. use () to group characters. * + are applied after the searching characters. using \w for all alphanumeric char.

How to throw business exception in web service calls.
The best way is to define some serializable fields when creating the exception class which will be returned by a web service method call. Otherwise when calling wsdl2java, it will automatically create a message field and getMessage() for you, which will cause problem in RAD tool. The reason for all this I guess is because the Exception class itself is not Serializable, and its fields can not be transfered. So it requires developer to create a subclass of Exception with its own fields to hold the exception code and message. Which can be shown within the fault segment.

Tuesday, February 20, 2007

jsf newbie problem

javax.servlet.jsp.JspException: Cannot find FacesContext is often the error you got when you run a jsf enabled page for the first time. For example if you invoke a page using http://localhost:8080/yourJSFpage.jsp

Actually you should use http://localhost:8080/faces/yourJSFpage.jsp when calling the page.This is required because FacesContext is initialized when the JSF servlet controller is invoked. Or you can do http://localhost:8080/yourJSFpage.faces .

Websphere server v6 + Websphere MQ v6 setup

I spent 3 whole working days trying to setup Websphere MQ (v6) and Websphere Server (v6). Finally got the sender (session bean) and the receiver (MDB) working.
Here are the things I learned.
1. MQ needs upgrade to v6.0.2 to be connectable from WAS. (must do for donwload trial version)
2. Need to specify the MQ_ROOT_DIR in WAS to synchronize the client jars.
3. Some previous failure deployment file can be stuck in the "profiles/default" temp and wstemp directories. Need to remove them to have a clean startup on WAS.
4. If you want to use websphere default JMS queue (not MQ) , a bus need to be setup.
See 5. If you want to use websphere MQ on the same server as WAS, a good tutorial (but for MQ 5.3) is
The code might need change though in the finally block of sender. (Don't close a resource if it is null, thus avoid noise exceptions)

Wednesday, January 31, 2007

Java Serialization Review

Java Serialization is always a tricky place and a hot spot for java job interview. Two important things:
1. If the Super class is not serializable and the sub class is. Then one important requirement is that the Superclass must have a non-arg constructor accessible by the sub class. Otherwise run time exception. Also all the fields in the super class will not be restored through serialization. ( The Default value or values set in the super class constructor will be used for these fields)

2. If the serializable contains a member variable which is not Serializable. Runtime exception will throw. The easy workaround is to mark the non-serializable object as a static memeber. Static member will not be serialized and default value is used. This also reflect why we should use POJO as much as possible and avoid complex non-serializable objects. Normally these objects are Unit object and should not be included in a transfer object at all.

Websphere session bean remote invokation tips

Here is how to access remote session bean in Websphere 6.0 server.

For remote session bean lookup, use the following code:

public class RemoteTest {
public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "");
prop.put(Context.PROVIDER_URL, "corbaloc:iiop:localhost:2809");
Context initContext = new InitialContext(prop);
EchoHome echoHome = (EchoHome) PortableRemoteObject.narrow(
initContext.lookup("ejb/com/yuan/EchoHome"), EchoHome.class);
Echo echo = (Echo) PortableRemoteObject.narrow(
echoHome.create(), Echo.class);
System.out.println("AAA" + echo.print());

} catch (Exception e) {

Two Technical points:

1. All client side library must be in the classpath to avoid exception.
The easy way is to include all jars under the server/lib dir.

2. The lookup address should be the raw name of the session bean (including full class name).
It is different from the lookup in the container environment (java:comp/env/ejb/Echo).