Issue Details (XML | Word | Printable)

Key: GLASSFISH-20984
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: jjsnyder83
Reporter: electricsam
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
glassfish

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

Created: 13/Feb/14 04:10 PM   Updated: 13/Feb/14 04:10 PM
Component/s: cdi, jms
Affects Version/s: 4.0
Fix Version/s: None

Time Tracking:
Not Specified

Environment:

Linux, jdk 1.7.0_45


Tags:
Participants: electricsam and jjsnyder83


 Description  « Hide

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();

        }
    }

}


There are no comments yet on this issue.