0 Replies Latest reply on Apr 5, 2004 5:57 AM by Marco

    SFSB prolem during state replication in HA cluster (4) ?

    Marco Newbie

      <<< the source code and configuration only for stateful EJB >>>

      ========================= TestHAEjbStatefulBean.java ================================================
      package it.ibm.com.it42021.test.ejb;

      import java.net.InetAddress;
      import java.net.UnknownHostException;
      import java.rmi.RemoteException;
      import java.util.Random;

      import javax.ejb.EJBException;
      import javax.ejb.EJBHome;
      import javax.ejb.EJBObject;
      import javax.ejb.Handle;
      import javax.ejb.RemoveException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      /**
      * @author ....
      */
      public class TestHAEjbStatefulBean implements SessionBean, TestHAEjbStatefulRemote {

      public SessionContext ctx = null;
      public String beanName = null;
      public int maxItems = 0;
      public int nextItem = 0;

      public TestHAEjbStatefulBean() {
      super();
      System.out.println("CTOR: TestHAEjbStatefulBean");
      }

      public void ejbActivate() throws EJBException, RemoteException {
      System.out.println("ejbActivate");
      }

      public void ejbPassivate() throws EJBException, RemoteException {
      System.out.println("ejbPassivate");
      }

      public void ejbRemove() throws EJBException, RemoteException {
      System.out.println("ejbRemove: "+beanName);
      }

      public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
      this.ctx = ctx;
      System.out.println("setSessionContext");
      }

      public EJBHome getEJBHome() throws RemoteException {
      EJBHome home = null;

      if ( ctx != null ) {
      home = ctx.getEJBHome();
      }
      System.out.println("getEJBHome: "+home);

      return home;
      }

      public Handle getHandle() throws RemoteException {
      Handle handle = null;

      if ( ctx != null ) {
      EJBObject object = ctx.getEJBObject();
      if ( object != null ) {
      handle = object.getHandle();
      }
      }
      System.out.println("getHandle: "+handle);
      return handle;
      }

      public Object getPrimaryKey() throws RemoteException {
      Object key = null;

      if ( ctx != null ) {
      EJBObject object = ctx.getEJBObject();
      if ( object != null ) {
      key = object.getPrimaryKey();
      }
      }
      System.out.println("getPrimaryKey: "+key);
      return key;
      }

      public boolean isIdentical(EJBObject obj) throws RemoteException {
      System.out.println("isIdentical: "+obj);
      return false;
      }

      public void remove() throws RemoteException, RemoveException {
      System.out.println("remove: "+beanName);
      }

      public void ejbCreate(String beanName) {
      this.beanName = beanName;
      maxItems = new Random().nextInt( 3 ) + 1;
      nextItem = maxItems;
      System.out.println("ejbCreate: bean ["+ beanName + "] with [" +maxItems+"] items to consume.");
      }

      public String whoAreYou(String requester) throws RemoteException {
      String serverName = null;
      String answer = null;

      try {
      serverName = InetAddress.getLocalHost().getHostName();
      } catch (UnknownHostException e) {
      try {
      serverName = InetAddress.getLocalHost().getHostAddress();
      } catch (UnknownHostException e1) {
      serverName = "<unknown host>";
      }
      }
      answer = "[from requester="+requester+"] bean ["+beanName+"] from server ["+serverName+"]";

      System.out.println("whoAreYou: "+beanName+", requester: "+requester);

      return answer;
      }

      public int getMaxItems() throws RemoteException {
      System.out.println("getMaxItems: "+beanName+", maxItems: "+maxItems);
      return maxItems;
      }

      public int getNextItem() throws RemoteException {
      if ( nextItem < 0 ) {
      throw new RemoteException("No more items.");
      }
      System.out.println("getNextItem: "+beanName+", nextItem: "+nextItem);
      return nextItem--;
      }

      // JBoss 3.2.x
      public boolean isModified() {
      System.out.println("isModified: false");
      return false;
      }
      }

      ======================== TestHAEjbStatefulHomeRemote =====================================================
      package it.ibm.com.it42021.test.ejb;

      import java.rmi.RemoteException;

      import javax.ejb.CreateException;
      import javax.ejb.EJBHome;

      /**
      * @author ...
      */
      public interface TestHAEjbStatefulHomeRemote extends EJBHome {

      public TestHAEjbStatefulRemote create(String beanName) throws RemoteException, CreateException;

      }
      ======================== TestHAEjbStatefulRemote =====================================================
      package it.ibm.com.it42021.test.ejb;

      import java.rmi.RemoteException;

      import javax.ejb.EJBObject;

      /**
      * @author ...
      */
      public interface TestHAEjbStatefulRemote extends EJBObject {

      public String whoAreYou(String requester) throws RemoteException;
      public int getMaxItems() throws RemoteException;
      public int getNextItem() throws RemoteException;
      }


      ============================= ServletTestHAJBossEjbStateful ==========================================
      package it.ibm.com.it42021.test.web;


      import it.ibm.com.it42021.test.ejb.TestHAEjbStatefulHomeRemote;
      import it.ibm.com.it42021.test.ejb.TestHAEjbStatefulRemote;
      import it.ibm.com.it42021.test.ejb.TestHAEjbStatelessHomeRemote;

      import java.io.IOException;
      import java.rmi.RemoteException;
      import java.util.Properties;

      import javax.ejb.CreateException;
      import javax.ejb.RemoveException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.servlet.ServletConfig;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      /**
      * @author ...
      */
      public class ServletTestHAJBossEjbStateful extends HttpServlet {

      private static final String _paramNameProviderUrl = "providerUrl";
      private static final String _paramNameResultJsp = "resultJsp";
      private static final String _defaultJBossHAJNDIServer = "localhost:1100";
      private static final String _defaultJBossNCtxFactory = "org.jnp.interfaces.NamingContextFactory";
      private static final String _ejbJndiName = "ejb/TestHAEjbStateful";
      private static final String _requesterName = "requesterName";
      private static final String _beanName = "beanName";
      private static final String _loops = "loops";
      private static final String _answerBean = "answerBean";
      private static String _resultJsp = "result.jsp";

      private InitialContext ctx = null;
      private TestHAEjbStatefulHomeRemote home = null;
      private TestHAEjbStatefulRemote bean = null;

      public ServletTestHAJBossEjbStateful() {
      super();
      }

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      execute(request, response);
      }

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      execute(request, response);
      }

      public void destroy() {
      super.destroy();
      if (ctx != null) {
      try {
      ctx.close();
      } catch (NamingException e) {
      }
      }
      }

      public void init(ServletConfig config) throws ServletException {
      super.init(config);

      String providerUrl = null;
      String param = null;

      param = config.getInitParameter(_paramNameProviderUrl);
      if (param != null) {
      providerUrl = param;
      } else {
      // default HA-JNDI for JBoss
      providerUrl = _defaultJBossHAJNDIServer;
      }

      param = config.getInitParameter(_paramNameResultJsp);
      if (param != null) {
      _resultJsp = param;
      }

      Properties jndiProps = new Properties();
      jndiProps.put(Context.PROVIDER_URL, providerUrl);
      jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, _defaultJBossNCtxFactory);

      System.out.println("Using: " + Context.PROVIDER_URL+" = "+providerUrl);
      System.out.println("Using: " + Context.INITIAL_CONTEXT_FACTORY+" = "+_defaultJBossNCtxFactory);
      System.out.println("Using: result jsp = "+_resultJsp);

      try {
      ctx = new InitialContext(jndiProps);

      // get home here to use balancing
      home = (TestHAEjbStatefulHomeRemote) ctx.lookup(_ejbJndiName);

      } catch (NamingException e) {
      throw new ServletException(e);
      }
      }

      protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String result = null;
      String name = request.getParameter(_requesterName);
      String beanName = request.getParameter(_beanName);
      String strLoops = request.getParameter(_loops);

      System.out.println("name: "+name+" beanName: "+beanName+" strLoops: "+strLoops);

      int loops = 1;
      try {
      loops = Integer.parseInt(strLoops);
      } catch (NumberFormatException e) {
      }

      if (name == null) {
      name = "anonimo";
      }

      if (beanName == null) {
      beanName = "bean anonimo";
      }

      StringBuffer sb = new StringBuffer();
      sb.append("<table border=\"1\">");

      bean = null;
      try {

      for (int i = 0; i < loops; i++) {
      bean = home.create(beanName);
      result = testEjb(name);
      sb.append(""+(i+1)+"");
      sb.append(result);
      sb.append("");
      try {
      bean.remove();
      } catch (RemoveException e2) {
      e2.printStackTrace();
      }
      bean = null;
      }
      } catch (RemoteException e1) {
      sb.append(""+e1.getMessage()+"");
      } catch (CreateException e1) {
      sb.append(""+e1.getMessage()+"");
      } finally {
      if ( bean != null ) {
      try {
      bean.remove();
      } catch (RemoteException e2) {
      e2.printStackTrace();
      } catch (RemoveException e2) {
      e2.printStackTrace();
      }
      }
      }

      sb.append("");

      request.setAttribute(_answerBean, sb.toString());

      request.getRequestDispatcher(_resultJsp).forward(request, response);
      }

      private String testEjb(String name) {
      String result = null;

      try {
      result = bean.whoAreYou(name);
      int max = bean.getMaxItems();
      result += " max items ["+max+"]";
      for (int i = 0; i < max; i++) {
      result += " <"+bean.getNextItem()+">";
      }
      } catch (RemoteException e) {
      result = e.getMessage();
      }

      return result;
      }
      }