實例與上一篇GlassFish一致,應用伺服器換成wildfiy主要介紹差異部分。 1.準備工作,下載wildfly 10.0.0.final 2.創建管理員和用戶, 解壓縮wildfly-10.0.0.Final,在解壓後的文件夾中wildfly-10.0.0.Final\bin 下運行add-u ...
實例與上一篇GlassFish一致,應用伺服器換成wildfiy主要介紹差異部分。
1.準備工作,下載wildfly 10.0.0.final
2.創建管理員和用戶, 解壓縮wildfly-10.0.0.Final,在解壓後的文件夾中wildfly-10.0.0.Final\bin 下運行add-user,參考文檔進行操作
3.NetBeans伺服器中添加伺服器->WildFly 應用伺服器->選擇解壓文件夾->使用預設埠和ip
4.修改CCEnterpriseApplication項目屬性->運行->伺服器選擇WildFly 應用伺服器,修改CCEnterpriseApplication-ejb,CCEnterpriseApplication-war 伺服器改成WildFly 應用伺服器
5.修改CCClient庫,去除gf-client.jar ,添加 jboss-client.jar (wildfly-10.0.0.Final\bin\client 文件夾下)
6.修改客戶端連接代碼,需要用第4種連接方式,前面三種都不行,Context的參數也發生變化,根據wildfly文檔進行修改,用戶名密碼是步驟2時建立的,Application User 就可以了。
try { Date d = new Date(); Hashtable env = new Hashtable(); //GlassFish // env.put("org.omg.CORBA.ORBInitialHost", "localhost"); // env.put("org.omg.CORBA.ORBInitialPort", "3700"); //Jboss env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); env.put(Context.SECURITY_PRINCIPAL,"user");//用戶名 env.put(Context.SECURITY_CREDENTIALS, "user");//密碼 env.put("jboss.naming.client.ejb.context", true); Context context = new InitialContext(env); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); /** * 1."cc.test.ejb.CCSessionBeanRemote" * 2.CCSessionBeanRemote.class.getName() * 3.java:global/CCEnterpriseApplication/CCSessionBean * 4.CCEnterpriseApplication/CCEnterpriseApplication-ejb/CCSessionBean!cc.test.ejb.CCSessionBeanRemote" * */ CCSessionBeanRemote ccSession = (CCSessionBeanRemote)context.lookup("CCEnterpriseApplication/CCEnterpriseApplication-ejb/CCSessionBean!"+CCSessionBeanRemote.class.getName()); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); this.messageLabel.setText(ccSession.checkConn()); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); CCSessionBeanRemote ccSession2 = (CCSessionBeanRemote)context.lookup("CCEnterpriseApplication/CCEnterpriseApplication-ejb/CCSessionBean!"+CCSessionBeanRemote.class.getName()); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); } catch (NamingException ex) { Logger.getLogger(LoginJFrame.class.getName()).log(Level.SEVERE, null, ex); }
7.在NetBeans 中啟動WildFly伺服器,部署CCEnterpriseApplication 項目,運行CCClient,點擊連接按鈕,查看輸出結果,與GlassFish對比可以發現獲取Context和EJB都很快不到1s就可以完成,Context.lookup時Jboss的提示信息顯示Remoteing connection信息已經變了。這個以後看看會不會有其他影響。
run: 五月 29, 2016 4:15:41 上午 org.xnio.Xnio <clinit> INFO: XNIO version 3.3.4.Final 五月 29, 2016 4:15:41 上午 org.xnio.nio.NioXnio <clinit> INFO: XNIO NIO Implementation Version 3.3.4.Final 五月 29, 2016 4:15:41 上午 org.jboss.remoting3.EndpointImpl <clinit> INFO: JBoss Remoting version 4.0.18.Final 五月 29, 2016 4:15:41 上午 cc.test.client.LoginJFrame jButton1MouseClicked 警告: 265 五月 29, 2016 4:15:42 上午 org.jboss.ejb.client.remoting.VersionReceiver handleMessage INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river] 五月 29, 2016 4:15:42 上午 org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@7483d09c, receiver=Remoting connection EJB receiver [connection=Remoting connection <a659c6a>,channel=jboss.ejb,nodename=dev-pc]} on channel Channel ID bdc84b3b (outbound) of Remoting connection 54d9e20f to localhost/127.0.0.1:8080 五月 29, 2016 4:15:42 上午 org.jboss.ejb.client.EJBClient <clinit> INFO: JBoss EJB Client version 2.1.4.Final 五月 29, 2016 4:15:42 上午 cc.test.client.LoginJFrame jButton1MouseClicked 警告: 624 五月 29, 2016 4:15:42 上午 cc.test.client.LoginJFrame jButton1MouseClicked 警告: 640 五月 29, 2016 4:15:42 上午 cc.test.client.LoginJFrame jButton1MouseClicked 信息: 640 五月 29, 2016 4:16:20 上午 cc.test.client.LoginJFrame jButton1MouseClicked 警告: 0 五月 29, 2016 4:16:20 上午 org.jboss.ejb.client.remoting.VersionReceiver handleMessage INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river] 五月 29, 2016 4:16:20 上午 org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@302d6389, receiver=Remoting connection EJB receiver [connection=Remoting connection <2f223c50>,channel=jboss.ejb,nodename=dev-pc]} on channel Channel ID af345337 (outbound) of Remoting connection 674abfe5 to localhost/127.0.0.1:8080 五月 29, 2016 4:16:20 上午 cc.test.client.LoginJFrame jButton1MouseClicked 警告: 62 五月 29, 2016 4:16:20 上午 cc.test.client.LoginJFrame jButton1MouseClicked 警告: 62 五月 29, 2016 4:16:20 上午 cc.test.client.LoginJFrame jButton1MouseClicked 信息: 62
總結一下,從WildFly和GlassFish EJB的遠端方法調用可以發現,跟官方文件差別還是有的,從EJB lookup 的路徑可以發現,GlassFish 是一個remote對應一個EJB,WildFly是一個項目下麵一個remote可以對應多個EJB。一個remote有多個實現是很正常,但是遠端方法調用我也不需要知道到底是哪個來實現的,對寫代碼都沒影響。還有一處client端的jar包問題,WildFly比較簡單,一個單獨的jar文件就可以了,GlassFish 側重在Server Container 的概念,在與伺服器同一臺機子會比較簡單,一但到實際分發的時候就比較麻煩,因為引入的jar包裡面都是關聯到其他jar包的。實際jar是空的,可以查看官方文檔關於發佈的介紹,使用方式是package-appclient ,以後再詳細說明。
WildFly 文檔EJB remote client 具體頁面
csdn地址:http://blog.csdn.net/qq_31417619/article/details/51530162
完整客戶端代碼
package cc.test.client; import cc.test.ejb.CCSessionBeanRemote; import java.sql.Time; import java.util.Date; import java.util.Hashtable; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.Context; /** * * @author dev */ public class LoginJFrame extends javax.swing.JFrame { /** * Creates new form JFrame */ public LoginJFrame() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); messageLabel = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("EJB連接測試"); jButton1.setText("連接EJB glassfish"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton1MouseClicked(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(129, 129, 129) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(messageLabel) .addComponent(jLabel1))) .addGroup(layout.createSequentialGroup() .addGap(92, 92, 92) .addComponent(jButton1))) .addContainerGap(173, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(134, 134, 134) .addComponent(messageLabel) .addGap(40, 40, 40) .addComponent(jButton1) .addContainerGap(78, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { try { Date d = new Date(); Hashtable env = new Hashtable(); //GlassFish //env.put("org.omg.CORBA.ORBInitialHost", "localhost"); // env.put("org.omg.CORBA.ORBInitialPort", "3700"); //Jboss env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); env.put(Context.SECURITY_PRINCIPAL,"user");//用戶名 env.put(Context.SECURITY_CREDENTIALS, "user");//密碼 env.put("jboss.naming.client.ejb.context", true); Context context = new InitialContext(env); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); /** * 1."cc.test.ejb.CCSessionBeanRemote" * 2.CCSessionBeanRemote.class.getName() * 3.java:global/CCEnterpriseApplication/CCSessionBean * 4.CCEnterpriseApplication/CCEnterpriseApplication-ejb/CCSessionBean!cc.test.ejb.CCSessionBeanRemote" * */ CCSessionBeanRemote ccSession = (CCSessionBeanRemote)context.lookup("CCEnterpriseApplication/CCEnterpriseApplication-ejb/CCSessionBean!"+CCSessionBeanRemote.class.getName()); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); this.messageLabel.setText(ccSession.checkConn()); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); //CCSessionBeanRemote ccSession2 = (CCSessionBeanRemote)context.lookup("CCEnterpriseApplication/CCEnterpriseApplication-ejb/CCSessionBean!"+CCSessionBeanRemote.class.getName()); Logger.getLogger(LoginJFrame.class.getName()).log(Level.WARNING,String.valueOf(new Date().getTime()-d.getTime())); } catch (NamingException ex) { Logger.getLogger(LoginJFrame.class.getName()).log(Level.SEVERE, null, ex); } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new LoginJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel messageLabel; // End of variables declaration }View Code