Google

Dec 6, 2011

Spring Interview Questions and Answers: JNDI, JMS, and bean scopes

Spring Interview Questions and Answers Q1 - Q14 are FAQs

Q1 - Q4 Overview & DIP Q5 - Q8 DI & IoC Q9 - Q10 Bean Scopes Q11 Packages Q12 Principle OCP Q14 AOP and interceptors
Q15 - Q16 Hibernate & Transaction Manager Q17 - Q20 Hibernate & JNDI Q21 - Q22 read properties Q23 - Q24 JMS & JNDI Q25 JDBC Q26 Spring MVC Q27 - Spring MVC Resolvers

Q23. How can you configure JNDI in spring application context?
A23. Using "org.springframework.jndi.JndiObjectFactoryBean". For example,

Example 1:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"   scope="singleton">
  <property name="jndiName">
   <value>java:comp/env/jdbc/dataSource/myapp_ds</value>
  </property>
 </bean>



Example 2:

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:myapp.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

The myapp.properties

jndi.naming.myapp=java:comp/env/jdbc/dataSource/myapp_ds
...

Use the property "jndi.naming.myapp"

<bean id="mfsShyDS" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>${jndi.naming.myapp}</value>
</property>
</bean>


Q. What is difference between singleton and prototype bean?
A. If you had noticed the above examples, the scope was set to "singleton". This means single bean instance per IOC container. In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller.



Scopes:
  • singleton – Return a single bean instance per Spring IoC container. you would use singletons for stateless services where you will only have one instance of each service, and since they are stateless they are  threadsafe.
  • prototype – Return a new bean instance each time when requested. you would use prototypes for stateful scenarios like request actions (e.g. in struts), so a new object gets created to handle each request.

the default scope is singleton. The web-aware scopes are
  • request – Return a single bean instance per HTTP request.
  • session – Return a single bean instance per HTTP session.
  • globalSession – Return a single bean instance per global HTTP session.



Q24. How would you use Spring to send JMS based messages
A24. Here is some pseudocode that uses Spring with JNDI and JMS to send messages. It assumes that the ConnectionFactory and Destination values are configured via JNDI.

To send messages

  • Locate a ConnectionFactory, typically using JNDI.
  • Locate a Destination, typically using JNDI.
  • Create a Spring JmsTemplate with the ConnectionFactory.
  • Create a JmsTemplate using the ConnectionFactory.
  • Inject the JmsTemplate and the destination to your "MyAppMessageSender" to send messages using the jmsTemplate and the destination you just injected.
  • A sessiion needs to be created via the Spring class MessageCreator, and subsequently a message is created on the session created.

To receive messages

  • Locate a ConnectionFactory, typically using JNDI.
  • Locate a Destination, typically using JNDI.
  • Create a message listener class (e.g. com.MyAppListener)
  • Create a JMS message container by injecting ConnectionFactory, Destination, and the listener. The container is responsible for binding the listener to the queue.


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> 

 <!-- JNDI configuration -->
    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
        <property name="environment"> 
            <props> 
                <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop> 
                <prop key="java.naming.provider.url">jnp://localhost:1099</prop> 
                <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop> 
                <prop key="java.naming.security.principal">admin</prop> 
                <prop key="java.naming.security.credentials">admin</prop> 
            </props> 
        </property> 
    </bean>  
  
  
    <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiTemplate" ref="jndiTemplate" /> 
        <property name="jndiName" value="MyAppConnectionFactory" /> 
    </bean> 
 
  <bean id="jmsQueueDestination" class="org.springframework.jndi.JndiObjectFactoryBean"> 
        <property name="jndiTemplate" ref="jndiTemplate" /> 
        <property name="jndiName"> 
            <value>queue/MyAppQueue</value> 
        </property> 
    </bean> 
 
 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
           <property name="connectionFactory" ref="jmsConnectionFactory"/>
 </bean>
 
 
 <bean id="sender" class="com.MyAppMessageSender"> 
         <property name="connectionFactory" ref="connectionFactory"/>
   <property name="queue" ref="jmsQueueDestination"> 
     
    </bean>


 <!-- POJO Messgae Listener -->
 <bean id="jmsMessageListener" class="com.MyAppListener" />
  
   
   <!-- Connects the queue to the POJO message listener -->
    <bean id="jmsContainer"  class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
        <property name="connectionFactory" ref="jmsConnectionFactory" /> 
        <property name="destination" ref="jmsQueueDestination" /> 
        <property name="messageListener" ref="jmsMessageListener" /> 
    </bean> 

  
</beans> 


Here is the message sender -- com.MyAppMessageSender

package com;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;


public class MyAppMessageSender {
    
    private JmsTemplate jmsTemplate; 
    private Destination destination; 

   
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void setDestination(Destination destination) {
        this.destination = destination;
    } 
    
    
    public void sendMessage() { 
        this.jmsTemplate.send(this.destination, new MessageCreator() { 
            public Message createMessage(Session session) throws JMSException { 
              return session.createTextMessage("message from myapp"); 
            } 
        }); 
    } 
}


Here is the message receiver -- com.MyAppListener. The onMessage(...) method in invoked asynchronously when a message apper in the destination (e.g. Queue/Topic)

package com;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MyAppListener implements MessageListener {
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                System.out.println(((TextMessage) message).getText());
            }
            catch (JMSException ex) {
                throw new RuntimeException(ex);
            }
        }
        else {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }
}

Labels:

2 Comments:

Blogger jigarnagar said...

This can be also help you.

http://www.aoiblog.com/spring-interview-question/

11:11 PM, April 30, 2012  
Anonymous grails said...

nice questions, but its hard to memorize interview questions.

10:47 AM, May 05, 2014  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home