Thursday, February 26, 2009

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 using JBoss and NetBeans.
Getting access to Naming Service of JBoss from remote client
Using appropriate .jar file of JBoss to access an EJB deployed in JBoss from a remote client.
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.
First configure NetBeans to use JBoss as application server using Tools ->Server Manager option of NetBeans.
Create a new project using File->New Project
Select Enterprise as category and EJB Module as type of project
Enter HelloEJB as the name of the project
Select Stateless and type of EJB and Remote as the interface
NetBeans provides - HelloRemote.java and HelloBean.java
Go to HelloBean.java. Invoke popup menu (right click) and select EJB Methods->Add Business Method
Enter sayHello as name of the method and String as return type
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";
}
}
Go to Projects window using Window->Projects
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:

Create a new project using File->New Project
Select General in category and Java Application as project type
Give project name as SwingClient
Select the project (SwingClient) in Projects window. Go to libraries node, right click and select Add Jar/Folder .
Add the following libraries (.jar files) to SwingClient project.
HelloEjb.jar - Jar file of HelloEJB application. This is found in dist directory of the project
jboss-ejb3-client.jar, jboss-aop-jdk50-client.jar, jboss-aspect-jdk50-client.jar and jbossall-client.jar - all these .jar files are found in client folder of JBoss installation directory( For ex, d:\jboss)
Add a Java class (HelloClient) to project using File->New File, select Java Classes in categories and Java Class as File Type
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);
}
}
Run HelloClient class by selecting Run File option from popup menu (right click).
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.

No comments:

Post a Comment