Spring Questions and Answers – Message-Driven POJOs in Spring and making connection

This set of Java Spring question bank focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: An MDB must implement both the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces and override all EJB life cycle methods (ejbCreate and ejbRemove).
advertisement
advertisement

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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: For example, the following MailListener listens for JMS messages that contain mail information:-

Note: Join free Sanfoundry classes at Telegram or Youtube
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
View Answer

Answer: a
Explanation: A message listener must implement the javax.jms.MessageListener interface.
advertisement

7. When a JMS message arrives, the onMessage() method will be called with the message as the method argument.
a) True
b) False
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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.
advertisement

9. Spring provides several types of message listener containers:-
a) SimpleMessageListenerContainer
b) DefaultMessageListenerContainer
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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.

Sanfoundry Global Education & Learning Series – Java Spring.
To practice Java Spring question bank, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.