Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup JMS configuration in Spring's applicationContext?

How can I setup application for given below java code?

Hashtable<String, String> properties = new Hashtable<String, String>();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.test.factory");
            properties.put("com.domain", "DevelopmentDomain");
//            properties.put(Context.PROVIDER_URL, "tcp://test:0506");
            properties.put(Context.PROVIDER_URL, "tcp://10.00.0.00:0506");


            properties.put(Context.SECURITY_PRINCIPAL, "aaa");
            properties.put(Context.SECURITY_CREDENTIALS, "aaa");

            javax.naming.Context context = new javax.naming.InitialContext(properties);
            ConnectionFactory factory = (ConnectionFactory) context.lookup("ImpactPocQueueConnectionFactory");

            Connection connection = factory.createConnection();

            connection.start();

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Destination queue = session.createQueue("test.producer");

I saw many examples here and there but none of them give complete picture.

like image 671
Himanshu Yadav Avatar asked Sep 03 '25 14:09

Himanshu Yadav


2 Answers

If you want to translate this code literally to Spring configuration, consider taking advantage of @Configuration approach:

@Bean
public Destination queue() throws JMSException, NamingException {
    return session().createQueue("test.producer");
}

@Bean
public Session session() throws JMSException, NamingException {
    return connection().createSession(false, Session.AUTO_ACKNOWLEDGE);
}

@Bean(initMethod = "start")
public Connection connection() throws JMSException, NamingException {
    return connectionFactory().createConnection();
}

@Bean
public ConnectionFactory connectionFactory() throws NamingException {
    return (ConnectionFactory) context().lookup("ImpactPocQueueConnectionFactory");
}

@Bean
public Context context() throws NamingException {
    return new javax.naming.InitialContext(properties());
}

@Bean
public Hashtable<String, String> properties() {
    Hashtable<String, String> properties = new Hashtable<String, String>();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.test.factory");
    properties.put("com.domain", "DevelopmentDomain");
    //properties.put(Context.PROVIDER_URL, "tcp://test:0506");
    properties.put(Context.PROVIDER_URL, "tcp://10.00.0.00:0506");
    properties.put(Context.SECURITY_PRINCIPAL, "aaa");
    properties.put(Context.SECURITY_CREDENTIALS, "aaa");
    return properties;
}

Technically you can do all of this using XML, but I found this approach much more readable and maintainable. Now you have connectionFactory and queue beans available in scope. You can easily integrate with Spring JMS support. Let us know if you need further assitance.

like image 196
Tomasz Nurkiewicz Avatar answered Sep 05 '25 08:09

Tomasz Nurkiewicz


Below given Spring applicationContext xml configuration is tested and working fine for me.

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">com.test.factory</prop>
                <prop key="com.sonicsw.jndi.mfcontext.domain">DevelopmentDomain</prop>
                <prop key="java.naming.provider.url">tcp://10.00.0.00:0506</prop>
                <prop key="java.naming.security.principal">aaa</prop>
                <prop key="java.naming.security.credentials">aaa</prop>
            </props>
        </property>
    </bean>
    <bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate" ref="jndiTemplate"/>
        <property name="jndiName" value="ImpactPocQueueConnectionFactory"/>
    </bean>
    <bean id="sonic" class="org.apache.camel.component.jms.JmsComponent"> 
        <property name="connectionFactory" ref="jmsQueueConnectionFactory"/> 
    </bean>

    <route>
    <from uri="seda:something"/>
    <to uri="sonic:queue:queueName"/>
    </route>
like image 38
Himanshu Yadav Avatar answered Sep 05 '25 07:09

Himanshu Yadav