Creating an EJB3 (Session Bean) using Netbeans 6.1 and Glassfish

April 29th, 2008  | Tags: , , ,

After the post “Example of EJB3 (Session Bean) using Eclipse Europa and JBoss” I am going to show you how to create the same example but using NetBeans 6.1.

Netbeans 6.x brought nice features and let the web development much easier. You can create many things in a “.VS-style”, in other words, drag-and-drop components on the User Interface. A good example of this it is the Visual Java Server Faces features from NetBeans 6 and the Web Service module.

IMHO, I think the NetBeans 6 is a good tool, however it makes much “magic” and I do not like it. But I have to admit that some stuffs are really cool in Netbeans. I still prefer Eclipse Europa to create Web Application, as well as EJB3 applications, but Netbeans became my Ruby on Rails official IDE and I am also always using Netbeans for Java programs to see how it does work. This topic has the goal to show you how to create the same EJB3 project but in a different platform. After that, you can choose yourself what is the best tool for you (no flamewar, please).

Netbeans brings Glassfish Application Server as its official server. As Glassfish is the the RI (Reference Implementation) from JEE 5, it has some features that you cannot find (yet) in JBoss. An example of this is the @EJB annotation, that using DI approach, you can inject your EJB object in the client directly, thus avoid coupling.

Using Glassfish within Netbeans 6.1

When you are installing Netbeans, you will be asked about extra-programs. One of them is the Glassfish. If you choose the Glassfish in the installation process, it will be configured automatically within NetBean 6.1

To check out the Glassfish installation, open Netbeans and go to the tab Services (left window). Expand Servers option and you should see the Glassfish there.

If you want, you can start the server: right mouse click on Glassfish -> Start.

Creating an Enterprise Application Project

Let’s get started the code itself. The first thing to do is to create the Enterprise Application Project, or simply, EAR.

To do that, back to the tab Projects and right mouse click and select option New Project. On the new screen, select the Category Enterprise and then the project Enterprise Application, click next to go to the next screen.

On the next screen select the Project name and the Project Location. (In my example, I am going to use the name Test), click next again.

On the third screen, select the server (by default Netbeans brings Glasshfish), choose the Java Version (Java EE 5 by default), and select the EJB and Web module.

Note: By default, the EJB module’s name is “EAR name” plus “-ejb“. The Web module’s name is “EAR name” plus “-war“. However you are free to change these names.

When the projects are created, they will be shown on the Projects tab.

Creating our first EJB component (Stateless Session Bean)

On the Projects tab, expand the EJB module (in my case called “Test-ejb”). You will see some options, such as: Enterprise Beans, Configuration Files, Server Resources and so on. We will work around the Enterprise Beans, therefore right mouse click on Enterprise Beans -> New -> Session Bean.

On the screen that will come up, choose the EJB Name, Package, Session Type and Interface. For my example, I am using “TestEJB”,”stateless”,”Stateless”,”Remote”. Click fisnish when your fields are filled.

After the Session Bean is created, you will see the class (and the interface) in the Source Packages section, as well as the Bean in the Enterprise Beans section. In the main tab, the TestEJBBean.java will be opened automatically.

Our Stateless Session Bean was created into the stateless package, but now we have to implement it.

Open it (if it is closed) with double click on the TestEJBBean from Enterprise Beans section (you can open the file directly from the Source Package as well).

In the body of the class there is a comment “ // Add business logic below. (Right-click in editor and choose // “EJB Methods > Add Business Method” or “Web Service > Add Operation”)

So right mouse click in the editor and choose EJB Methods > Add Business Method. A new screen will come up. In your example, we will have only one method (called getMessage()) with String return. Hence, put in the field name getMessage and return type: String.

Done, our first EJB3 Session Bean object has been created. To check out the code it has created, you can open the class TestEJBBean (implementation) and the interface TestEJBRemote (remote interface). Different than Eclipse, where we put ourself the interface and the class name, Netbeans uses the pattern Object Name plus Remote or Local. Of couse you can change it yourself :)

