4 Replies Latest reply on Apr 30, 2006 3:34 PM by manishatjboss

    EJB3.0 Standalone client

    manishatjboss

      Hi,

      I can find a lot of instructions on the web on how to write a standalone client for the old style EJBs (where there was an explicitly created home interface). I'm using the new style EJBs (annotations, no home interface required). I'd like to be able to use the EJBs from a standalone client.

      How do I go about doing this? Suggestions will be appreciated. Thanks.

        • 1. Re: EJB3.0 Standalone client
          alesj

          Simple as coding to interfaces.

          public class TestEJB {
          
           public static void main(String[] args) {
           TestHelloBean thb = (TestHelloBean) findJndi("gema/TestHelloBeanImpl/remote");
           if (thb != null)
           System.out.println("thb = " + thb.sayHello("Ales"));
           }
          
           private static Object findJndi(String jndiName) {
           Context context = null;
           try {
           context = new InitialContext();
           return context.lookup(jndiName);
           } catch(Exception e) {
           e.printStackTrace();
           return null;
           } finally {
           try {
           if (context != null)
           context.close();
           } catch (NamingException e) {
           e.printStackTrace();
           }
           }
           }
          
          }
          


          • 2. Re: EJB3.0 Standalone client
            manishatjboss

            thanks for the suggestion. I'm still not entirely clear:

            My interface is:

            @Remote
            public interface Get {
             public int get(int key);
            }
            


            My bean is:
            @Stateless
            public class GetBean implements Get {
            
             public int get(int key) {
             return 42+key;
             }
            
            }
            


            My client is??
            public class Getter {
             @EJB
             private static Get getter;
            
             /**
             * @param args
             */
             public static void main(String[] args) {
             Context ctx = new InitialContext();
             getter = ctx.lookup("???");
             System.out.println("got: " + getter.get(0));
             }
            
            }
            


            Is that correct? If so, how do I figure out what the jndi name will be for the above bean.

            Thanks again.

            • 3. Re: EJB3.0 Standalone client
              leonell

               

              "manishATjboss" wrote:
              thanks for the suggestion. I'm still not entirely clear:

              My interface is:
              @Remote
              public interface Get {
               public int get(int key);
              }
              


              My bean is:
              @Stateless
              public class GetBean implements Get {
              
               public int get(int key) {
               return 42+key;
               }
              
              }
              



              Client ( "blablabla" is name of your target EAR ):
              public class Getter {
               private Get getter;
              
               public Getter()
               {
               try
               {
               Context ctx = new InitialContext(); // used parameters from jndi.properties
               getter = (Get) ctx.lookup("blablabla/GetBean/remote");
               System.out.println("got: " + getter.get(0));
               }
               catch (NamingException e)
               {
               System.err.println(""+e);
               System.exit(0);
               }
               }
              
               public static void main(String[] args)
               {
               new Getter();
               }
              }
              


              And jndi.properties:
              java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
              java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
              java.naming.provider.url=localhost
              


              • 4. Re: EJB3.0 Standalone client
                manishatjboss

                thanks. works just right