使用eclipse通过weblogic开发简单的ejb应用(weblogic 10.x & ejb3.x)

来源:互联网 发布:虚拟社交网络语言规则 编辑:程序博客网 时间:2024/05/20 23:07
  1. 创建maven项目: ilucky-ejb-3.0-weblogic, 然后按照如下过程,将其转换为ejb项目:
    这里写图片描述

直接看serve端代码:

package com.ilucky.ejb;public interface EJBDemoIntf {    String sayHi();}
package com.ilucky.ejb;import java.util.Random;import javax.ejb.Remote;import javax.ejb.Stateless;@Stateless(mappedName="EJBSessBean")@Remote(EJBDemoIntf.class)public class EJBSessBean implements EJBDemoIntf {    public String sayHi() {        Random r = new Random();        int i = r.nextInt(10);        System.out.println("=============EJB Client comming=================(i % 3)=" + (i % 3));        if(i % 3 == 0) {            int j = 0;            System.out.println("========> zero " + i/j);        }        return "Hello World !!!";    }}

pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.ilucky.ejb</groupId>  <artifactId>ilucky-ejb-3.0-weblogic</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>ilucky-ejb-3.0-weblogic</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>            <!-- other plugin versions -->        <version.compiler.plugin>2.3.1</version.compiler.plugin>        <version.ejb.plugin>2.3</version.ejb.plugin>        <!-- maven-compiler-plugin -->        <maven.compiler.target>1.6</maven.compiler.target>        <maven.compiler.source>1.6</maven.compiler.source>  </properties>  <dependencies>      <dependency>            <groupId>javax.ejb</groupId>            <artifactId>javax.ejb-api</artifactId>            <version>3.2</version>        </dependency>         <dependency>            <groupId>com.weblogic</groupId>            <artifactId>weblogic</artifactId>            <version>10.3</version>        </dependency>  </dependencies>  <build>        <!-- <finalName>${artifactId}</finalName> -->        <!-- Set the name of the deployment -->        <plugins>            <!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation                 processors -->            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>${version.compiler.plugin}</version>                <configuration>                    <source>${maven.compiler.source}</source>                    <target>${maven.compiler.target}</target>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-ejb-plugin</artifactId>                <version>${version.ejb.plugin}</version>                <configuration>                    <ejbVersion>3.1</ejbVersion>                    <!-- this is false by default -->                    <generateClient>true</generateClient>                </configuration>            </plugin>        </plugins>    </build></project>

注意: 最后需要将其打包安装到本地仓库,因为下面的client项目会依赖它。
即 mvn clean install

  1. 创建maven项目: ilucky-ejb-3.0-weblogic-client,client代码如下:
