glassfish
  1. glassfish
  2. GLASSFISH-20984

Unable to inject JMS resources in a bean defined in a dependency jar

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: 4.0
    • Fix Version/s: None
    • Component/s: cdi, jms
    • Labels:
      None
    • Environment:

      Linux, jdk 1.7.0_45

      Description

      I have an @ApplicationScoped bean defined in a common library which I add to my projects in a dependency jar.

      Injection of the connection factory and queue results in null objects when using @Resource

      Example code:

      In this code, I have tried both @Resource(name = "... and @Resource(mappedName = "... with the same result.

      @ApplicationScoped
      public class PerformanceLoggingProducer {
      
          private static final Logger logger = Logger.getLogger(PerformanceLoggingProducer.class.getName());
      
          @Resource(mappedName = "jms/ConnectionFactory")
          private ConnectionFactory connectionFactory;
          @Resource(name = "jms/PerformanceLoggingQueue")
          private Queue queue;
      
          public void log(final PerformanceLogTracker tracker) {
      
              if (tracker != null) {
            
                  Thread t = new Thread(new Runnable() {
      
                      @Override
                      public void run() {
                          try (Connection connection = connectionFactory.createConnection()) {
                              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                              MessageProducer producer = session.createProducer(queue);
                              ObjectMessage message = session.createObjectMessage(tracker);
                              producer.send(message);
                          } catch (Exception ex) {
                              logger.log(Level.SEVERE, "An error occurred when logging message: " + tracker, ex);
                          }
                      }
                  });
                  
                  t.start();
      
              }
          }
      
      }
      

      Workaround:

      Don't use @Resource for the lookup.

      @ApplicationScoped
      public class PerformanceLoggingProducer {
      
          private static final Logger logger = Logger.getLogger(PerformanceLoggingProducer.class.getName());
      
      //    @Resource(mappedName = "jms/ConnectionFactory")
          private ConnectionFactory connectionFactory;
      //    @Resource(name = "jms/PerformanceLoggingQueue")
          private Queue queue;
      
          public PerformanceLoggingProducer() {
              try {
                  connectionFactory = (ConnectionFactory) new InitialContext().lookup("jms/ConnectionFactory");
                  queue = (Queue) new InitialContext().lookup("jms/PerformanceLoggingQueue");
              } catch (NamingException ex) {
                  logger.log(Level.SEVERE, null, ex);
              }
          }
      
          public void log(final PerformanceLogTracker tracker) {
      
              if (tracker != null) {
            
                  Thread t = new Thread(new Runnable() {
      
                      @Override
                      public void run() {
                          try (Connection connection = connectionFactory.createConnection()) {
                              Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                              MessageProducer producer = session.createProducer(queue);
                              ObjectMessage message = session.createObjectMessage(tracker);
                              producer.send(message);
                          } catch (Exception ex) {
                              logger.log(Level.SEVERE, "An error occurred when logging message: " + tracker, ex);
                          }
                      }
                  });
                  
                  t.start();
      
              }
          }
      
      }
      

        Activity

        Hide
        jjsnyder83 added a comment -

        Can you provide a test app? I'd like to see how it's packaged.

        Show
        jjsnyder83 added a comment - Can you provide a test app? I'd like to see how it's packaged.

          People

          • Assignee:
            jjsnyder83
            Reporter:
            electricsam
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: