Accessing EJB In JBoss From Swing Client

This articles shows how to access a simple stateless session bean in EJB 3.0 from a Swing client. Though it sounds like a simple process, it needs good number of steps. You need to use good number or .jar files and properties to access ejb 3.0 from a remote client.

At the end of this article you will have learnt how to do the following:

Creating a simple stateless session bean in JBoss with NetBeans

Follow the steps and code given below to create a stateless session bean with a single method. I use JBoss as the EJB Container and NetBeans IDE.
  1. First configure NetBeans to use JBoss as application server using Tools ->Server Manager option of NetBeans.
  2. Create a new project using File->New Project
  3. Select Enterprise as category and EJB Module as type of project
  4. Enter HelloEJB as the name of the project
  5. Select Stateless and type of EJB and Remote as the interface
  6. NetBeans provides - HelloRemote.java and HelloBean.java
  7. Go to HelloBean.java. Invoke popup menu (right click) and select EJB Methods->Add Business Method
  8. Enter sayHello as name of the method and String as return type
  9. Write the code shown below. At the end of the process HelloRemote.java and HelloBean.java should be as shown below.
    // HelloRemote.java
    package st;
    import javax.ejb.Remote;
    
    @Remote
    public interface HelloRemote {
        String sayHello();
    }    
    
    // HelloBean.java
    package st;
    import javax.ejb.Stateless;
    
    @Stateless
    public class HelloBean implements HelloRemote {
        
        public HelloBean() {
        }
    
        public String sayHello() {
            return "Hello";
        }
    }    
  10. Go to Projects window using Window->Projects
  11. Right click on project name to invoke popup menu and select Deploy Project option to deploy project

Creating Swing Client

Now, let us create a frame-based swing application to access EJB. This swing application runs on its own and not in JBoss. So, it is called as remote client to EJB. Client project needs to have access to a couple of  .jar files to access to JBoss and EJB deployed in JBoss.

Take the following steps related to client:

  1. Create a new project using File->New Project
  2. Select General in category and Java Application as project type
  3. Give project name as SwingClient
  4. Select the project (SwingClient)  in Projects window. Go to libraries node, right click and select Add Jar/Folder .
  5. Add the following libraries (.jar files) to SwingClient project.
  6. Add a Java class (HelloClient) to project using File->New File, select Java Classes in categories and Java Class as File Type
  7. Write the following code in HelloClient.java
    import java.util.Properties;
    import javax.naming.InitialContext;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import st.HelloRemote;
    
    public class HelloClient extends JFrame {
        public HelloClient() {
            super("Hello Client");
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            JButton b1 = new JButton("Access EJB");
    
            getContentPane().add(b1, BorderLayout.PAGE_END);
            
            b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    String msg = "";
                    try {
                        // Access JNDI Initial Context.
                        Properties p = new Properties();
                        p.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
                        p.put("java.naming.provider.url","jnp://localhost:1099");
                        p.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
                        InitialContext ctx = new InitialContext(p);
                        // Change jndi name according to your server and ejb
                        HelloRemote remote = (HelloRemote) ctx.lookup("HelloBean/remote");
                        msg = "Message From EJB --> " + remote.sayHello();
                    } 
                    catch(Exception ex){
                        msg = "Error --> " +  ex.getCause().toString();
                    }
                    JOptionPane.showMessageDialog(HelloClient.this,msg,"Message", JOptionPane.INFORMATION_MESSAGE);
                }
            });
            setSize(200,200);
        } //
        public static void main(String args[]) {
           new HelloClient().setVisible(true);
        } 
    }
    
  8. Run HelloClient class by selecting Run File option from popup menu (right click).
  9. Click on Access EJB button and you must see a message dialog with message coming from ejb - Hello.
I hope  this article helps you to understand how to access an EJB deployed in JBoss from a remote client. Remote client may be a swing application, a console application or a web application.

Keep Learning.

Srikanth .