Felsia Thyagaraj
Felsia Thyagaraj

Reputation: 11

How to connect Existing JMS configured as JNDI in JBOSS EAP 7 from spring boot application?

I am having a spring boot application which has to connect to an existing JMS configured as JNDI in JBOSS eap 7. Here is my code:

private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
private static final String CONNECTION_FACTORY = "jms/RemoteConnectionFactory";


@Bean
public ConnectionFactory connectionFactory() {
    try {
        System.out.println("Retrieving JMS queue with JNDI name: " + CONNECTION_FACTORY);
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName(CONNECTION_FACTORY);
        jndiObjectFactoryBean.setJndiEnvironment(getEnvProperties());
        jndiObjectFactoryBean.afterPropertiesSet();

        return (QueueConnectionFactory) jndiObjectFactoryBean.getObject();

    } catch (NamingException e) {
        System.out.println("Error while retrieving JMS queue with JNDI name: [" + CONNECTION_FACTORY + "]");
        e.printStackTrace();
    } catch (Exception ex) {
        System.out.println("Error");
        ex.printStackTrace();
    }
    return null;
}

Properties getEnvProperties() {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
    env.put(Context.PROVIDER_URL, "http-remoting://<<servername>>:8080");
    env.put(Context.SECURITY_PRINCIPAL, "user");
    env.put(Context.SECURITY_CREDENTIALS, "password");
    return env;
}

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();
    jndiDestinationResolver.setJndiEnvironment(getEnvProperties());
    factory.setDestinationResolver(jndiDestinationResolver);
    return factory;
}

I am getting the following error:

"javax.naming.CommunicationException: Failed to connect to any server. Servers tried: [http-remoting://<<servername>>:8080 (java.net.ConnectException: Connection refused: no further information)]"

Upvotes: 1

Views: 2013

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36223

You don't have to create the configuration manually.

Spring Boot supports JNDI.

You can use the ConnectionFactory like this:

spring.jms.jndi-name=java:/jms/RemoteConnectionFactory

Please also have a look at the documentation:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jms-jndi

Upvotes: 1

Related Questions