1

Despite what seems to be a successful binding of the JBoss (AS 7.1.1.Final) connection factory:

[org.jboss.as.messaging] (MSC service thread 1-9) JBAS011601: Bound messaging object to jndi name java:/ConnectionFactory

The ConnectionFactory in the lookup is always null. Can anyone see what the problem is?

@Configuration
@ComponentScan(basePackages = "reservation")
public class AppConfiguration extends WebMvcConfigurerAdapter {

    // ***********************//
    // ******** JMS **********//
    // ***********************//
    @Bean
    public ConnectionFactory jmsConnectionFactory() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName("java:/ConnectionFactory");
        return (ConnectionFactory) jndiObjectFactoryBean.getObject();
    }

    @Bean
    public Queue requestsQueue() {
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName("java:/queue/test");
        return (Queue) jndiObjectFactoryBean.getObject();
    }

    @Bean
    public JmsOperations jmsOperations() {
        final JmsTemplate jmsTemplate = new JmsTemplate(jmsConnectionFactory());
        jmsTemplate.setDefaultDestination(requestsQueue());
        return jmsTemplate;
    }
}
chrisjleu
  • 4,329
  • 7
  • 42
  • 55

1 Answers1

3

Unfortunately you must call afterPropertiesSet() manually:

@Bean
public ConnectionFactory jmsConnectionFactory() {
    JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("java:/ConnectionFactory");
    jndiObjectFactoryBean.afterPropertiesSet();                    //HERE
    return (ConnectionFactory) jndiObjectFactoryBean.getObject();
}

An alternative I particularly like is as follows:

@Bean
public JndiObjectFactoryBean jmsConnectionFactoryFactoryBean() {
    JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("java:/ConnectionFactory");
    return jndiObjectFactoryBean;
}

public ConnectionFactory jmsConnectionFactory() {
    return (ConnectionFactory) jmsConnectionFactoryFactoryBean().getObject();
}

Notice that jmsConnectionFactory() is not annotated with @Bean (it's important). In that case Spring will call appropriate callback method for you.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Suggested Edit by @chrisjleu which was rejected as an Invalid Edit : _"Change `public jmsConnectionFactory()` to `public ConnectionFactory jmsConnectionFactory()`"_. Please review and make the changes if it needs to be changed. – iDev Jan 25 '13 at 06:29
  • 1
    @ACB: Not sure what "Please review" means but it didn't compile without the edit so that's why I made it. Now I've been able to fully test and can confirm that the answer is correct. Incidentally, I went with the alternative/second approach and used it also for configuring `requestsQueue()`. – chrisjleu Jan 25 '13 at 13:12
  • @chrisjleu: your edit was valid but before I came it was rejected. Nevertheless I applied the changes. I'm glad it helped you! – Tomasz Nurkiewicz Jan 25 '13 at 15:13
  • @chrisjleu, It is always better to add a comment and then if it is not changed, edit the answer. Usually these kind of edits will be rejected as Invalid Edit since you are modifying an answer/accepted answer. I felt your edit could be a genuine one but wanted Tomasz Nurkiewicz to review it before making the changes. – iDev Jan 25 '13 at 18:02
  • 2
    @ACB: is it always better to add a comment? I don't agree. The answer was a perfectly good one except for a small error. Correcting the answer itself means that future viewers of this question do not have to waste time reading comments that cease to be relevant the moment the correction is made. That is partly the purpose of the edit button is it not - to suggest a correction or improvement? Anyway, if you wanted Tomasz to review it first, why not just let him... – chrisjleu Jan 25 '13 at 22:15
  • @chrisjleu, Well, I understand your good intentions. But often these kind of edits are rejected once it comes to the Edit queue since the person who is reviewing may not be able to judge whether an edit is a good one or bad one. So if you really wish to make it go through it, you might have to add a comment like I did. Otherwise your edit would have been just rejected and future viewers wouldn't have even noticed that. It is upto you to decide. Check this meta answer for details http://meta.stackexchange.com/a/154814/200687 – iDev Jan 25 '13 at 22:52
  • 1
    From the meta answer: "The other reviewer rejected your edit because your change was too radical; changing code is generally frowned upon, unless you are correcting obvious typos in answer code". I'm inclined to think the alteration I made borders on being a correction of a typo, but nevertheless your point is noted. Perhaps we should end this because despite these comments being utterly useless for this particular question, I'm starting to feel like it would be a shame to delete them... ;-) – chrisjleu Jan 26 '13 at 06:38