websphere6.1開發、部署、遠程調用EJB2.0

來源:互聯網
上載者:User

開發工具是IBM RAD 7.5.4。

WAS版本6.1。

EJB版本2.0。


開發過程如圖:





建立完後,工程結構如下:




再建立SessionBean




下一步




完成。

如圖:




修改幾個檔案:

package com.ejb;import static java.lang.System.out;/** * Bean implementation class for Enterprise Bean: HelloSession */public class HelloSessionBean implements javax.ejb.SessionBean {static final long serialVersionUID = 3206093459760846163L;private javax.ejb.SessionContext mySessionCtx;/** * getSessionContext */public javax.ejb.SessionContext getSessionContext() {return mySessionCtx;}/** * setSessionContext */public void setSessionContext(javax.ejb.SessionContext ctx) {mySessionCtx = ctx;}/** * ejbCreate */public void ejbCreate() throws javax.ejb.CreateException {out.println("ejbCreate");}/** * ejbActivate */public void ejbActivate() {out.println("ejbActivate");}/** * ejbPassivate */public void ejbPassivate() {out.println("ejbPassivate");}/** * ejbRemove */public void ejbRemove() {out.println("ejbRemove");}public String sayHello(String someOne) {out.println("sayHello");return "Hello, " + someOne + "!";}}

package com.ejb;import java.rmi.RemoteException;/** * Remote interface for Enterprise Bean: HelloSession */public interface HelloSession extends javax.ejb.EJBObject {public String sayHello(String someOne) throws RemoteException;}

增加一個測試類別:

package com.ejb;import java.rmi.RemoteException;import java.util.Properties;import javax.ejb.CreateException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.rmi.PortableRemoteObject;public class TestClient {/** * @param args * @throws NamingException * @throws CreateException * @throws RemoteException */public static void main(String[] args) throws NamingException,RemoteException, CreateException {Properties props = new Properties();props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");props.setProperty(Context.PROVIDER_URL, "iiop://127.0.0.1:2810/");Context ctx = new InitialContext(props);Object objref = ctx.lookup("ejb/com/ejb/HelloSessionHome");HelloSessionHome home = (HelloSessionHome) PortableRemoteObject.narrow(objref, HelloSessionHome.class);HelloSession hello = home.create();String msg = hello.sayHello("ejb");System.out.println(msg);}}

上面jndi的名字,可以在ibm-ejb-jar-bnd.xmi中找到,其內容:

<?xml version="1.0" encoding="UTF-8"?><ejbbnd:EJBJarBinding xmi:version="2.0"xmlns:xmi="http://www.omg.org/XMI" xmlns:ejb="ejb.xmi" xmlns:ejbbnd="ejbbnd.xmi"xmi:id="EJBJarBinding_1393691689296"><ejbJar href="META-INF/ejb-jar.xml#ejb-jar_ID" /><ejbBindings xmi:id="EnterpriseBeanBinding_1393691689296"jndiName="ejb/com/ejb/HelloSessionHome"><enterpriseBean xmi:type="ejb:Session"href="META-INF/ejb-jar.xml#HelloSession" /></ejbBindings></ejbbnd:EJBJarBinding>

ejb-jar.xml內容:

<?xml version="1.0" encoding="UTF-8"?><ejb-jar id="ejb-jar_ID"><display-name>EJB</display-name><enterprise-beans><session id="HelloSession"><ejb-name>HelloSession</ejb-name><home>com.ejb.HelloSessionHome</home><remote>com.ejb.HelloSession</remote><ejb-class>com.ejb.HelloSessionBean</ejb-class><session-type>Stateless</session-type><transaction-type>Bean</transaction-type></session></enterprise-beans><ejb-client-jar>EJBClient.jar</ejb-client-jar></ejb-jar>

上面兩個檔案內容,都是自動產生的,無需修改。如果知道怎麼改,也可以改。


然後把EJBSystem加到WAS6.1裡去,啟動WAS,會產生一些java檔案,如圖:



此時,運行TestClient,會報錯,解決辦法,見http://blog.csdn.net/feier7501/article/details/20247219


上面的問題,解決後,就可以測試成功了。上面的URL裡的連接埠,一般是2809,我這裡的是2810。反正就從2809開始往上加1。


服務端輸出:

[14-3-2 0:44:04:890 CST] 00000041 SystemOut     O ejbCreate[14-3-2 0:44:04:906 CST] 00000041 SystemOut     O sayHello

用戶端輸出:

Hello, ejb!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.