因为 Java™ API for XML based Remote Procedure Call (JAX-RPC) 规范发生更改,所以可封装在 WebSphere® Application Server V5.1 中的 Enterprise JavaBeans (EJB) 应用程序不能封装在 V9.0 中,除非您修改基本 EJB 应用程序的异常处理的代码。
关于此任务
JAX-RPC V1.1 规范陈述:
在远程方法特征符中声明的特定于服务的异常必须是已校验的异常。它必须直接或间接扩展 java.lang.Exception,但不得是 RuntimeException。
不再能够直接使用 java.lang.Exception 或 java.lang.Throwable 类型。必须修改使用特定于服务的异常的应用程序,以符合规范。
过程
- 修改使用特定于服务的异常的应用程序。 例如,如果现有 EJB 使用称为 UserException 的特定于服务的异常。在 UserException 中是一个类型为 java.lang.Exception 的字段 ex。要成功地将具有 Web Service 的应用程序封装在 WebSphere Application Server V9.0 中,您必须更改 UserException 类。在此示例中,可以修改 UserException 以使 ex 的类型为 java.lang.String 而不是 java.lang.Exception。
新建 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);
}
}
- 修改使用异常处理的企业 Bean 中的所有异常处理。 必须确保已对企业 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());
}