2009年3月23日星期一

WLS_061:FastSwap特性

运行开发环境:WebLogic Server 12c开发版(12.1.1.1.0) + OEPE 12c(12.1.1.1.0)

FastSwap特性可以动态装载修改后的Java类而无需重新发布应用,显然这能够大大提升开发效率。

1. 创建EAR Application Project:FastSwapApp
(1)
 (2)

2. 创建Dynamic Web Project:FastSwapWeb
 (1)
 (2)选择Add Project to an EAR

(3)index.jsp代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="sessionbean.AccountBean" %>

<%
AccountBean bean;
if(session.getAttribute("bean") == null){
bean = new AccountBean();
session.setAttribute("bean",bean);
}else{
bean = (AccountBean)session.getAttribute("bean");
session.setAttribute("balance",bean.getBalance());
}
bean.deposit(100);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Current Balance: <%= bean.getBalance() %>
</body>
</html>

3. 创建EJB Project:EJBProject
 (1)选择Add Project to an EAR
 (2)选择Add Project to an EAR

(3)EJB代码
package sessionbean;

import static javax.ejb.TransactionAttributeType.*;

import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.Remote;
import javax.ejb.EJB;

import javax.annotation.PreDestroy;

import javax.interceptor.Interceptors;
import javax.interceptor.ExcludeClassInterceptors;
import javax.interceptor.InvocationContext;

/**
 * Bean file that implements the Account business interface. Uses the following
 * EJB annotations: - @Stateful: specifies that this is a stateful session EJB - @TransactionAttribute
 * - specifies that this EJB never runs in a transaction - @Remote - specifies
 * the Remote interface for this EJB - @EJB - specifies a dependency on the
 * ServiceBean stateless session ejb - @Interceptors - Specifies that the bean
 * file is associated with an Interceptor class; by default all business methods
 * invoke the method in the interceptor class annotated with @AroundInvoke. - @ExcludeClassInterceptors
 * - Specifies that the interceptor methods defined for the bean class should
 * NOT fire for the annotated method. - @PreDestroy - Specifies lifecycle method
 * that is invoked when the bean is about to be destoryed by EJB container.
 * 
 */

@Stateful
@TransactionAttribute(NEVER)
@Remote({ sessionbean.Account.class })
@Interceptors({ sessionbean.AuditInterceptor.class })
public class AccountBean implements Account {

private int balance = 0;

public void deposit(int amount) {
balance += amount;
System.out.println("deposited: " + amount + " balance: " + balance);
}

public void withdraw(int amount) {
balance -= amount;
System.out.println("withdrew: " + amount + " balance: " + balance);
}

public int getBalance() {
int bogus;
return balance;
}

@ExcludeClassInterceptors
public void sayHelloFromAccountBean() {

}

@PreDestroy
public void preDestroy() {
System.out.println("Invoking method: preDestroy()");
}

}


4. 修改FastSwapWeb Project的Deployment Assembly
(1)右键FastSwap项目,选择属性,选择Deployment Assembly
(2)点击Add,选择 Project
(3)选择EJB Project和EJBProjectClient
(4)添加完成后,EJBProject.jar和EJBClient.jar添加到WEB-INF/lib目录下。

 5. 右键index.jsp,选择Run As,Run on Server
(1)显示Balance值。
(2)刷新页面,会以100递增Balance值。
(3)修改AccountBean.java,修改方法getBalance:return balance + 1;
(4)刷新页面,发现仍以100递增Balance值,也就是没有使用修改后的Java类。

(5)选择Server Tab,Publish应用。
(6)刷新页面,这次发现修改起作用了。

(7)在FastSwapApp Prjoect中,找到META-INF\weblogic-application.xml,选中FashSwap,勾上Enable class redefinition,然后重新发布应用。
(8) 再次修改AccountBean.java,修改方法getBalance:return balance + 2; 。
(9)无需重新发布应用,直接刷新页面,发现使用了修改后的Java类,说明FashSwap起作用了。

Project 下载:FastSwap.7z

没有评论: