jboss3.2初步配置使用心得[windows2000+sql server 2000]

类别:Java 点击:0 评论:0 推荐:

1、下载和安装
 <1>下载可以到www.jboss.org下载,我下的是jboss3.2.1。并且不是那个和tomcat整合的版本。
 <2>安装方法就是将文件解压到硬盘的某个目录下(例如:我的解到 d:\jboss-3.2.1)。
2、配置
 <1>首先必须安装jdk。jboss不像weblogic自带java标准开发包。我安装的是1.4  安装目录是C:\j2sdk1.4.0 。
 <2>设置环境变量
  <<1>>JAVA_HOME设置为JDK的安装目录(例如:我的为C:\j2sdk1.4.0)
  <<2>>classpath的设置:看看我的,大家就知道该包含哪些包了
   C:\j2sdk1.4.0\lib\tools.jar;
   D:\jboss-3.2.1\server\default\lib\javax.servlet.jar;
   D:\jboss-3.2.1\server\default\lib\jboss-j2ee.jar;
   .;
   D:\jboss-3.2.1\client\jbossall-client.jar;
   D:\jboss-3.2.1\client\jnet.jar;
   D:\jboss-3.2.1\client\log4j.jar;
3、运行
 进入jboss3.2.1的bin目录,执行run.bat就可以了。看到出现
 00:59:26,978 INFO  [Server] JBoss (MX MicroKernel) [3.2.1 (build: CVSTag=JBoss_3
 _2_1 date=200305041533)] Started in 17s:595ms
 这样的提示信息就说明启动成功,测试方法如下
 http://localhost:8080/jmx-console 你将看到jmx agent view

4、使用
 <1>html,jsp测试
  jboss的默认web目录是在jboss-3.2.1\server\default\deploy下面。下面就介绍测试jsp,servlet,ejb的方法:
  对于测试首先在deploy下建立一个test.war文件夹,然后将html,jsp文件都可以放到这里进行测试
  例如建立一test.jsp如下:
  <%@ page language="java" %>
  <%@ page contentType="text/html;charset=gb2312" %>
  <%@ page import="java.util.*" %>
  <%=new Date().toLocalString()%>
  通过http://localhost:8080/test/test.jsp调用就可得到当前日期时间。
 <2>设置sql server2000的数据源然后在jsp中操调用操作数据库
  <<1>>下载安装sql server2000的jdbc驱动(微软提供)
  <<2>>将mssqlserver,msbase,msutil三个jar文件复制到jboss-3.2.1\server\default\lib目录下面
  <<3>>将jboss-3.2.1\docs\examples\jca\mssql-ds.xml复制一份到jboss-3.2.1\server\default\deploy下面
      部分修改如下:
      <datasources>
    <local-tx-datasource>
     <jndi-name>ds</jndi-name>
     <connection-url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind</connection-url>
     <driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
     <user-name>sa</user-name>
     <password></password>
     </local-tx-datasource>
     </datasources>

     上面的配置就是用ds作为数据源的名称,用不带密码的sa用户连接sql server的样板数据库northwind。你可以作适当的修改。
  <<4>>配置完成后,用如下testSql.jsp文件测试数据库操作
   <%@ page language="java" %>
   <%@ page import="javax.naming.*" %>
   <%@ page import="javax.sql.*" %>
   <%@ page import="java.sql.*" %>
   <%!
    javax.sql.DataSource ds;
   %>
 
   <%
 
    try {
     Context ctx = new InitialContext();
     ds = (javax.sql.DataSource)ctx.lookup("java:/ds");
    } catch (Exception e) {
     out.println("erro when connect to java naming api");
    }
 
 
    Connection conn = ds.getConnection();
    Statement st = conn.createStatement();
    String sqlStr = "select * from categories";
    ResultSet rs = st.executeQuery(sqlStr);
    while  ( rs.next() ) {
     out.println(rs.getString("categoryName")+"<br>");
    }
    rs.close();
    st.close();
    conn.close();
   %>
   
   将输出categories表的每条记录的categoryName,值得注意的是在mssql-ds.xml设置的jndi-name是ds,而真正的绑定是
   java:/ds。

 <3> servlet的配置测试
  <<1>>编写并编译如下的简单servlet
  import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;

  public class HelloWorld extends HttpServlet {
   public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("");
    out.println("Hello World");
    out.println("<p><p><p><p><div align='right'>author's email:[email protected]</div>");
   }
  }
  <<2>>在test.war文件夹下建立一个WEB-INF目录,里面再建立一个classes目录和web.xml文件目录结构如下:
  [test.war]
   [WEB-INF]
    [classes]
    web.xml
  将编译成功的HelloWorld.class放置到classes目录下,web.xml内容如下:

  <?xml version="1.0" encoding="ISO-8859-1"?>

  <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
   "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
   
  <web-app>
   <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>HelloWorld</servlet-class>        
   </servlet>
    <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
   </servlet-mapping>
  </web-app>

  通过调用http://localhost:8080/test/HelloWorld 就可以看到hello world的输出
 
 <4>session bean的测试(Hello)
  这是一个简单的stateless session bean的测试
  <<1>>Hello.java

   import java.rmi.RemoteException;
   import javax.ejb.EJBObject;

   public interface Hello extends javax.ejb.EJBObject {
    public String hello() throws java.rmi.RemoteException;
   }

  <<2>>HelloHome.java

   import java.rmi.RemoteException;

   public interface HelloHome extends javax.ejb.EJBHome {
    Hello create() throws java.rmi.RemoteException,javax.ejb.CreateException;
   }

  <<3>>HelloBean.java
   
   import javax.ejb.SessionBean;
   import javax.ejb.SessionContext;

   public class HelloBean implements javax.ejb.SessionBean {
    private SessionContext ctx;
 
    public void ejbCreate() {
     System.out.println("ejbCreate()");
    }
 
    public void ejbRemove() {
     System.out.println("ejbRemove()");
    }
 
    public void ejbActivate() {
     System.out.println("ejbActivate()");
    }
 
    public void ejbPassivate() {
     System.out.println("ejbPassivate()");
    }
 
    public void setSessionContext(javax.ejb.SessionContext ctx) {
     his.ctx = ctx;
    }
 
    public String hello() {
     System.out.println("hello()");
     return "hello,world";
    }
   }
  <<4>>ejb-jar.xml
   <?xml version="1.0"?>
   <!DOCTYPE ejb-jar PUBLIC
    '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
    'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

   <ejb-jar>
    <enterprise-beans>
     <session>
      <ejb-name>Hello</ejb-name>
      <home>HelloHome</home>
      <remote>Hello</remote>
      <ejb-class>HelloBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
     </session>
    </enterprise-beans>
   </ejb-jar>
  <<5>>jboss.xml
   <?xml version="1.0" encoding="UTF-8"?>
   <jboss>
    <enterprise-beans>
     <session>
      <ejb-name>Hello</ejb-name>
      <jndi-name>HelloEJB</jndi-name>
     </session>
    </enterprise-beans>
   </jboss>
  <<6>>

  编译3个.java文件后按照以下路径存放文件(假设放到d:\sessionEjb下面)
  d:\sessionEjb\Hello.class
  d:\sessionEjb\HelloBean.class
  d:\sessionEjb\HelloHome.class
  d:\sessionEjb\META-INF\ejb-jar.xml
  d:\sessionEjb\META-INF\jboss.xml

  然后打包d:\sessionEjb\jar cvf helloejb.jar *.*,打包后将helloejb.jar放到deploy目录下面,会在控制台看到如下输出

  11:35:30,815 INFO  [EjbModule] Creating
  11:35:30,825 INFO  [EjbModule] Deploying Hello
  11:35:30,835 INFO  [StatelessSessionContainer] Creating
  11:35:30,835 INFO  [StatelessSessionInstancePool] Creating
  11:35:30,835 INFO  [StatelessSessionInstancePool] Created
  11:35:30,845 INFO  [StatelessSessionContainer] Created
  11:35:30,845 INFO  [EjbModule] Created
  11:35:30,845 INFO  [EjbModule] Starting
  11:35:30,845 INFO  [StatelessSessionContainer] Starting
  11:35:30,905 INFO  [StatelessSessionInstancePool] Starting
  11:35:30,905 INFO  [StatelessSessionInstancePool] Started
  11:35:30,905 INFO  [StatelessSessionContainer] Started
  11:35:30,905 INFO  [EjbModule] Started
  11:35:30,905 INFO  [EJBDeployer] Deployed: file:/D:/jboss-3.2.1/server/default/d
  eploy/helloejb.jar
  11:35:30,925 INFO  [MainDeployer] Deployed package: file:/D:/jboss-3.2.1/server/
  default/deploy/helloejb.jar

  表明配置没有错误信息


  测试的客户端代码

  import javax.naming.InitialContext;
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.ejb.CreateException;
  import java.rmi.RemoteException;
  import javax.rmi.PortableRemoteObject;
  import java.util.Properties;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;


  public class HelloWorldClient extends JFrame implements ActionListener {
   public HelloWorldClient() {
    addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
      System.exit(0);
     }
    });
    setTitle("EJBClient");
    setSize(300,200);
    Container contentPane = getContentPane();
    area = new JTextArea(6,20);
    subButton = new JButton("Say hello to EJB");
    subButton.setActionCommand("sya");
    subButton.addActionListener(this);
    contentPane.add(area,"Center");
    contentPane.add(subButton,"South");
   }
   public void actionPerformed(ActionEvent e) {
    if ( e.getActionCommand().equals("sya") ) {
     ejbSays = getEJBInfo();
     area.append(ejbSays + "\n");
    }
   }
  
   public String getEJBInfo() {
    try {
     Context jndiContext = getInitialContext();
     Object ref = jndiContext.lookup("HelloEJB");
     HelloHome home = (HelloHome)PortableRemoteObject.narrow(ref,HelloHome.class);
     Hello hw= home.create();
     ejbSays = hw.hello();
   
    } catch (java.rmi.RemoteException e) {
     e.printStackTrace();
    } catch ( Throwable t ) {
     t.printStackTrace();
    } finally {
   
    }
    return ejbSays;
   }
 
   public static Context getInitialContext() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY,
             "org.jnp.interfaces.NamingContextFactory");
           p.put(Context.PROVIDER_URL, "localhost:1099");
           return new InitialContext(p);
   }
       
   public static void main(String[] args) {
           JFrame win = new HelloWorldClient();
           win.pack();
           win.setVisible(true);
   }
       
   private String ejbSays="";
   private JTextField info;
   private JButton subButton;
   private JTextArea area;
  }

  作了一个简单的gui,主要注意的是getInitalContext()方法jboss的连接参数的设置和getEJBInfo()方法对ejb的调用

  将编译好的HelloWorldClient.class,HelloWorldClient$1.class,放置到d:\sessionbean目录下(如果放到别的地方,确定可以
  通过路径找到HelloBean,HelloHome,Hello类

  d:\sessionbean\java HelloWorldClient

  点按钮就会返回hello world(做gui是为了复习一下以前的知识)

 <5>cmp的测试
  此cmp继续使用上面的数据源对应的northwind数据库,要在其中建一个表cd。
  <<1>>CD.java

   import javax.ejb.EJBObject;
   import java.rmi.RemoteException;

   public interface CD extends EJBObject {

    public Integer getId() throws RemoteException;
    public void setId(Integer id) throws RemoteException;
 
    public String getTitle() throws RemoteException;
    public void setTitle(String title) throws RemoteException;

    public String getArtist() throws RemoteException;
    public void setArtist(String artist) throws RemoteException;

    public String getType() throws RemoteException;
    public void setType(String type) throws RemoteException;

    public String getNotes() throws RemoteException;
    public void setNotes(String notes) throws RemoteException;
   }

  <<2>>CDHome.java

   import javax.ejb.EJBHome;
   import javax.ejb.CreateException;
   import javax.ejb.FinderException;
   import java.rmi.RemoteException;
   import java.util.Collection;

   public interface CDHome extends EJBHome {
    public CD create(Integer id,String  title,String artist,String type,String notes) throws RemoteException,CreateException;

    public CD findByPrimaryKey(Integer id) throws RemoteException,FinderException;

    public Collection findByType(String type) throws RemoteException,FinderException;

    
   }

  <<3>>CDBean.java
   import javax.ejb.EntityBean;
   import javax.ejb.EntityContext;
   import java.rmi.RemoteException;
   import javax.ejb.CreateException;

   public abstract class CDBean implements EntityBean {
    EntityContext ctx;

    public Integer id;
    public String title;
    public String artist;
    public String type;
    public String notes;

    public Integer ejbCreate(Integer id,String title,String artist,String type,String notes) throws CreateException{
     setId(id);
     setTitle(title);
     setArtist(artist);
     setType(type);
     setNotes(notes);
     return id;
    }

    public void ejbPostCreate(Integer id,String title,String artist,String type,String notes) {
    }

    public abstract String getTitle() ;

    public abstract void setTitle(String _title) ;
 
    public abstract Integer getId() ;

    public abstract void setId(Integer _id);
 
    public abstract String getArtist();

    public abstract void setArtist(String _artist) ;

    public abstract String getType() ;
 
    public abstract void setType(String _type) ;

    public abstract String getNotes() ;

    public abstract void setNotes(String _notes);

    public void setEntityContext(EntityContext ctx) {
     this.ctx = ctx;
    }

    public void unsetEntityContext() {
     ctx = null;
    }

    public void ejbActivate() {}
    public void ejbPassivate() {}
    public void ejbLoad() {}
    public void ejbStore() {}
    public void ejbRemove() {}
   }

  <<4>>ejb-jar.xml

   <?xml version="1.0"?>

   <!DOCTYPE ejb-jar PUBLIC
   "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
   "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

   <ejb-jar>
    <display-name>MusicCDs</display-name>
    <enterprise-beans>
     <entity>
      <description>Modles a music CD</description>
      <ejb-name>CDBean</ejb-name>
      <home>CDHome</home>
      <remote>CD</remote>
      <ejb-class>CDBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>cd</abstract-schema-name>
      <cmp-field><field-name>id</field-name></cmp-field>
      <cmp-field><field-name>title</field-name></cmp-field>
      <cmp-field><field-name>artist</field-name></cmp-field>
      <cmp-field><field-name>type</field-name></cmp-field>
      <cmp-field><field-name>notes</field-name></cmp-field>

      <primkey-field>id</primkey-field>

      <query>
       <query-method>
        <method-name>findByType</method-name>
        <method-params>
         <method-param>java.lang.String</method-param>
        </method-params>
       </query-method>
       <ejb-ql><![CDATA[
        SELECT OBJECT(A)
        FROM cd AS A
        WHERE A.type=?1
       ]]></ejb-ql>
      </query>
     </entity>
    </enterprise-beans>

    <assembly-descriptor>
     <container-transaction>
      <method>
       <ejb-name>CDBean</ejb-name>
       <method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
     </container-transaction>
    </assembly-descriptor>
   </ejb-jar>

  <<5>>jboss.xml

   <?xml version="1.0" encoding="UTF-8"?>
 
   <!DOCTYPE jboss PUBLIC
    "-//JBoss//DTD JBOSS 3.0//EN"
    "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">


   <jboss>
    <enterprise-beans>
     <entity>
      <ejb-name>CDBean</ejb-name>
      <jndi-name>ejb/cdbean</jndi-name>
     </entity>
    </enterprise-beans>
   </jboss>

  <<6>>jbosscmp-jdbc.xml

   <?xml version="1.0" encoding="UTF-8"?>

   <!DOCTYPE jbosscmp-jdbc PUBLIC  
    "-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN"
   "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_0.dtd">

   <jbosscmp-jdbc>
    <defaults>
     <datasource>java:/ds</datasource>
     <datasource-mapping>MS SQLSERVER2000</datasource-mapping>
     <create-table>true</create-table>
     <remove-table>true</remove-table>
     <pk-constraint>true</pk-constraint>
     <preferred-relation-mapping>foreign-key</preferred-relation-mapping>
    </defaults>

    <enterprise-beans>

     <entity>
      <ejb-name>CDBean</ejb-name>
      <table-name>cd</table-name>

      <cmp-field>
       <field-name>id</field-name>
       <column-name>id</column-name>
      </cmp-field>

      <cmp-field>
       <field-name>title</field-name>
       <column-name>title</column-name>
      </cmp-field>

      <cmp-field>
       <field-name>artist</field-name>
       <column-name>artist</column-name>
      </cmp-field>

      <cmp-field>
       <field-name>type</field-name>
       <column-name>type</column-name>
      </cmp-field>

      <cmp-field>
       <field-name>notes</field-name>
       <column-name>notes</column-name>
      </cmp-field>
     </entity>

    </enterprise-beans>

  
   </jbosscmp-jdbc>

   需要解释的是
   <create-table>true</create-table>
   <remove-table>true</remove-table>
   会在部署的时候自动在库中建立数据表,而在删除的时候同时删除对应的表

  <<7>>编译部署
   编译3个.java文件,按照下面的目录结构存放(假设放在d:\cmpbean下面)
   d:\cmpbean\CD.class
   d:\cmpbean\CDHome.class
   d:\cmpbean\CDBean.class
   d:\cmpbean\META-INF\ejb-jar.xml
   d:\cmpbean\META-INF\jboss.xml
   d:\cmpbean\META-INF\jbosscmp-jdbc.xml

   打包:d:\cmpbean\jar cvf cdejb.jar *.*

   将cdejb.jar复制到deploy目录下

  <<8>>客户端代码测试

   import javax.naming.InitialContext;
   import javax.naming.Context;
   import javax.naming.NamingException;
   import javax.ejb.CreateException;
   import java.rmi.RemoteException;
   import javax.rmi.PortableRemoteObject;
   import java.util.Properties;
   import java.util.*;

   public class CDClient {
    public static Context getInitialContext() throws Exception {
     Properties p = new Properties();
     p.put(Context.INITIAL_CONTEXT_FACTORY,
               "org.jnp.interfaces.NamingContextFactory");
     p.put(Context.PROVIDER_URL, "localhost:1099");
     return new InitialContext(p);
    }

    public static void main(String[] args) {
     try {
      Context jndiContext = getInitialContext();
      Object ref= jndiContext.lookup("ejb/cdbean");
      CDHome home = (CDHome)PortableRemoteObject.narrow(ref,CDHome.class);
      home.create(new Integer(100),"Bohn jove","rock and roll","rock","a good type");
      home.create(new Integer(200),"黑宝","rock","rock","roll star");
      home.create(new Integer(300),"mike jackson","sssss","iiop","none");
      Collection c = home.findByType("rock");
      Iterator i = c.iterator();
      while ( i.hasNext() ) {
       String ss = ( (CD)javax.rmi.PortableRemoteObject.narrow(i.next(),CD.class)).getNotes();
       System.out.println(ss);
      }

     } catch (java.rmi.RemoteException e) {
      e.printStackTrace();
     } catch (Throwable t) {
      t.printStackTrace();
     } finally {
     }
    }
   }

   请确保通过类路径可以找到CD.class,CDHome.Class.
   执行java CDclient 将看到如下输出:

   D:\cmpbean\>java CDClient
   a good type
   roll star

   D:\cmpbean\>

5、结束语

 首先我也是个初学者,自己在csdn上多次提问有关jboss的问题,却所得到回答甚少。
 而且搜索了很久也找不到相应的文档可以了解jboss3.2的使用。
 为此希望写此文档为初学者提供一点帮助,错误的地方还请指正。
 另外、对于jboss3.2 mssql-xa-ds.xml的设置以及连接池的
 使用方法,有谁弄过,请告诉我,不胜感激。 email: [email protected]

 

  

 


 

 

 

 

 

本文地址:http://com.8s8s.com/it/it17403.htm