JBOSS(4.2、7.1)+eclipse+EJB3

1、JBOSSeclipse的集成

(1)JBOSS4.2

直接下載,解壓即可使用。

 

(2)JBOSS7.1

下載JBOSS7.1

下載安裝JBOSSTOOLS

步驟:【eclipse】→【幫助】→【Install New Software】→【Add】→【Name】:JBossTools→【Location】:http://download.jboss.org/jbosstools/updates/development/indigo/ 

下載安裝JBossTools Aop:

步驟:【eclipse】→【幫助】→【Install New Software】→【Add】→【Name】:JBossTools→【Location】:http://download.jboss.org/jbosst ... indigo/soa-tooling/ 

 

然後再環境變量中進行配置,JBOSS_HOME=JBOSS的安裝目錄,再在path中加入%JBOSS_HOME%\bin;

 

(3)集成:在Server配置中可以根據不同版本加入JBOSS,不知什麼原因,JBOSS4.2的安裝目錄不要帶有中文和空格,我出錯了,改完後就成功。JBOSS7.1沒出現這種情況。

 

 

2、EJB3.0的開發

創建java項目、直接建EJB項目都可以。創建java項目需要導入相應的jar包,在JBOSS的client目錄下有。若是EJB項目,創建時會自動導入。

 

 

3、EJB3.0在JBOSS中發佈

Eclipse中集成好JBOSS,可以直接部署,不用手動拷貝。用java創建web時,導出成war時會報錯,關於Module的錯誤。但通過ant可以導出。以下是build.xml文件的格式。

<?xml version="1.0"?>

 

<!-- ======================================================================= -->

<!-- WEB EJBTest build file                                                       -->

<!-- ======================================================================= -->

 

<project name="EJBTest" default="web-war" basedir=".">

 

<property environment="env" />

<property name="resouce.dir" value="${basedir}/web" />

<property name="jboss.home" value="${env.JBOSS_HOME}" />

<property name="jboss.server.config" value="default" />

 

<target name="web-war" description="創建WEB發佈包">

    <war warfile="${basedir}/EJBTest.war" webxml="${resouce.dir}/WEB-INF/web.xml">

      <fileset dir="${resouce.dir}">

        <include name="**/*.jsp"/>

      </fileset>

    </war>

</target>

 

<target name="deploy" depends="web-war">

<copy file="${basedir}/EJBTest.war" todir="${jboss.home}/server/${jboss.server.config}/deploy" />

</target>

 

<!-- =================================================================== -->

<!-- Cleans up generated stuff                                           -->

<!-- =================================================================== -->

<target name="clean">

<delete file="${jboss.home}/server/${jboss.server.config}/deploy/EJBTest.war" />

</target>

 

</project>

 

 

4、客戶端訪問EJB

都需要現在客戶端創建對應的接口(全名要一致)

(1)EJB發佈在JBOSS4.2

<%@ page import="com.xzb.ejb.HelloWorldRemote, javax.naming.*, java.util.Properties"%>

<%

Properties props = new Properties();

props.setProperty("java.naming.factory.initial""org.jnp.interfaces.NamingContextFactory");

props.setProperty("java.naming.provider.url""localhost:1099");

props.setProperty("java.naming.factory.url.pkgs""org.jboss.naming");

 

InitialContext ctx;

try {

ctx = new InitialContext(props);

HelloWorldRemote helloworld = (HelloWorldRemote) ctx.lookup("HelloWorld/remote");

out.println(helloworld.sayHello("佛山人"));

catch (NamingException e) {

out.println(e.getMessage());

}

%>

 

(2)EJB發佈在JBOSS7.1

配置文件:

內容爲:

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost

remote.connection.default.port = 4447

remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

 

 

訪問程序:

public class HelloWorldTest {

    /**

     * @param args

     */

    public static void main(String[] args) {

        // TODO Auto-generated method stub

                Hashtable<String, String> jndiProperties = new Hashtable<String, String>();

                jndiProperties.put(Context.URL_PKG_PREFIXES"org.jboss.ejb.client.naming");

                try {

                    Context context = new InitialContext(jndiProperties);

                   

                    final String appName = "";

                    final String moduleName = "Hello";

                    final String distinctName = "";

                   

                    Object obj = context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/HelloWorldBean!com.foshanshop.ejb3.HelloWorld");

                   

                    HelloWorld hwr =(HelloWorld)obj;

                    String say = hwr.SayHello("小明");

                    System.out.println(say);

                } catch (NamingException e) {

                    e.printStackTrace();

                }

    }

 

}

 

說明:"/HelloWorldBean!com.foshanshop.ejb3.HelloWorld"

前面是實現接口的類,後面是接口全名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章