<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    A "Hello" program on EJB3 with Jboss server 4.2.2GA.

        Recently I did some research on EJB3 programming, find that it's really simple than programming with previous EJB version. Coding in EJB2.X or previous version is really a nightmare for most developers, maybe for this, EJB3 gives me much more impression than Spring when it first comes to me, this article is the first one of these series articles which record the new knowledge I find interesting in EJB3.

        Well, let's begin.

        EJB3 is simple with AOP and DI, there is no Home interface any more, only with Service Interface and your Service implementation you can create an typical EJB3 application. I will create a simple hello world program in this article, the service interface as follow:

     1 package com.ramon.expejb3.session;
     2 
     3 import javax.ejb.Remote;
     4 
     5 @Remote
     6 public interface Greeting {
     7     
     8     /**
     9      * say hello test
    10      * @param name
    11      */
    12     void greeting(String name);
    13 
    14 }

        It's really simple, especially for you that are familiar with programing with Interface, after that is the service implementation code:
     1 package com.ramon.expejb3.session.impl;
     2 
     3 import javax.annotation.PostConstruct;
     4 import javax.annotation.PreDestroy;
     5 import javax.ejb.Stateless;
     6 
     7 import com.ramon.expejb3.session.Greeting;
     8 
     9 @Stateless(name = "Greeting")
    10 public class GreetingBean implements Greeting {
    11     
    12     @PostConstruct
    13     public void init() {
    14         System.out.println("Init Greeting Bean.");
    15     }
    16     
    17     @PreDestroy
    18     public void destroy() {
    19         System.out.println("Garbage collect Greeting Bean.");
    20     }
    21 
    22     /* (non-Javadoc)
    23      * @see com.ramon.expejb3.session.Greeting#greeting(java.lang.String)
    24      */
    25     public void greeting(String name) {
    26         System.out.println("Hello " + name + ".");
    27     }
    28 }
    29 
        Still simple and very self-explanation, @Stateless(name = "Greeting") specify the JNDI name for client invocation. OK, that's all for our EJB jar file, no more file needed for this simple hello application. Execute your ANT script make a jar for it, part of ANT script may look like:
     1     <target name="prepareexpejb3" description="Create exp_jsf distribution.">
     2         <mkdir dir="${dist.dir}" />
     3         <mkdir dir="${build.dir}" />
     4         <mkdir dir="${build.core.dir}"/>
     5     </target>
     6 
     7     <!-- ================================= 
     8           target: Compile expejb3 classes      
     9          ================================= -->
    10     <target name="compileexpejb3" depends="cleanDist,prepareexpejb3">
    11         <javac destdir="${build.core.dir}" debug="yes" deprecation="on" srcdir="${src.dir}">
    12             <include name="${core.src.dir}/**" />
    13             <classpath refid="expejb3.classpath" />
    14         </javac>
    15         
    16     </target>
    17 
    18     <!-- ================================= 
    19           target: Create EJB3 jar
    20          ================================= -->
    21     <target name="createEJB3Jar" depends="compileexpejb3">
    22         <jar jarfile="${dist.dir}/${expejb3.core.name}.jar">
    23             <fileset dir="${build.core.dir}"></fileset>
    24         </jar>
    25     </target>

        Put the EJB jar into jboss server server\default\deploy, you should see some log from jboss console like:
       
    1 10:59:27,036 INFO  [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.stateless.St
    2 atelessContainer
    3 10:59:27,051 INFO  [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=expejb3core.jar,name=Gree
    4 ting,service=EJB3 with dependencies:
    5 10:59:27,208 INFO  [EJBContainer] STARTED EJB: com.ramon.expejb3.session.impl.GreetingBean ejbName:
    6 Greeting
        Which mean that you have successfully deploy your EJB into jboss server, create a client code to invoke your EJB service:
     1 package com.ramon.expejb3.session;
     2 
     3 import java.util.Properties;
     4 
     5 import javax.naming.Context;
     6 import javax.naming.InitialContext;
     7 
     8 import junit.framework.TestCase;
     9 
    10 public class ExpEJB3BaseTestCase extends TestCase {
    11 
    12     private Properties properties = null;
    13     
    14     private Context context;
    15 
    16     protected void setUp() throws Exception {
    17         super.setUp();
    18         properties = new Properties();
    19         properties.put("java.naming.factory.initial""org.jnp.interfaces.NamingContextFactory");
    20         properties.put("java.naming.factory.url.pkgs""org.jboss.naming:org.jnp.interfaces");
    21         properties.put("java.naming.provider.url""localhost:1099");
    22         context = new InitialContext(properties);
    23     }
    24 
    25     protected void tearDown() throws Exception {
    26         super.tearDown();
    27         context = null;
    28         properties = null;
    29     }
    30 
    31     public final Context getContext() {
    32         return context;
    33     }
    34 
    35     public final void setContext(Context context) {
    36         this.context = context;
    37     }
    38 
    39 }
    40 

     1 package com.ramon.expejb3.session.impl;
     2 
     3 import javax.naming.NamingException;
     4 
     5 import com.ramon.expejb3.session.ExpEJB3BaseTestCase;
     6 import com.ramon.expejb3.session.Greeting;
     7 
     8 public class GreetingBeanTest extends ExpEJB3BaseTestCase {
     9 
    10     public void testGreeting() {
    11         try {
    12             Greeting greetService = (Greeting)this.getContext().lookup("Greeting/remote");
    13             greetService.greeting("Ramon");
    14         } catch (NamingException e) {
    15             // TODO Auto-generated catch block
    16             e.printStackTrace();
    17         }
    18     }
    19 
    20 }
    21 
        That's really simple, right? What you should note is that you must include following jars into your classpath when you run this clien test case, otherwise you will get the annoying classNotFound Exception:
     1 concurrent.jar
     2 jboss-aop-jdk50.jar
     3 jboss-aspect-library-jdk50.jar
     4 jboss-common.jar
     5 jboss-ejb3.jar
     6 jboss-ejb3-client.jar
     7 jboss-remoting.jar
     8 jbossx-client.jar
     9 jboss-transaction-client.jar
    10 jnp-client.jar


     

       

    posted on 2008-03-21 16:02 Find it, try it, experience it 閱讀(1804) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2008年3月>
    2425262728291
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    導(dǎo)航

    統(tǒng)計(jì)

    公告

    If there is any question you have, please don't hesitate, let me know ASAP, you can find me at kenees@gmail.com or QQ: 9808873, hope to make friends with you ;)

    常用鏈接

    留言簿(1)

    隨筆檔案

    文章檔案

    搜索

    積分與排名

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 久久精品熟女亚洲av麻豆| 四虎免费在线观看| 一区二区免费国产在线观看| 久久久久亚洲AV无码观看 | 亚洲AV无码成人精品区蜜桃| 亚洲精品乱码久久久久久自慰| 成人免费在线观看网站| 免费国产a理论片| 久久久久亚洲精品美女| 一二三四免费观看在线电影| 人人爽人人爽人人片av免费 | 亚洲免费人成视频观看| 永久免费av无码入口国语片| 人妻仑乱A级毛片免费看| 成人亚洲国产精品久久| 亚洲成人激情小说| 亚洲精品线路一在线观看| 啦啦啦www免费视频| 一二三四免费观看在线电影| 国产曰批免费视频播放免费s| 一级毛片a女人刺激视频免费 | 国产青草视频在线观看免费影院| 亚洲免费视频网址| 2015日韩永久免费视频播放 | 国产美女被遭强高潮免费网站| 男人的好免费观看在线视频| 蜜臀AV免费一区二区三区| 久久久久久曰本AV免费免费| 日本xxxx色视频在线观看免费| 四虎影视成人永久免费观看视频 | 久久久久久国产a免费观看黄色大片 | 亚洲免费视频观看| 国产成人精品免费视| 1000部禁片黄的免费看| 最刺激黄a大片免费网站| 91人成网站色www免费下载| 国产免费一区二区三区在线观看| 97无码人妻福利免费公开在线视频| 99视频免费在线观看| 精品亚洲福利一区二区| 无人视频在线观看免费播放影院|