由於 Java™ APIs for XML based Remote Procedure Call (JAX-RPC) 規格的變更,除非修改基本 EJB 應用程式處理異常狀況的程式碼,否則,在 9.0 版 中,無法包裝 WebSphere® Application Server 5.1 版所能包裝的 Enterprise JavaBeans (EJB) 應用程式。
關於這項作業
JAX-RPC 1.1 版規格指出:
遠端方法中宣告的服務特定異常狀況必須是已檢查的異常狀況。
必須直接或間接延伸 java.lang.Exception,但不可以是 RuntimeException。
不可能再直接使用 java.lang.Exception 或 java.lang.Throwable 類型。
必須使用服務特定異常狀況來修改應用程式,才符合規格。
程序
- 修改使用服務特定異常狀況的應用程式。 例如,假設現有的 EJB 使用一個稱為 UserException 的服務特定異常狀況。UserException 內有一個欄位稱為 ex,是 java.lang.Exception 類型。
為了在 WebSphere Application Server 9.0 版中成功以 Web 服務包裝應用程式,您必須變更 UserException 類別。在這個範例中,您可以修改 UserException,將 ex 的類型從 java.lang.Exception 改成 java.lang.String。
新的 UserException 類別:
package irwwbase;
/**
* Insert the type's description here.
* Creation date: (9/25/00 2:25:18 PM)
* @author: Administrator
*/
public class UserException extends java.lang.Exception {
private java.lang.String _infostring = null;
private java.lang.String ex;
/**
* UserException constructor comment.
*/
public UserException() {
super();
}
/**
* UserException constructor comment.
*/
public UserException (String infostring)
{
_infostring = infostring;
} // ctor
/**
* Insert the method's description here.
* Creation date: (11/29/2001 9:25:50 AM)
* @param msg java.lang.String
* @param ex java.lang.Exception
*/
public UserException(String msg,String t) {
super(msg);
this.setEx(t);
}
/**
* @return
*/
public java.lang.String get_infostring() {
return _infostring;
}
/**
* @return
*/
public java.lang.String getEx() {
return ex;
}
/**
* @param string
*/
public void set_infostring(java.lang.String string) {
_infostring = string;
}
/**
* @param Exception
*/
public void setEx(java.lang.String exception) {
ex = exception;
}
public void printStackTrace(java.io.PrintWriter s) {
System.out.println("the exception is :"+ex);
}
}
- 在使用它的 Enterprise Bean 中修改整個異常狀況處理。 必須確保撰寫的 Enterprise Bean 接受新的異常狀況。在這個範例中,程式碼可能如下所示:
新的 EJB 異常狀況處理:
try {
if (isDistributed()) itemCMPEntity = itemCMPEntityHome.findByPrimaryKey(ckey);
else itemCMPEntityLocal = itemCMPEntityLocalHome.findByPrimaryKey(ckey);
} catch (Exception ex) {
System.out.println("%%%%% ERROR: getItemInstance - CMPjdbc " + _className);
ex.printStackTrace();
throw new UserException("error on itemCMPEntityHome.findByPrimaryKey(ckey)",ex.getMessage());
}