ejb3+weblogic 10的一些小結

來源:互聯網
上載者:User

  一個helloworld的例子,以及講明如何用ant去打包以及其指令碼.

首先是程式

package com.liao;

public interface HelloWorld {
    public String SayHello(String name);
}

package liao;

public interface HelloWorldLocal extends HelloWorld{

}

 

 

package com.liao.impl;

import com.liao.HelloWorld;
import com.liao.HelloWorldLocal;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(mappedName="HelloWorldBean")
@Remote (HelloWorld.class)
@Local (HelloWorldLocal.class)
public class HelloWorldBean implements HelloWorld, HelloWorldLocal {

    public String SayHello(String name) {       
        return name +"說:你好!?";
    }

}

web部分的測試jsp:
 <%@ page contentType="text/html; charset=GBK"%>
<%@ page import="com.foshanshop.ejb3.*, javax.naming.*"%>
<%
 try {  
        InitialContext ctx = new InitialContext();
      HelloWorldLocal local = (HelloWorldLocal) ctx.lookup("java:comp/env/ejb/HelloWorldLocal");
      out.println(local.SayHello("test"));
 } catch (Exception e) {
  out.println(e.getMessage());
 }
%>

web.xml:
  <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   <description>foshanshop ejb3.0 Sample</description>
  
  <ejb-local-ref>
    <ejb-ref-name>ejb/HelloWorldLocal</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>com.foshanshop.ejb3.HelloWorldLocal</local>
 <!-- 如果一個EAR檔案中存在多個EJB jar,而這些jar分別含有相同名稱的EJB時,
     應在EJB名稱前加上jar檔案名稱和#號,如:HelloWorld.jar#HelloWorldBean -->
    <ejb-link>HelloWorldBean</ejb-link>
  </ejb-local-ref>
    <welcome-file-list>
      <welcome-file>Test.jsp</welcome-file>     
  </welcome-file-list>
</web-app>

 當ejb模組和web一起打包成.ear時,要在meta-inf下放application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
             version="5">
 <display-name>EJB3 Sample Application</display-name>
 <module>
  <web>
   <web-uri>HelloWorld.war</web-uri>
   <context-root>HelloWorld</context-root>
  </web>
 </module> 
 <module>
     <ejb>HelloWorld.jar</ejb>
 </module> 
</application>

ant的設定檔:

  

<?xml version="1.0"?>
<!-- ======================================================================= -->
<!-- EJB3 WeblogicHelloWorld build file                                      -->
<!-- ======================================================================= -->
<project name="WeblogicHelloWorld" default="ear" basedir="..">
 <property name="app.dir" value="${basedir}\WeblogicHelloWorld" />
 <property name="src.dir" value="${app.dir}\src" />
 <property name="weblogic.home" value="C:\bea\wlserver_10.3"  description="weblogic產品所在目錄,本書安裝在C:\bea\wlserver_10.3"/>
 <property name="wls.username" value="weblogic" description="登入使用者名稱,預設為weblogic"/>
 <property name="wls.password" value="weblogic" description="登入密碼,預設為weblogic"/>
 <property name="wls.hostname" value="localhost" description="主機名稱"/>
 <property name="wls.port" value="7001" description="所在連接埠,預設為7001"/>
 <property name="wls.server.name" value="examplesServer" description="weblogic中的目標伺服器,預設為examplesServer"/>
 <property name="build.dir" value="${app.dir}\build" />
 <property name="build.classes.dir" value="${build.dir}\classes" />
 <!-- Build classpath -->
 <path id="build.classpath">
  <fileset dir="${basedir}\lib\javaee">
   <include name="*.jar" />
  </fileset>
  <fileset dir="${weblogic.home}\server\lib">
   <include name="weblogic.jar" />
  </fileset>
  <pathelement location="${build.classes.dir}" />
 </path>
 <!-- 定義一個Ant任務標籤 -->
 <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy" classpathref="build.classpath"/>
 <!-- =================================================================== -->
 <!-- Prepares the build directory                                        -->
 <!-- =================================================================== -->
 <target name="prepare" depends="clean">
  <mkdir dir="${build.dir}" />
  <mkdir dir="${build.classes.dir}" />
 </target>

 <!-- =================================================================== -->
 <!-- Compiles the source code                                            -->
 <!-- =================================================================== -->
 <target name="compile" depends="prepare" description="編繹">
  <javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**">
   <classpath refid="build.classpath" />
  </javac>
 </target>

 <target name="ear" depends="compile" description="建立JavaEE發布包">
  <jar jarfile="${app.dir}\HelloWorld.jar">
   <fileset dir="${build.classes.dir}">
    <include name="com/**/*.class" />
   </fileset>
  </jar>
     <war warfile="${app.dir}\HelloWorld.war" webxml="${app.dir}\web\WEB-INF\web.xml">
       <fileset dir="${app.dir}/web">
         <include name="*.*"/>
       </fileset>
     </war>
  <ear earfile="${app.dir}\HelloWorld.ear" appxml="${app.dir}\META-INF\application.xml">
   <fileset dir="${app.dir}">
    <include name="HelloWorld.jar"/>
    <include name="HelloWorld.war"/>
   </fileset>
  </ear>
  <delete file="${app.dir}\HelloWorld.jar" />
  <delete file="${app.dir}\HelloWorld.war" />
 </target>
 <!-- =================================================================== -->
 <!-- 需要先啟動weblogic                                                    -->
 <!-- =================================================================== -->
 <target name="deploy" depends="undeploy,ear" description="把應用發布到weblogic,部署名稱為HelloWorld,你不再需要手工部署">
  <wldeploy action="deploy" name="HelloWorld"
       source="${app.dir}\HelloWorld.ear" targets="${wls.server.name}"
       user="${wls.username}" password="${wls.password}"
       adminurl="t3://${wls.hostname}:${wls.port}"
       debug="true" verbose="true" failonerror="true"/>
    <echo message="你可以通過http://${wls.hostname}:${wls.port}/HelloWorld/Test.jsp調用EJB"/>
    <echo message="或者通過單元測試用例HelloWorldTest調用EJB,需要使用JDK1.6,並且把[weblogic home]\wlserver_10.3\server\lib\weblogic.jar加入到類路徑下"/>
 </target>

 <target name="undeploy" description="卸載部署名稱為HelloWorld的應用">
  <wldeploy action="undeploy" name="HelloWorld" targets="${wls.server.name}"
   user="${wls.username}" password="${wls.password}"
   adminurl="t3://${wls.hostname}:${wls.port}"
      debug="false" verbose="false" failonerror="false" />
 </target>
 
 <!-- =================================================================== -->
 <!-- Cleans up generated stuff                                           -->
 <!-- =================================================================== -->
 <target name="clean">
  <delete dir="${build.dir}" />
 </target>
</project>

 

而如果用J2SE的測試檔案:

 

package junit.test;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.junit.Test;

import com.liao.HelloWorld;
/**
 * 執行該用例,你需要JDK1.6以上版本,並且需要把[weblogic home]\wlserver_10.3\server\lib\weblogic.jar加入到類路徑下
 * @author lihuoming
 *
 */
public class HelloWorldTest {
 
 @Test
 public void testSayHello() {
  try {
    Properties props = new Properties();
    props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    props.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
    InitialContext ctx = new InitialContext(props);
    HelloWorld remote = (HelloWorld) ctx.lookup("HelloWorldBean#"+ HelloWorld.class.getName());
    System.out.println(remote.SayHello("test"));
  } catch (NamingException e) {
   e.printStackTrace();
  }
 }

}

 

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.