Jboss EJB 3.0--Stateless Beans

來源:互聯網
上載者:User

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




相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。