现在的位置: 首页 > 综合 > 正文

ejb3+weblogic 10的一些小结

2011年03月31日 ⁄ 综合 ⁄ 共 7331字 ⁄ 字号 评论关闭

  一个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();
  }
 }

}

 

抱歉!评论已关闭.