Remote Access to EJB on GlassFish
Enterprise Java Beans (EJB) is a server-side architecture part of Java EE. Its specification contains two kinds of client views: remote and local ones. In one case, my Java app may require the session and entity beans with local home and component interfaces and in another case - with remote ones.
Let’s find out what are the differences between these interfaces and which of them I need to choose to work with.
If I am sure that other clients and EJBs will access my bean via a single JVM, I can use a local client view. It is also suitable for the case my beans are associated one with another. Such an access is performed within direct calling of methods, not through the remote method invocation (RMI).
For the case my client is placed on the other JVM, i.e. I would like to use my bean in the distributed environment, then there is a necessity to work with the remote client view. All the methods’ calls from the remote interfaces will be handled by it. It is also preferably to use RCV while working with parameters, passed by value between the client application and bean.
Let’s examine how to deploy Java Bean to the CloudJiffyhosting platform and use EJB remote client to work with it.
A. Create the environment
1. Log in to my CloudJiffyaccount within CloudJiffy Manager2. Click the Create environment button in order to open the environment topology wizard. Pick up GlassFish as my application server and set cloudlet limits for it accordingly to the resources consumption needs of my Java enterprise application. Enable the Public IP for GlassFish, type the name for my environment and click Create.
Wait about a minute for my environment to be created.
< im 2 >
B. Create the application
1. At the very beginning, create a new directory to place my EJB and client application files in.
2. Then create my Session Bean. It will be used to access the deployed application via the remote client app through executing business tasks inside the server.
1
2
3
4
5
6
7
8
9
10
11
12
|
package com; import javax.ejb.Stateless; @Stateless public class EJBTest implements EJBTestRemote { @Override public String getName(String name) { return "name is: " + name; } } |
1
2
3
4
5
6
7
|
package com; import javax.ejb.Remote; public interface EJBTestRemote { public String getName (String name); } |
5. The following code is an example of remote client application, which is used to access the bean. It performs the remote connection to my EJB through the Public IP of GlassFish application server and calls the getName() method, which, in its turn, returns the data to the client.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package ejbclient; import com.EJBTestRemote; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.InitialContext; import javax.naming.NamingException; public class Main { private static InitialContext ic; //private String host="",port=""; public void loadProperties(String h, String p) { try { Properties props = new Properties(); System.out.println( "h: " + h + " p: " + p); props.setProperty( "java.naming.factory.initial" , "com.sun.enterprise.naming.SerialInitContextFactory" ); props.setProperty( "java.naming.factory.url.pkgs" , "com.sun.enterprise.naming" ); props.setProperty( "java.naming.factory.state" , "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl" ); props.setProperty( "org.omg.CORBA.ORBInitialHost" , h); props.setProperty( "org.omg.CORBA.ORBInitialPort" , p); ic = new InitialContext(props); } catch (NamingException ex) { Logger.getLogger(Main. class .getName()).log(Level.SEVERE, null , ex); } } public static void main(String a[]) { try { new Main().loadProperties( "{GlassFish_Public_IP}" , "23700" ); EJBTestRemote etr = (EJBTestRemote) ic.lookup( "com.EJBTestRemote" ); System.out.println(etr.getName( "Jelastic" )); } catch (NamingException ex) { } } } |
Note: All the numbers of ports begin with additional 2 digit for the reason the CloudJiffyplatform works with gfcluster only.
As an example of .ear file I can use this package.
C. Deploy the application
1. Navigate to the CloudJiffydashboard, open the Deployment manager and upload the created .ear within it.Hope this tutorial on the basics of remote interfaces usage helped me. Enjoy!
2. Deploy the uploaded package to the GlassFish environment, created in the step A of this instruction.
3. Finally, run my application (press Open in browser button next to the environment) and check the results.
Hope this tutorial on the basics of remote interfaces usage helped me. Enjoy!