3 Replies Latest reply on Sep 28, 2005 10:07 AM by Michael Small

    Access session bean service in another session bean

    Martin Ganserer Novice

      Hello,

      I have got two stateless session beans: DAO and a LogService.
      How can I call a method from my LogService in my DAO?

      Thank you!

        • 1. Re: Access session bean service in another session bean
          Michael Small Newbie

          You inject the LogService into the Dao service. For example:

          
          @Local
          public interface LogService {
           ...
          }
          
          @Stateless
          public class LogServiceBean extends LogService {
           ...
          }
          
          @Local
          public interface Dao {
           ...
          }
          
          @Stateless
          public class DaoBean extends Dao {
          
           private LogService logService;
          
           @EJB(businessInterface=LogService.class)
           public void setLogService(LogService logService) {
           this.logService = logService;
           }
          
          }
          


          The @EJB annotation signals to the container that it should inject the LogService session bean when the DaoBean is initialized (i.e. before any calls to any of its exposed methods).

          • 2. Re: Access session bean service in another session bean
            Martin Ganserer Novice

            Thank you michael for your detailed explanation! It works!
            I did it with

            @PostConstruct
             public void init()
             {
             try
             {
             ctx = new InitialContext();
             logService = (LogService) ctx.lookup(LogService.class.getName());
             }
             catch(Exception ex)
             {
             System.out.println("Error while initializing log service!");
             }
            
             }

            But your solution is much more convenient!


            But I have on further question!
            Now I am able to use the business methods of my log service. For debug purpose I can save log messages without any problem.
            But I want to log error messages too! What I tried now is to call the log method in the catch-block:





            try
             {
             em.persist(country);
            
             if(LogService.ACTIVE_LOG_LEVEL == LogService.LOG_LEVEL_DEBUG)
             logService.log
             (
             this.getClass().getName(),
             Thread.currentThread().getStackTrace(),
             LogService.LOG_STATE_INFO,
             "Country object with id " + country.getId() + " created!"
             );
            
             }
             catch (Exception e)
             {
             logService.log
             (
             this.getClass().getName(),
             Thread.currentThread().getStackTrace(),
             LogService.LOG_LEVEL_ERROR,
             e.getMessage()
             );
             throw new GenericBusinessException(e.getMessage(), e.getCause());
             }
             return country;
            
            


            But if an error occurs the log message will not be saved. I guess the reason for this is the container manged transaction mechanism I use. I already tried to use a EntityManagerFactory. But I read that this is not supported in JBOSS.

            Any comments?

            • 3. Re: Access session bean service in another session bean
              Michael Small Newbie

              Couple of comments:

              1) Is your logging service using the em for storag?. If so, when the initial failures occurs (i.e. em.persist(...)), it is safer to assume that something is serious wrong with the EntityManager and subsequent calls to it will fail.
              2) Why not use a standard logging API such as log4j? log4j can be configured to write logging to a file (which wouldn't be effected by datasource problems) and can be later moved into a database (log4j provides file to database converters I believe).