Package org.jboss.ejb3.stateful

Examples of org.jboss.ejb3.stateful.StatefulBeanContext


      return create(null, null);
   }

   public StatefulBeanContext create(Class<?>[] initTypes, Object[] initValues)
   {
      StatefulBeanContext ctx = null;
      try
      {
         ctx = container.create(initTypes, initValues);
         if (log.isTraceEnabled())
         {
            log.trace("Caching context " + ctx.getId() + " of type " + ctx.getClass());
         }
         synchronized (cacheMap)
         {
            cacheMap.put(ctx.getId(), ctx);
            ctx.setInUse(true);
            ctx.lastUsed = System.currentTimeMillis();
         }
         ++createCount;
      }
      catch (EJBException e)
View Full Code Here


      return get(key, true);
   }
  
   public StatefulBeanContext get(final Object key, boolean markInUse) throws EJBException
   {
      StatefulBeanContext entry = null;
      synchronized (cacheMap)
      {
         entry = cacheMap.get(key);
      }
      if(entry == null)
      {
         // TODO: optimize
         synchronized (cacheMap)
         {
            entry = cacheMap.get(key);
            if(entry == null)
            {
               Iterator<StatefulBeanContext> i = passivationQueue.iterator();
               while(i.hasNext())
               {
                  StatefulBeanContext ctx = i.next();
                  if(ctx.getId().equals(key))
                  {
                     boolean passivationCanceled = passivationQueue.remove(ctx);
                     if(passivationCanceled)
                     {
                        entry = ctx;
                        cacheMap.put(key, entry);
                     }
                     break;
                  }
               }
            }
         }
      }
      if (entry == null)
      {
         // JBPAPP-7523: create a task for activation while holding the pm lock,
         // then execute said task outside the lock
         FutureTask<StatefulBeanContext> activation;
         final boolean executeActivation;
         synchronized(pm)
         {
            activation = activations.get(key);
            if (activation == null)
            {
               activation = new FutureTask<StatefulBeanContext>(new Callable<StatefulBeanContext>()
               {
                  @Override
                  public StatefulBeanContext call() throws Exception {
                      StatefulBeanContext entry;
                      // has someone already activated it?
                      synchronized (cacheMap)
                      {
                         entry = cacheMap.get(key);
                      }
                      if(entry == null)
                      {
                         entry = pm.activateSession(key);
                         if (entry == null)
                         {
                            throw new NoSuchEJBException("Could not find stateful bean: " + key);
                         }
                         --passivatedCount;

                         // We cache the entry even if we will throw an exception below
                         // as we may still need it for its children and XPC references
                         if (log.isTraceEnabled())
                         {
                            log.trace("Caching activated context " + entry.getId() + " of type " + entry.getClass());
                         }

                         synchronized (cacheMap)
                         {
                            cacheMap.put(key, entry);
                            // remove the activation, worst case a new task will be constructed and
                            // the check whether someone has already activated it will prevent actual activation.
                            activations.remove(key);
                         }
                      }
                      return entry;
                  }
               });
               activations.put(key, activation);
               executeActivation = true;
            }
            else
               executeActivation = false;
         }
         if (executeActivation)
            activation.run();
         try {
            entry = activation.get();
         }
         catch (InterruptedException e)
         {
            throw new EJBException(e);
         }
         catch (ExecutionException e)
         {
            if (e.getCause() instanceof RuntimeException)
               throw (RuntimeException) e.getCause();
            throw (EJBException) new EJBException().initCause(e.getCause());
         }
      }
     
      // Now we know entry isn't null
      if (markInUse)
      {
         if (entry.isRemoved())
         {
            throw new NoSuchEJBException("Could not find stateful bean: " + key +
                                         " (bean was marked as removed");
         }     
     
         entry.setInUse(true);
         entry.lastUsed = System.currentTimeMillis();
      }
     
      return entry;
   }