The last thing to do is to implement the business method we just added. It is simple, only return a string message: Hello EJB World. The code looks like below:

package stateless;
 
import javax.ejb.Stateless;
 
@Stateless
public class TestEJBBean implements TestEJBRemote {
    public String getMessage() {
        return "Hello EJB World";
    }
}

Simple, huh?

Using the Web Module as EJB Client

We have already created the Web Module previously when the EAR has been created, so let’s use it as EJB Client.

When the Web module is created, by default Netbeans already creates a file called index.jsp in the Web Pages section. Open this file to add a call to the servlet. The code looks like below:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<h2>Hello World!</h2>
<a href="TestServlet">Click here to call the EJB component</a>

As you can see, we must create a servlet called TestServlet. it will call the EJB component when the link from User Interface is clicked.

To create the servlet itself, right click on Web project (in my case Test-web), New -> Servlet.

On the new screen, select the Class Name and the Package. On next screen, you can keep all options and click on Finish button. The Servlet will be created and its code will appear on the Main tab.

Much easier than JBoss, in Glassfish there is the annotation @EJB. When the container sees this annotation, is inject the component directly in the Servlet, Java Class, Managed Bean, whatever. (In JBoss this annotation has not been implemented yet, so you must use JNDI to call a remote component).

The @EJB annotation gets inside the attribute and can be used directly in the code. The code from Servlet looks like below:

package servlets;
 
import java.io.*;
 
import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;
import stateless.TestEJBRemote;
 
public class TestServlet extends HttpServlet {
 
    //This annotation INJECTS the TestEJBRemove object from EJB into this attribute
    @EJB
    private TestEJBRemote testEJB;
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(testEJB.getMessage());
    }
}

Look at the line #13. It has the @EJB annotation. Also, look at the body of doGet method. No JNDI method was used, no code to call the EJB object was used, thus the code gets much clear than JNDI (Point to Glassfish over JBoss :) ).

Running the code

You can run the application right click on EAR project (in my case Test) and select option Run. Netbeans will do the deploy automatically and when the glassfish is started, the index.jsp page will be opened in your web browser.

After that, click on the LINK and see in the console of Netbeans (tab Glassfish) to see the output.

Note: Although the @EJB annotation is much better than JNDI calls, it only works in the Glassfish server environment. Therefore if you are creating a stand-alone application that calls a remote object, you CANNOT use the @EJB annotation.

Creating a Java Stand-Alone Client

As mentioned above, you cannot use the @EJB annotation out of a Glassfish server environment, therefore in a Stand-Alone application you must use the JNDI. Below following an example of how to call a EJB from a Java Stand-Alone client.

First of all, create a Java Project. New Project -> Java -> Java Application.

In this application, you must add two JARs file, as well as associate your application with the EJB module (thus your client knows about the EJB Interface).

To do that, right click on Java Project, Properties -> Libraries -> Add Project (to add the Test project) and Add JAR/Folder to add the following jar files:

  1. appserv-rt.jar
  2. javaee.jar
  3. appserv-deployment-client.jar (tip from David Etheridge)
  4. appserv-ext.jar (tip from David Etheridge)

Both jar can be located into $GLASSFISH_HOME/lib directory.

By default, the appserv-rt.jar brings a jndi.properties file, but I will show you how to create one. Why? Because when you are running the client in the machine different than the app server, you must override this file.

So create a file called jndi.properties file into the root of the Java project and put the following content:

java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs = com.sun.enterprise.naming
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
#optional.  Defaults to localhost.  Only needed if web server is running
#on a different host than the appserver
org.omg.CORBA.ORBInitialHost = localhost
#optional.  Defaults to 3700.  Only needed if target orb port is not 3700.
org.omg.CORBA.ORBInitialPort = 3700

Now, open the main class and add the following commands:

package testclient;
 
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.InitialContext;
import stateless.TestEJBRemote;
 
