Java EE 远程客户的访问EJB实现实例(Jboss wildfly)

实例与上一篇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

 

posted @ 2016-05-29 13:54  NZuoF  阅读(1849)  评论(0编辑  收藏  举报