增加log4j...
首先将Hello.java改成:
package com.rox;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import org.apache.log4j.Logger;
/**
* XDoclet-based session bean. The class must be declared
* public according to the EJB specification.
*
* To generate the EJB related files to this EJB:
* - Add Standard EJB module to XDoclet project properties
* - Customize XDoclet configuration for your appserver
* - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="Hello"
* display-name="Name for Hello"
* description="Description for Hello"
* jndi-name="ejb/Hello"
* type="Stateless"
* view-type="remote"
*/
public class Hello implements SessionBean {
* XDoclet-based session bean. The class must be declared
* public according to the EJB specification.
*
* To generate the EJB related files to this EJB:
* - Add Standard EJB module to XDoclet project properties
* - Customize XDoclet configuration for your appserver
* - Run XDoclet
*
* Below are the xdoclet-related tags needed for this EJB.
*
* @ejb.bean name="Hello"
* display-name="Name for Hello"
* description="Description for Hello"
* jndi-name="ejb/Hello"
* type="Stateless"
* view-type="remote"
*/
public class Hello implements SessionBean {
/** The session context */
private SessionContext context;
private final Logger log = Logger.getLogger(Hello.class);
public Hello() {
super();
// TODO Auto-generated constructor stub
}
private SessionContext context;
private final Logger log = Logger.getLogger(Hello.class);
public Hello() {
super();
// TODO Auto-generated constructor stub
}
/**
* Set the associated session context. The container calls this method
* after the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
* Set the associated session context. The container calls this method
* after the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
log.info("setSessionContext");
}
public void ejbCreate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
log.info("ejb create");
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
log.info("ejb remove");
}
}
public void ejbCreate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
log.info("ejb create");
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
log.info("ejb remove");
}
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
log.info("ejb activate");
}
// TODO Auto-generated method stub
log.info("ejb activate");
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
log.info("ejb passivate");
}
// TODO Auto-generated method stub
log.info("ejb passivate");
}
/**
* An example business method
*
* @ejb.interface-method view-type = "remote"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public String sayIt() throws EJBException {
log.info("hello didi");
return "hello didi.";
}
* An example business method
*
* @ejb.interface-method view-type = "remote"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public String sayIt() throws EJBException {
log.info("hello didi");
return "hello didi.";
}
}
上面org.apache.log4j.Logger类继承自org.apache.log4j.Category类,log4j有三个重要的组件:categories, appenders和layouts.categories有各种方法来发送日志信息,可以将日志信息发送到控制台,文件,socket....,而这些终端就被称为appenders,可以理解为往内容中"append"的意思,layouts就是日志信息的格式,例如HTMLLayout就是html的格式.
log4j输出日志是分级别(priority)的:
debug,info,warn,error,fatal.这五个级别的等级由低到高,即如果配置文件的日志级别设置为debug时,所有log信息都会输出;而如果设置为fatal时只有fatal信息会输出,并且程序会终止运行.
当运行EjbTest类时,会在控制台打印出:
11:43:00,587 INFO [Hello] setSessionContext
11:43:00,587 INFO [Hello] ejb create
11:43:00,603 INFO [Hello] hello didi
1.从输出信息也可以看出无状态bean的回调方法(容器调用的方法)中,ejbActivate,ejbPassivate是不会被调用的.
2.如果再次调用EjbTest,控制台中只会输出
11:43:00,603 INFO [Hello] hello didi
这说明jboss直接从缓存中取出已经实例化的ejb对象,而不是重新实例化.
3.如果将Hello.jar从deploy中移出,控制台将会输出ejb remove的信息.
如果要更改日志的输出级别,请更改jboss中deploy同级的conf目录中的log4j.xml.找到如下语句:
INFO
"/>
%d{ABSOLUTE}
%-5p [%c{1}] %m%n"/>
将红色的INFO替换成其他的级别即可,jboss过几秒钟就会自动侦测到log4j.xml的变化,自动进行配置.
如果要改变日志输出的样式,可以改变以上内容中的兰色部分,假如去掉%-5p,你将会发现日志输出中将没有日志级别的信息.
好了,到此日志输出部分划上句号,下一步将研究配合entiyBean使用.