View Full Code Here

      }
      // don't directly use the cacheMap to get the
      // object from the key. Instead, use the get() method
      // which will even activate any sessions which have been
      // passivated (see https://jira.jboss.org/jira/browse/EJBTHREE-2030)
      StatefulBeanContext ctx = this.get(key);
      if(ctx == null)
         throw new NoSuchEJBException("Could not find Stateful bean: " + key);
      if (!ctx.isRemoved())
         container.destroy(ctx);
     
      ++removeCount;
     
      if (ctx.getCanRemoveFromCache())
      {
         if(it == null)
         {
            synchronized (cacheMap)
            {
View Full Code Here

      public boolean removeEldestEntry(Entry<Object, StatefulBeanContext> entry)
      {
         boolean removeIt = size() > maxSize;
         if (removeIt)
         {
            StatefulBeanContext centry = (StatefulBeanContext) entry.getValue();
            synchronized (centry)
            {
               if (centry.getCanPassivate())
               {
                  passivate(centry);
                  // its ok to evict because bean will be passivated.
               }
               else
               {
                  centry.markedForPassivation = true;
                 
                  if (!centry.isInUse())
                  {
                     // Can't passivate but not in use means a child bean is
                     // in use.
                     // It's not ok to evict because bean will not be passivated
                     removeIt = false;
View Full Code Here

                  
                  Iterator<Entry<Object, StatefulBeanContext>> it = cacheMap.entrySet().iterator();
                  while (it.hasNext())
                  {
                     Entry<Object, StatefulBeanContext> entry = it.next();
                     StatefulBeanContext centry = entry.getValue();
                     if (now - centry.lastUsed >= removalTimeout * 1000)
                     {
                        synchronized (centry)
                        {
                           remove(centry.getId(), it);
                        }
                     }
                  }                 
               }
              
               List<StatefulBeanContext> beans = pm.getPassivatedBeans()
               Iterator<StatefulBeanContext> it = beans.iterator();
               while (it.hasNext())
               {      
                  StatefulBeanContext centry = it.next();
                  if (now - centry.lastUsed >= removalTimeout * 1000)
                  {
                     get(centry.getId(), false);
                     remove(centry.getId());
                  }
               }
              
               // Invoke post-removal callback
               this.postRemoval();
View Full Code Here

                  Iterator<Entry<Object, StatefulBeanContext>> it = cacheMap.entrySet().iterator();
                  long now = System.currentTimeMillis();
                  while (it.hasNext())
                  {
                     Entry<Object, StatefulBeanContext> entry = it.next();
                     StatefulBeanContext centry = entry.getValue();
                     if (now - centry.lastUsed >= sessionTimeout * 1000)
                     {
                        synchronized (centry)
                        {                    
                           if (centry.getCanPassivate())
                           {
                              if (!centry.getCanRemoveFromCache())
                              {
                                 passivationQueue.add(centry);
                              }
                              else if (trace)
                              {
                                 log.trace("Removing " + entry.getKey() + " from cache");
                              }
                           }
                           else
                           {
                              centry.markedForPassivation = true;                             
                              assert centry.isInUse() : centry + " is not in use, and thus will never be passivated";
                           }
                           // its ok to evict because it will be passivated
                           // or we determined above that we can remove it
                           it.remove();
                        }
                     }
                     else if (trace)
                     {
                        log.trace("Not passivating; id=" + centry.getId() +
                              " only inactive " + Math.max(0, now - centry.lastUsed) + " ms");
                     }
                  }                 
               }
              
               prePassivationCompleted();
              
               StatefulBeanContext ctx;
               while ((ctx = passivationQueue.poll()) != null)
               { 
                  passivate(ctx);
               }
              
View Full Code Here

         container.setJaccContextId("none");
         container.start();
        
         Object id = container.createSession();
        
         StatefulBeanContext ctx = container.getCache().get(id, false);
        
         System.out.println("inUse = " + ctx.isInUse());
         MockBean bean = (MockBean) ctx.getInstance();
         System.out.println(bean.ctx);
         ctx.setInUse(false);
         ctx = null;
        
         synchronized (MockBean.notification)
         {
            MockBean.notification.wait(5000);
         }
         Thread.sleep(500);
        
         ctx = container.getCache().get(id, false);
         bean = (MockBean) ctx.getInstance();
        
         String a = ctx.getEJBContext().toString();
         String b = bean.ctx.toString();
         System.out.println(ctx.getEJBContext());
         System.out.println(bean.ctx);
         assertTrue(a.regionMatches(a.indexOf('{'), b, b.indexOf('{'), a.length() - a.indexOf('{')));
      }
      finally
      {
View Full Code Here

    */
   @Test
   public void testRemovalAfterRemovalTimeout() throws Exception
   {
      // create a session
      StatefulBeanContext sfsbContext = cache.create();
      Object sessionId = sfsbContext.getId();
      logger.info("Created StatefulBeanContext with session id " + sessionId);
      // mark it as *not in use* so that it can be passivated
      sfsbContext.setInUse(true);
      // set last used to now
      sfsbContext.lastUsed = new Date().getTime();
     
      // wait for 3 (or more seconds for removal to happen)
      // The SimpleSFSB is configured for a removalTimeoutSeconds of 1 seconds (so that it will be
      // removed after 1 seconds, idleTimeout is set to 10)
      logger.info("Sleeping for 3 seconds to allow removal thread to remove the bean context with id "
            + sessionId);
      Thread.sleep(3000);
     
      if(! sfsbContext.isRemoved())
      {
        Assert.fail("SFSB context should be removed now EJBTHREE-2275");
          
        logger.info("Clean up - trying to remove session " + sessionId + " from cache");
        cache.remove(sessionId);
View Full Code Here

      if (debug)
      {
         log.debug("Reading session state from: " + file);
      }

      StatefulBeanContext bean = null;
      try
      {
         FileInputStream fis = FISAction.open(file);
         // todo need to rewrite SessionObjectInputStream to support EJB3 classes
         ObjectInputStream in;

         in = new JBossObjectInputStream(new BufferedInputStream(fis));
         try
         {
            bean = (StatefulBeanContext) in.readObject();
         }
         finally
         {
            fis.close();
            in.close();
         }
      }
      catch (EJBException e)
      {
         throw e;
      }
      catch (Exception e)
      {
         throw new EJBException("Could not activate; failed to " +
                                "restore state", e);
      }

      removePassivated(id);

      bean.postActivate();
      return bean;
   }
View Full Code Here

            FileInputStream fis = FISAction.open(file);
  
            in = new JBossObjectInputStream(new BufferedInputStream(fis));
            try
            {
               StatefulBeanContext bean = (StatefulBeanContext) in.readObject();
               beans.add(bean);
            }
            finally
            {
               fis.close();
View Full Code Here

TOP

Related Classes of org.jboss.ejb3.stateful.StatefulBeanContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.