2013-01-24 59 views
1

儘管似乎是JBoss的一個成功的結合(AS 7.1.1.Final)連接工廠:不能在春註解配置的JNDI查找找到了JBoss的ConnectionFactory

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

在查找ConnectionFactory的始終爲空。任何人都可以看到問題是什麼?

@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; 
    } 
} 

回答

3

不幸的是,你必須手動調用afterPropertiesSet()

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

另我特別喜歡的是如下:

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

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

注意jmsConnectionFactory()帶註釋@Bean(這一點很重要)。在這種情況下,Spring會爲你調用適當的回調方法。

+0

建議由@chrisjleu編輯,它被拒絕爲無效編輯:_「將public jmsConnectionFactory()'更改爲'public ConnectionFactory jmsConnectionFactory()'」_。如果需要更改,請查看並進行更改。 – iDev

+1

@ACB:不確定「Please review」是什麼意思,但沒有編輯就沒有編譯,所以這就是我爲什麼編譯它的原因。現在我已經能夠完全測試並確認答案是正確的。順便說一下,我採用了替代/第二種方法,並將它用於配置'requestsQueue()'。 – chrisjleu

+0

@chrisjleu:你的編輯是有效的,但在我來之前它被拒絕了。不過,我應用了這些變化。我很高興它幫助你! –