public class Main {
 
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.load(new FileInputStream("jndi.properties"));
        InitialContext ctx = new InitialContext(props);
        TestEJBRemote testEJB = (TestEJBRemote) ctx.lookup("stateless.TestEJBRemote");
        System.out.println(testEJB.getMessage());
    }
}

Save it and before run this code, make sure the Glassfish is running. After you run the java class, you should see the EJB message onto the console.

This topic overs here. I hope this topic be useful for anyone, as well as you can choose yourself which environment (Eclipse or Netbeans) is better for you.

On the next topic, I will show you how to configure Glassfish on Eclipse Europa and create a Statefull object.

  1. Thomas
    May 7th, 2008 at 04:57
    #1

    Great article for any person new to NetBeans and EJB to start with. Good work!!!!!

  2. May 7th, 2008 at 11:25
    #2

    Hi,

    My name is Varun Nischal and I’m the NetBeans Docs Contribution Coordinator. Your blog entry would make a fantastic tutorial for our Community Docs wiki (http://wiki.netbeans.org/CommunityDocs).

    Would you be willing to contribute it? If you need any help or have any questions, please contact me at nvarun@netbeans.org

    I look forward to hearing from you.

    Thanks,
    Varun Nischal

  3. May 7th, 2008 at 16:43
    #3

    Excellent! Thanks for doing this…it’s really nice to know that I’m not alone on a lot of these things.

  4. Sadeep
    May 15th, 2008 at 17:41
    #4

    Thanks!

    It gives a very good quick start!
    nice stuff……

  5. pegah
    May 17th, 2008 at 06:05
    #5

    Hi

    It was great. Thanks a lot.

  6. Dhruv
    May 17th, 2008 at 14:38
    #6

    Too helpful topic, especially for a newbie like me who isn’t that familiar with EJBs.

    Thanks very much.

  7. Rolf
    June 1st, 2008 at 20:16
    #7

    Hey,
    First of all, thx for the tutorial.
    On the creation of the servlet I get the compiler error: cannot find symbol TestEJBRemote
    How do I solve this?

  8. KamaL
    June 10th, 2008 at 18:56
    #8

    If you want to invoke EJB method from web module in Net Beans and Glassfish, then please follow the below steps
    1. Right click the web module & go to properties à Libraries à Click Add Project button and add the EJB project
    2. Click the Add Jar /folder button to add C:\Program Files\GlassFish\lib\appserv-rt.jar and C:\Program Files\GlassFish\lib\javaee.jar

  9. Chris
    June 16th, 2008 at 12:46
    #9

    hello, at the java client, I get following error:

    Exception in thread “main” javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory ]

    Do I have to include another library?

  10. Larissa
    July 24th, 2008 at 08:18
    #10

    Good morning! I read your tutorial and executed with great success and interest. I would like to know how to implement servlets and deploy them under tomcat 6.0. Deploying also sessions beans, the latter under Jboss 4.2. I work with Netbeans IDE. thank you

  11. jrjuniorsp
    July 24th, 2008 at 12:14
    #11

    Hi Larissa.
    Thanks for your feedback.
    First of all, Tomcat doesn’t implement the @EJB annotation yet, so you must do a JNDI lookup to reach the EJB components. You can see how to do that in the following article, as well as how to deploy an application into JBoss.
    http://jairrillo.wordpress.com/2008/04/25/example-of-ejb3-session-bean-using-eclipse-europa-and-jboss/

    Although the articles talks about Eclipse, you can use the same example in Netbeans, only change the Netbeans default server to JBoss.

    Check out this blog, there are many articles about Netbeans, Eclipse, JBoss, Tomcat, Glassfish, EBJ and so on :)

  12. nirav
    August 21st, 2008 at 09:13
    #12

    Hi,
    Thanks for such a helpful example.
    But I am not able to execute it.
    I am getting following error
    Exception in thread “main” javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory ]

  13. jrjuniorsp
    August 21st, 2008 at 12:18
    #13

    Hi Nirav,

    Check out the following link: http://forums.sun.com/thread.jspa?messageID=10215367
    Hopefully it will be helpful for you

    Jair

  14. Justin
    August 24th, 2008 at 07:56
    #14

    Hello

    Your article is very helpful to me.

    I try it and it work.

    But i want to launch the client in the machine different than the glassfish app server. On the client machine i edit the jndi.properties like this :

    #———–
    java.naming.factory.initial= com.sun.enterprise.naming.SerialInitContextFactory
    java.naming.factory.url.pkgs=com.sun.enterprise.naming
    java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
    org.omg.CORBA.ORBInitialHost=192.168.0.10
    org.omg.CORBA.ORBInitialPort=3918
    #———–

    when i run client on the same machine than the glassfish app server, it works fine.
    But i run it on the different machine than the glassfish app server (on the same network without firewall), it fails with this error message :

    eclan@eclan-laptop:~/NetBeansProjects/TestClient$ java -jar dist/TestClient.jar
    24 août 2008 09:53:05 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl
    ATTENTION: “IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 3918″
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testclient.Main.main(Main.java:14)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    … 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    … 14 more
    24 août 2008 09:53:05 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl
    ATTENTION: “IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 4038″
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testclient.Main.main(Main.java:14)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    … 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    … 14 more
    24 août 2008 09:53:05 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl
    ATTENTION: “IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 4138″
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testclient.Main.main(Main.java:14)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    … 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    … 14 more
    24 août 2008 09:53:05 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl
    ATTENTION: “IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 3918″
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testclient.Main.main(Main.java:14)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    … 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    … 14 more
    24 août 2008 09:53:05 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl
    ATTENTION: “IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 4038″
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testclient.Main.main(Main.java:14)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    … 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    … 14 more
    24 août 2008 09:53:05 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl
    ATTENTION: “IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: 127.0.1.1; port: 4138″
    org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 201 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2690)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2711)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:181)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:325)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:131)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:220)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:160)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:398)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testclient.Main.main(Main.java:14)
    Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    … 13 more
    Caused by: java.net.ConnectException: Connection refused
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    … 14 more
    eclan@eclan-laptop:~/NetBeansProjects/TestClient$

    THANKS YOU FOR YOUR HELP

  15. Aniceto Perez
    September 11th, 2008 at 14:11
    #15

    I’ve done exactly as you have but lookup(props) always looks up in localhost. All the code I’ve found in internet is the same, but I’ve never get it work.
    I’ve found a code to list the jndi names listed in the context:

    InitialContext id=new InitalContext(props);
    NamingEnumeration ne=ic.list(”");
    while(ne.hasMore()){
    System.out.pprintln(ne.nextElement();
    }

    Listed jndi names are always the local server ones.
    I’ll go on searchig for a response.
    Thanks

  16. fjodir
    September 16th, 2008 at 15:58
    #16

    I created a Netbeans Java Application project, a client and added the .jar files mentioned above. I build the class and get a dist folder containing the folloing:

    myprogram.jar
    lib

    the lib folder contains the .jar files mentioned above, everything seems to work perfect, but if I try to run the application using the folloing command:

    java -classpath lib\appserv-rt.jar;lib\appserv-ext.jar;lib\appserv-deployment-client.jar;lib\javaee.jar -jar myprogram.jar

    I still get the Exception in thread “main” javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory ]

    If I try work around it, adding:

    import com.sun.enterprise.naming.SerialInitContextFactory;

    SerialInitContextFactory fakeContext = new SerialInitContextFactory();
    System.out.println(fakeContext.toString());

    To make sure it is imported during the build, I get the following:

    com.sun.enterprise.naming.SerialInitContextFactory@1b67f74
    Exception in thread “main” javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory
    [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory ]

    It actually do the toString method on the class, stright afterwards, it doesn’t find it, how is this possible?

    Is there a way around this that is not too far fetched?

    Adding the classes as strings in a hashtable makes the building environment in Netbeans ignore it somehow, at least I am not able to add it back using classpath. Is there something wrong in my java execution command?

    Somehow I also get it to work, it did find the SerialInitContextFactory, I don’t remember how I did this now unfortunately, but then I also get the “localhost” problem mentioned above.

    Is there something wrong in the appser-rt.jar that don’t make it overwrite the default jndi.propreties?

    It would be great if someone would look into this…
    Thanks a lot for the great tutorial !!

  17. titus
    November 20th, 2009 at 06:56
    #17

    hi Jair and thanks a lot for your tutorial !
    I tried it and it works !

    then i try some new exercices :

    look :

    I try this minimal piece of code for a standalone client and it works under JDK6 J2EE5 glassfish 2.1 netbeans 6.7 :

    package org.cyber;

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.InitialContext;

    import javax.naming.NamingException;

    /**
    *
    * @author titus
    */
    public class CyberClientStandalone {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    try {
    StatelessSalutationsRemote statelessSalutationsBean;
    InitialContext contexte;
    contexte = new InitialContext();
    statelessSalutationsBean = (StatelessSalutationsRemote) contexte.lookup(”EJBSALUT”);

    System.out.println(statelessSalutationsBean.saluer());
    } catch (NamingException ex) {
    Logger.getLogger(CyberClientStandalone.class.getName()).log(Level.SEVERE, null, ex);
    }

    }
    }

    ===========================
    AND :
    ============================

    package org.cyber;

    import javax.ejb.Stateless;

    /**
    *
    * @author titus
    */
    @Stateless(mappedName=”EJBSALUT”)
    public class StatelessSalutationsBean implements StatelessSalutationsRemote, StatelessSalutationsLocal {

    public String saluer() {
    return “bonjour à tous “;
    }

    }

    ========================
    AND :
    ========================

    package org.cyber;

    import javax.ejb.Remote;

    /**
    *
    * @author titus
    */
    @Remote
    public interface StatelessSalutationsRemote {

    String saluer();

    }

    ================================

    warnig : EJB and standalone client are in the same package, on a same computer
    without JNDI

    I don’t really understand why it works ? :-)
    Have you an idea ?

  18. stephane
    December 11th, 2009 at 13:42
    #18

    Hello,

    Good tutorial, thanks !
    I am trying to realise your example.
    The client running into the EJB container run nice. But the standalone version of this client refused to run !
    I am obtaint this exception on the ctx.lookup(”stateless.TestEJBRemote”); instruction :
    Exception in thread “main” javax.naming.CommunicationException: Can’t find SerialContextProvider [Root exception is org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 208 completed: Maybe]
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:164)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:409)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at testmyfirstejb.Main.main(Main.java:15)
    Caused by: org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 208 completed: Maybe
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectionAbort(ORBUtilSystemException.java:2862)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectionAbort(ORBUtilSystemException.java:2880)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doOptimizedReadStrategy(SocketOrChannelConnectionImpl.java:1788)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(SocketOrChannelConnectionImpl.java:1263)
    at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:555)
    Caused by: org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 211 completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.ioexceptionWhenReadingConnection(ORBUtilSystemException.java:2946)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.ioexceptionWhenReadingConnection(ORBUtilSystemException.java:2965)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.nonBlockingRead(SocketOrChannelConnectionImpl.java:2000)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doOptimizedReadStrategy(SocketOrChannelConnectionImpl.java:1713)
    … 2 more
    Caused by: java.io.IOException: End-of-stream
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.nonBlockingRead(SocketOrChannelConnectionImpl.java:1989)
    … 3 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    I d’ont understand why this client d’ont run correctly.

    Have an idea ?

    Thanks.

  19. fredit
    January 24th, 2010 at 15:21
    #19

    I tried step by step tutorial by my netbeans does not create the
    string “Click here to call the EJB component” in index.jsp
    nor it injects EJB using @. What possiblew cuseses could it be?

TOP