Java Spring question bank on “Message-Driven POJOs in Spring and making connection”
1. When you call the receive() method on a JMS message consumer to receive a message.
a) send
b) receive
c) All of the mentioned
d) None of the mentioned
Answer: b
Clarification: When you call the receive() method on a JMS message consumer to receive a message, the calling thread is blocked until a message is available.
2. EJB component which was introduced for asynchronous reception of JMS messages.
a) message-driven bean
b) message-driven
c) message bean
d) none of the mentioned
Answer: a
Clarification: Starting with EJB 2.0, a new kind of EJB component called a message-driven bean (MDB) was introduced for asynchronous reception of JMS messages.
3. MDB must implements interfaces:-
a) javax.ejb.MessageDrivenBean
b) javax.jms.MessageListener
c) all of the mentioned
d) none of the mentioned
Answer: c
Clarification: An MDB must implement both the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces and override all EJB life cycle methods (ejbCreate and ejbRemove).
4. In EJB 3.0, an MDB can be a POJO that implements the MessageListener interface and is annotated with the:-
a) @MessageDrive
b) @Message
c) all of the mentioned
d) @MessageDriven
Answer: d
Clarification: In EJB 3.0, an MDB can be a POJO that implements the MessageListener interface and is annotated with the @MessageDriven annotation.
5. You create a message listener to listen for JMS messages. This negates the need for the approach taken in BackOfficeImpl in previous recipes.
a) True
b) False
Answer: a
Clarification: For example, the following MailListener listens for JMS messages that contain mail information:-
import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageListener; import org.springframework.jms.support.JmsUtils; public class MailListener implements MessageListener { public void onMessage(Message message) { MapMessage mapMessage = (MapMessage) message; try { Mail mail = new Mail(); mail.setMailId(mapMessage.getString("mailId")); mail.setCountry(mapMessage.getString("country")); mail.setWeight(mapMessage.getDouble("weight")); displayMail(mail); } catch (JMSException e) { throw JmsUtils.convertJmsAccessException(e); } } private void displayMail(Mail mail) { System.out.println("Mail #" + mail.getMailId() + " received"); } }
6. A message listener must implement the:-
a) javax.jms.MessageListener
b) javax.jms.Message
c) javax.jms
d) none of the mentioned
Answer: a
Clarification: A message listener must implement the javax.jms.MessageListener interface.
7. When a JMS message arrives, the onMessage() method will be called with the message as the method argument.
a) True
b) False
Answer: a
Clarification: In this sample, you simply display the mail information to the console.
import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageListener; import org.springframework.jms.support.JmsUtils; public class MailListener implements MessageListener { public void onMessage(Message message) { MapMessage mapMessage = (MapMessage) message; try { Mail mail = new Mail(); mail.setMailId(mapMessage.getString("mailId")); mail.setCountry(mapMessage.getString("country")); mail.setWeight(mapMessage.getDouble("weight")); displayMail(mail); } catch (JMSException e) { throw JmsUtils.convertJmsAccessException(e); } } private void displayMail(Mail mail) { System.out.println("Mail #" + mail.getMailId() + " received"); } }
8. Method to convert MapMessage Object into Spring runtime exception JmsException.
a) JmsUtils.convertJmsAccessException()
b) JmsUtils.convertJmsAccess()
c) JmsUtils.convertJms()
d) none of the mentioned
Answer: a
Clarification: When extracting message information from a MapMessage object, you need to handle the JMS API’s JMSException. You can make a call to JmsUtils.convertJmsAccessException() to convert it into Spring runtime exception JmsException.
9. Spring provides several types of message listener containers:-
a) SimpleMessageListenerContainer
b) DefaultMessageListenerContainer
c) All of the mentioned
d) None of the mentioned
Answer: c
Clarification: Spring provides several types of message listener containers for you to choose from in the org.springframework.jms.listener package, of which SimpleMessageListenerContainer and DefaultMessageListenerContainer are the most commonly used.
10. If you have a transaction requirement in receiving messages, you have to use:-
a) SimpleMessageListenerContainer
b) DefaultMessageListenerContainer
c) All of the mentioned
d) None of the mentioned
Answer: b
Clarification: If you have a transaction requirement in receiving messages, you have to use DefaultMessageListenerContainer.
11. You have to set the delegate property of MessageListenerAdapter to your target bean.
a) True
b) False
Answer: a
Clarification: By default, this adapter will call the method whose name is handleMessage on that bean.
12. Using Spring JMS support with a very simple instance of:-
a) org.apache.activemq.ActiveMQConnectionFactory
b) org.apache.activemq.ActiveMQConnection
c) org.apache.activemq.ActiveMQ
d) none of the mentioned
Answer: a
Clarification: We’ve explored using Spring JMS support with a very simple instance of org.apache.activemq.ActiveMQConnectionFactory as our connection factory.
13. ActiveMQ, provides only one pooled connection factory class alternative.
a) True
b) False
Answer: b
Clarification: ActiveMQ provides two, actually: one for use consuming messages with a JCA connector and another one for use outside of a JCA container.
14. It caches consumers correctly, or use Spring ConnectionFactory implementations.
a) MessageListenerContainer
b) MessageListener
c) MessageContainer
d) None of the mentioned
Answer: a
Clarification: MessageListenerContainer implementations mechanism (MDPs), because it caches consumers correctly, or use Spring ConnectionFactory implementations.
15. Implementation which returns the same underlying JMS connection each time (which is thread-safe according to the JMS API) and ignores calls to the close() method.
a) org.springframework.jms.connection.SingleConnectionFactory
b) org.springframework.jms.connection.Single
c) org.springframework.jms.connection.SingleConnection
d) none of the mentioned
Answer: a
Clarification: org.springframework.jms.connection.SingleConnectionFactory, returns the same underlying JMS connection each time (which is thread-safe according to the JMS API) and ignores calls to the close() method.
To practice Java Spring question bank,