Exemple #1
0
  public static void main(String[] args) {
    try {
      Properties p = new Properties();

      p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.PROVIDER_URL, "10.10.10.13:1100,10.10.10.14:1100");
      // p.put(Context.PROVIDER_URL, "localhost:1100");
      p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      InitialContext ctx = new InitialContext(p);

      StatelessSessionHome statelessSessionHome =
          (StatelessSessionHome) ctx.lookup("nextgen.StatelessSession");
      EnterpriseEntityHome cmpHome = (EnterpriseEntityHome) ctx.lookup("nextgen.EnterpriseEntity");
      StatelessSession statelessSession = statelessSessionHome.create();
      EnterpriseEntity cmp = null;
      try {
        cmp = cmpHome.findByPrimaryKey("bill");
      } catch (Exception ex) {
        cmp = cmpHome.create("bill");
      }
      int count = 0;
      while (true) {
        System.out.println(statelessSession.callBusinessMethodB());
        try {
          cmp.setOtherField(count++);
        } catch (Exception ex) {
          System.out.println("exception, trying to create it: " + ex);
          cmp = cmpHome.create("bill");
          cmp.setOtherField(count++);
        }
        System.out.println("Entity: " + cmp.getOtherField());
        Thread.sleep(2000);
      }
    } catch (NamingException nex) {
      if (nex.getRootCause() != null) {
        nex.getRootCause().printStackTrace();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public void testStatelessBean() throws Exception {

    getLog().debug(++test + "- " + "Trying the context...");

    Context ctx = new InitialContext();
    getLog().debug("OK");

    /// *
    getLog().debug("");
    getLog().debug("Test Stateless Bean");
    getLog().debug("===================");
    getLog().debug("");
    getLog().debug(++test + "- " + "Looking up the home nextgen.StatelessSession...");
    StatelessSessionHome statelessSessionHome =
        (StatelessSessionHome) ctx.lookup("nextgen.StatelessSession");
    if (statelessSessionHome != null) getLog().debug("ok");
    getLog().debug(++test + "- " + "Calling create on StatelessSessionHome...");
    StatelessSession statelessSession = statelessSessionHome.create();
    assertTrue("statelessSessionHome.create() != null", statelessSession != null);
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling getEJBHome() on StatelessSession...");
    assertTrue("statelessSession.getEJBHome() != null", statelessSession.getEJBHome() != null);
    getLog().debug("ok");

    getLog().debug(++test + "- " + "Calling Business Method A on StatelessSession... ");
    statelessSession.callBusinessMethodA();
    getLog().debug("ok");
    getLog().debug(++test + "- " + "Calling Business Method B on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodB());
    getLog().debug(++test + "- " + "Calling Business Method B(String) on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodB("of wisdom"));
    getLog().debug(++test + "- " + "Calling Business Method C on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodC());
    getLog().debug(++test + "- " + "Calling Business Method D on StatelessSession... ");
    try {
      statelessSession.callBusinessMethodD();
      fail("callBusinessMethodD, no exception was thrown");
    } catch (BusinessMethodException e) {
      getLog().debug("Caught BusinessMethodException OK");
    }
    getLog()
        .debug(++test + "- " + "Calling Business Method E (getEJBObject) on StatelessSession... ");
    getLog().debug(statelessSession.callBusinessMethodE());

    getLog().debug(++test + "- " + "Calling testClassLoading on StatelessSession... ");
    statelessSession.testClassLoading();
    getLog().debug("OK");

    getLog().debug("***Testing the various local Object class calls");
    getLog().debug(++test + "- " + "toString ... " + statelessSession.toString());

    getLog().debug(++test + "- " + "hashCode ... " + statelessSession.hashCode());

    getLog()
        .debug(
            ++test
                + "- "
                + "equals (same object) ... "
                + statelessSession.equals(statelessSession));

    getLog()
        .debug(
            ++test
                + "- "
                + "equals (another object) (true under same home)... "
                + statelessSession.equals(statelessSessionHome.create()));

    getLog().debug("***Testing the various local EJBObject class calls");

    getLog().debug(++test + "- " + "Get Handle ... ");
    Handle statelessHandle = statelessSession.getHandle();
    assertTrue("statelessHandle != null", statelessHandle != null);
    getLog().debug("OK");
    getLog().debug(++test + "- " + "Serialize handle and deserialize..");
    MarshalledObject mo = new MarshalledObject(statelessHandle);
    Handle handle2 = (Handle) mo.get();
    StatelessSession statelessSession2 = (StatelessSession) handle2.getEJBObject();
    assertTrue("statelessSession2 != null", statelessSession2 != null);
    getLog().debug("OK");
    getLog().debug(++test + "- " + "Calling businessMethodB on it...");
    getLog().debug(statelessSession2.callBusinessMethodB());
    getLog()
        .debug(
            ++test
                + "- "
                + "They should be identical..."
                + statelessSession.isIdentical(statelessSession2));
    getLog().debug("***Testing the various local EJBHome class calls");
    getLog().debug(++test + "- " + "Getting the metaData...");
    EJBMetaData statelessMetaData = statelessSessionHome.getEJBMetaData();
    assertTrue("statelessMetaData != null", statelessMetaData != null);
    getLog().debug("OK");
    getLog()
        .debug(++test + "- " + "Is stateless Session? " + statelessMetaData.isStatelessSession());
    getLog()
        .debug(
            ++test + "- " + "The remote class is " + statelessMetaData.getRemoteInterfaceClass());

    getLog().debug("");
    getLog().debug(++test + "- " + "Calling StatelessSession.remove()...");
    statelessSession.remove();
    getLog().debug("ok");
  }