做web程序这么久,一直把精力放在了前台的展现层,后台都没有怎么去做,除了前一段改进了一个系统的底层架构设计,优化了hibernate和log4j,更改了一些可能会导致内存泄露的地方。ejb更是一直没有用过ejb,最近看了ejb3.0介绍,感觉还不错,准备开始新一轮的修炼。
首先到网上下载了最新的jboss4.0.4,配置jbuilder2006的server,全部默认就可以了,具体就不说了。
先要创建一个EJBModule,然后用向导建一个session bean,名称为:HelloBean。
可以看到生成了3个文件:Hello.java,HelloBean.java,HelloHome.java
修改Hello接口为:
package net.fangw;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface Hello extends EJBObject {
String hello(String name) throws RemoteException,CreateException;
}
修改HelloBean类为:
package net.fangw;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
public class HelloBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
//业务方法
public String hello(String name) {
System.out.println("hello," + name + "!");
return "hello," + name + "!";
}
}
保存,编译,然后在EJBModule上点右键,把它deploy。
在工程里新建一个jboss的server,并运行,一般是不会有什么问题的,这样就发布了。
测试EJB:
创建EJB测试类HelloBeanTestClient1.java:
package net.fangw;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.lang.String;
import javax.naming.InitialContext;
import java.util.Hashtable;
import javax.ejb.*;
import java.rmi.*;
/**
* <p>Title: ejb test</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: fangw</p>
*
* @author fangw
* @version 1.0
*/
public class HelloBeanTestClient1 {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private Hello hello = null;
private HelloHome helloHome = null;
//Construct the EJB test client
public HelloBeanTestClient1() {
initialize();
}
public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("HelloBean");
//look up jndi name and cast to Home interface
helloHome = (HelloHome) PortableRemoteObject.narrow(ref, HelloHome.class);
if (logging) {
log(
"Succeeded initializing bean access through Home interface.");
long endTime = System.currentTimeMillis();
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
}
private Context getInitialContext() throws NamingException {
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
return new InitialContext(environment);
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public Hello create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
hello = helloHome.create();
if (logging) {
log("Succeeded: create()");
long endTime = System.currentTimeMillis();
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed : create()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from create(): " + hello + ".");
}
return hello;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public String hello(String name) {
String returnValue = "";
if (hello == null) {
System.out.println("Error in hello(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling hello(" + name + ")");
startTime = System.currentTimeMillis();
}
try {
returnValue = hello.hello(name);
if (logging) {
log("Succeeded: hello(" + name + ")");
long endTime = System.currentTimeMillis();
log("Execution time: " + (endTime - startTime) + " ms.");
}
} catch (Exception e) {
if (logging) {
log("Failed : hello(" + name + ")");
}
e.printStackTrace();
}
if (logging) {
log("Return value from hello(" + name + "): " + returnValue + ".");
}
return returnValue;
}
public void executeRemoteCallsWithDefaultArguments() {
if (hello == null) {
System.out.println(
"Error in executeRemoteCallsWithDefaultArguments(): " +
ERROR_NULL_REMOTE);
return;
}
try {
hello("");
} catch (Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
private void log(String message) {
if (message == null) {
System.out.println("-- null");
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " +
message.substring(0, MAX_OUTPUT_LINE_LENGTH) +
" ...");
} else {
System.out.println("-- " + message);
}
}
//Main method
public static void main(String[] args) {
HelloBeanTestClient1 client = new HelloBeanTestClient1();
try {
client.create();
client.hello("fangw");
client.hello.remove();
} catch (RemoveException ex) {
ex.printStackTrace();
} catch (RemoteException ex) {
ex.printStackTrace();
}
// Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers.
}
}
保存,编译,运行。
如果出现:
-- Initializing bean access.
log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
log4j:WARN Please initialize the log4j system properly.
-- Succeeded initializing bean access through Home interface.
-- Execution time: 2113 ms.
-- Calling create()
-- Succeeded: create()
-- Execution time: 261 ms.
-- Return value from create(): HelloBean:Stateless.
-- Calling hello(fangw)
-- Succeeded: hello(fangw)
-- Execution time: 10 ms.
-- Return value from hello(fangw): hello,fangw!.
那就ok了,可以睡觉了,今天就到这里。