<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 閱讀(1805) 評(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成人无码电影| 久久精品国产免费一区| 黄瓜视频高清在线看免费下载| 精品久久久久久亚洲| 深夜a级毛片免费视频| 国产免费人人看大香伊| 美国免费高清一级毛片| 国产最新凸凹视频免费| 四虎永久在线精品免费一区二区| 男人的天堂亚洲一区二区三区 | 国产精品免费大片| 亚洲国产精品无码中文字| 任你躁在线精品免费| 亚洲av鲁丝一区二区三区| 在线观看免费av网站| 久久夜色精品国产噜噜亚洲a| 成年女性特黄午夜视频免费看 | 久久av免费天堂小草播放| 国产亚洲综合成人91精品| 久久aa毛片免费播放嗯啊| 亚洲欧洲精品一区二区三区| 亚洲成在人线aⅴ免费毛片| 亚洲日韩精品国产3区| 免费人成无码大片在线观看| 一级女性全黄久久生活片免费| 免费一级毛片不卡在线播放| 国产免费人成视频在线播放播| 亚洲av永久无码制服河南实里 | 亚洲视频在线免费观看| 亚洲成人福利网站| 免费人成在线观看视频播放| 久久99免费视频| 亚洲av无码一区二区三区天堂古代| 国产精品嫩草影院免费| 大妹子影视剧在线观看全集免费| 久久精品夜色国产亚洲av| 青青青免费国产在线视频小草| 国产精品亚洲一区二区三区久久 | 一级有奶水毛片免费看| 亚洲系列国产精品制服丝袜第| 成年女人免费视频播放体验区|