package com.ilucky.ejb;import javax.naming.InitialContext;public class EJBDemoTest {    private static String ip="10.0.2.90";    private static String port = "7001";//  /**//   * 获取配置//   *///  public static void initConf() {//      //  }    public static void main(String[] args) {        java.util.Properties prop = new java.util.Properties();          prop.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,  "weblogic.jndi.WLInitialContextFactory");        prop.setProperty(javax.naming.Context.PROVIDER_URL, "t3://"+ip+":" + port);        InitialContext ic;        try {            ic = new InitialContext(prop);            EJBDemoIntf h = (EJBDemoIntf)ic.lookup("EJBSessBean#com.ilucky.ejb.EJBDemoIntf");              String result = h.sayHi();            System.out.println("=======ejb return result=======>"+result);        } catch (Exception e) {            e.printStackTrace();            System.out.println("=====================");        }      }}

pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.ilucky.ejb</groupId>  <artifactId>ilucky-ejb-3.0-weblogic-client</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>ilucky-ejb-3.0-weblogic-client</name>  <url>http://maven.apache.org</url>    <properties>        <!-- Explicitly declaring the source encoding eliminates the following            message: -->        <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered            resources, i.e. build is platform dependent! -->        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <!-- other plugin versions -->        <version.compiler.plugin>2.3.1</version.compiler.plugin>        <version.exec.plugin>1.2.1</version.exec.plugin>        <!-- maven-compiler-plugin -->        <maven.compiler.target>1.6</maven.compiler.target>        <maven.compiler.source>1.6</maven.compiler.source>    </properties>    <dependencies>    <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.0.1</version>        </dependency>        <!-- ADD  -->        <!-- We depend on the EJB remote business interfaces of this application -->        <dependency>             <groupId>com.ilucky.ejb</groupId>            <artifactId>ilucky-ejb-3.0-weblogic</artifactId>            <version>0.0.1-SNAPSHOT</version>            <exclusions>                <exclusion>                    <groupId>com.weblogic</groupId>                    <artifactId>weblogic</artifactId>                </exclusion>            </exclusions>        </dependency>         <dependency>            <groupId>javax.ejb</groupId>            <artifactId>javax.ejb-api</artifactId>            <version>3.2</version>        </dependency>    <dependency>            <groupId>com.wlfullclient</groupId>            <artifactId>wlfullclient</artifactId>            <version>10.3</version>        </dependency>    <!--     <dependency>            <groupId>com.weblogic</groupId>            <artifactId>weblogic</artifactId>            <version>10.3</version>        </dependency> -->     </dependencies>    <build>        <plugins>            <!-- Enforce Java 1.6 -->            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>${version.compiler.plugin}</version>                <configuration>                    <source>${maven.compiler.source}</source>                    <target>${maven.compiler.target}</target>                </configuration>            </plugin>            <!-- Add the maven exec plugin to allow us to run a java program via maven -->            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>exec-maven-plugin</artifactId>                <version>${version.exec.plugin}</version>                <executions>                    <execution>                        <goals>                            <goal>exec</goal>                        </goals>                    </execution>                </executions>                <configuration>                    <executable>java</executable>                    <workingDirectory>${project.build.directory}/exec-working-directory</workingDirectory>                    <arguments>                        <!-- automatically creates the classpath using all project dependencies,                            also adding the project build directory -->                        <argument>-classpath</argument>                        <classpath>                        </classpath>                        <argument>com.ilucky.ejb.Client</argument>                    </arguments>                </configuration>            </plugin>                <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <version>2.3</version>                <configuration>                    <createDependencyReducedPom>true</createDependencyReducedPom>                    <filters>                        <filter>                            <artifact>*:*</artifact>                            <excludes>                                <exclude>META-INF/*.SF</exclude>                                <exclude>META-INF/*.DSA</exclude>                                <exclude>META-INF/*.RSA</exclude>                            </excludes>                        </filter>                    </filters>                    <encoding>UTF-8</encoding>                </configuration>                <executions>                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <transformers>                                <transformer                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />                                <transformer                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <mainClass>com.ilucky.ejb.EJBDemoTest</mainClass>                                </transformer>                            </transformers>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

需要注意的有两点:
1. 如下是对server的依赖。

<dependency>             <groupId>com.ilucky.ejb</groupId>            <artifactId>ilucky-ejb-3.0-weblogic</artifactId>            <version>0.0.1-SNAPSHOT</version>            <exclusions>                <exclusion>                    <groupId>com.weblogic</groupId>                    <artifactId>weblogic</artifactId>                </exclusion>            </exclusions>        </dependency> 
  1. 如下的文件需要自己生成。
    <dependency>            <groupId>com.wlfullclient</groupId>            <artifactId>wlfullclient</artifactId>            <version>10.3</version>        </dependency>

client要导入wlfullclient.jar, 但是你会发现weblogic里面没有带wlfullclient.jar, 需要自己生成:
1 cd C:/${weblogic_home}/wlserver_10.0/server/lib
2 java -jar ../../../modules/com.bea.core.jarbuilder_1.0.7.0.jar(若weblgic版本不同则jarbuilder.jar的版本略有差异略有差异,到../../../modules目录下看一看就知道了)

demo下载地址:
http://download.csdn.net/download/sidongxue2/9942998

原创粉丝点击