Jboss EJB 3.0
Stateless Beans
Calculator.Java // 計算器的接口:加、減
package org.jboss.tutorial.stateless.bean;
public interface Calculator
{
int add(int x, int y);
int suBTract(int x, int y);
}
CalculatorBean.Java
package org.jboss.tutorial.stateless.bean;
import Javax.ejb.Stateless;
@Stateless //標記CalculatorBean 為 stateless bean,為本地和遠程接口的實現
public class CalculatorBean implements CalculatorRemote, CalculatorLocal
{
public int add(int x, int y)
{
return x + y;
}
public int suBTract(int x, int y)
{
return x - y;
}
}
CalculatorLocal.Java
package org.jboss.tutorial.stateless.bean;
import Javax.ejb.Local;
@Local //標記為本地接口,繼承了計算器接口
public interface CalculatorLocal extends Calculator
{
}
CalculatorRemote.Java
package org.jboss.tutorial.stateless.bean;
import Javax.ejb.Remote;
@Remote //標記為遠程接口,繼承了計算器接口
public interface CalculatorRemote extends Calculator
{
}
客戶端
ClIEnt.Java
package org.jboss.tutorial.stateless.clIEnt;
import org.jboss.tutorial.stateless.bean.Calculator;
import org.jboss.tutorial.stateless.bean.CalculatorRemote;
import Javax.naming.InitialContext;
public class ClIEnt
{
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
//查找遠程接口
Calculator calculator = (Calculator) ctx.lookup(CalculatorRemote.class.getName());
System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.suBTract(1, 1));
}
}
*****************************簡化版********************************
只需三個程序即可實現StatelessBean功能。
Calculator.Java
package org.jboss.tutorial.stateless.bean;
import Javax.ejb.Remote;
@Remote //直接將計算器標記為遠程接口
public interface Calculator
{
int add(int x, int y);
int suBTract(int x, int y);
}
CalculatorBean.Java
package org.jboss.tutorial.stateless.bean;
import Javax.ejb.Stateless;
@Stateless
public class CalculatorBean implements Calculator //這裡實現了計算器接口
{
public int add(int x, int y)
{
return x + y;
}
public int suBTract(int x, int y)
{
return x - y;
}
}
客戶端:
ClIEnt.Java
package org.jboss.tutorial.stateless.clIEnt;
import org.jboss.tutorial.stateless.bean.Calculator;
import Javax.naming.InitialContext;
public class ClIEnt
{
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
Calculator calculator = (Calculator) ctx.lookup(Calculator.class.getName());
//只要查找標記為remote的接口就行了
System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.suBTract(1, 1));
}
}
這裡附上log4j.properties 在jboss-EJB-3.0_PrevIEw_5.zip 裡面沒有這個老是顯示缺少appender
log4j.propertIEs
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=record.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %d{hh:mm:ss} %t %c{1} -%m%n
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.MaxFileSize=100KB
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.rootLogger=stdout,R
運行:參考installing.Html
Windows下
打開命令提示符cmd,到 jboss_home/bin
Run.bat –c all
用ant
先build後run 就行了。
討論:
簡化版是看到jboss-EJB-3.0_PrevIEw_5.zip裡面的statefull源碼而改寫的,在EJB spe 3.0說好像是刪除了home這個接口,我覺得簡化很多接口更方面,有效的開發J2EE application