0 Replies Latest reply on Jan 4, 2005 9:54 AM by Robert Costanzo

    Flushing the cache for the new cmp2.x jdbc2 in JBoss 3.2.6

    Robert Costanzo Newbie

      Tried to use RepositorySelector on MDB

      on my MDB i put this

      public void setMessageDrivenContext(MessageDrivenContext _context)throws EJBException {
       context = _context;
      
       //initialized my own RepositorySelector class
       MyBeanRepositorySelector.init(context);
      
       log = Logger.getLogger(CrmIntergatorBean.class);
      
      
       // For debugging purposes
       log(Priority.DEBUG, "CrmIntergatorBean.setMessageDrivenContext()");
      
       }



      and created a class file
      import java.io.InputStream;
      import java.util.HashMap;
      import java.util.Map;
      
      import javax.ejb.EJBException;
      import javax.ejb.MessageDrivenContext;
      import javax.xml.parsers.DocumentBuilderFactory;
      
      import org.apache.log4j.Hierarchy;
      import org.apache.log4j.Level;
      import org.apache.log4j.LogManager;
      import org.apache.log4j.spi.LoggerRepository;
      import org.apache.log4j.spi.RepositorySelector;
      import org.apache.log4j.spi.RootCategory;
      import org.apache.log4j.xml.DOMConfigurator;
      import org.w3c.dom.Document;
      
      /**
       * @author jmer
       *
       */
      public class MyBeanRepositorySelector implements RepositorySelector{
      
       private static boolean initialized = false;
       private static Object guard = new Object();
      
       private static Map repositories = new HashMap();
       private static LoggerRepository defaultRepository;
      
       private MyBeanRepositorySelector() {
       }
      
       /**
       * Register MDB with repository selector
       * @param config
       * @throws EJBException
       */
      
       public static synchronized void init(MessageDrivenContext config) throws EJBException{
       if( !initialized ) // set the global RepositorySelector
       {
       defaultRepository = LogManager.getLoggerRepository();
       RepositorySelector theSelector = new MyBeanRepositorySelector();
       LogManager.setRepositorySelector(theSelector, guard);
       initialized = true;
       }
      
       Hierarchy hierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
       loadLog4JConfig(config, hierarchy);
       ClassLoader loader = Thread.currentThread().getContextClassLoader();
       repositories.put(loader, hierarchy);
       }
      
      
       /**
       * Load the log4jxml
       * @param config
       * @param hierarchy
       * @throws EJBException
       */
       private static void loadLog4JConfig(MessageDrivenContext config, Hierarchy hierarchy) throws EJBException {
       try {
       String log4jFile = "/META-INF/log4j.xml";
       InputStream log4JConfig = config.getClass().getResourceAsStream(log4jFile);
       Document doc = DocumentBuilderFactory.newInstance()
       .newDocumentBuilder()
       .parse(log4JConfig);
       DOMConfigurator conf = new DOMConfigurator();
       conf.doConfigure(doc.getDocumentElement(), hierarchy);
       } catch (Exception e) {
       e.printStackTrace();
       throw new EJBException(e);
       }
       }
      
      
       /* (non-Javadoc)
       * @see org.apache.log4j.spi.RepositorySelector#getLoggerRepository()
       */
       public LoggerRepository getLoggerRepository() {
       ClassLoader loader = Thread.currentThread().getContextClassLoader();
       LoggerRepository repository = (LoggerRepository)repositories.get(loader);
      
       if (repository == null) {
       return defaultRepository;
       } else {
       return repository;
       }
       }
      
      }
      
      


      I also added log4j.xml on package to META-INF, When i deployed and test the code i got this error



      10:53:16,062 ERROR [LogInterceptor] TransactionRolledbackLocalException in method: public abstract void javax.jms.MessageListener.onMessage(javax.jms.Message), causedBy:
      java.lang.IllegalArgumentException: Attempted to reset the LoggerFactory without possessing the guard.
      at org.apache.log4j.LogManager.setRepositorySelector(LogManager.java:144)
      at ph.net.mdi.crm.util.MyBeanRepositorySelector.init(MyBeanRepositorySelector.java:54)
      at ph.net.mdi.crm.ejb.CrmIntergatorBean.setMessageDrivenContext(CrmIntergatorBean.java:99)
      at org.jboss.ejb.MessageDrivenEnterpriseContext.<init>(MessageDrivenEnterpriseContext.java:61)
      at org.jboss.ejb.plugins.MessageDrivenInstancePool.create(MessageDrivenInstancePool.java:36)
      at org.jboss.ejb.plugins.AbstractInstancePool.get(AbstractInstancePool.java:168)
      at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:48)
      at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
      at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:315)
      at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:148)
      at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:90)
      at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
      at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
      at org.jboss.ejb.MessageDrivenContainer.internalInvoke(MessageDrivenContainer.java:372)
      at org.jboss.ejb.Container.invoke(Container.java:723)
      at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:914)
      at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1208)
      at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:276)
      at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:871)
      at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:159)
      at org.jboss.mq.SpySession.run(SpySession.java:347)
      at org.jboss.jms.asf.StdServerSession.run0(StdServerSession.java:200)
      at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:180)
      at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:743)
      at java.lang.Thread.run(Thread.java:536)



      How do i fix it?