Spring Questions and Answers – JMS Messages and Transactions

This set of Java Spring Multiple Choice Questions & Answers (MCQs) focuses on “JMS Messages and Transactions”.

1. Template which can send and receive JMS messages with much less code
a) JmsTemplate
b) EMail
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: a
Explanation: With a JMS template (Spring framework class JmsTemplate), you can send and receive JMS messages with much less code.

2. The template handles the boilerplate tasks for you and also converts the JMS API JMSException hierarchy into Spring runtime exception:-
a) org.springframework.jms.Jms
b) org.springframework.jms.JmsException
c) org.springframework.jms.JmsTemplate
d) none of the mentioned
View Answer

Answer: b
Explanation: The translation converts exceptions to a mirrored hierarchy of unchecked exceptions.

3. To address different JMS APIs, Spring provides :-
a) JmsTemplate
b) JmsTemplate102
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: To address different JMS APIs, Spring provides two JMS template classes, JmsTemplate and JmsTemplate102, for these two versions of JMS.
advertisement
advertisement

4. Before you can send and receive JMS messages, you need to install a JMS message broker:-
a) Apache ActiveM
b) Apache Active
c) Apache MQ
d) Apache ActiveMQ
View Answer

Answer: d
Explanation: Before you can send and receive JMS messages, you need to install a JMS message broker. For simplicity’s sake, we have chosen Apache ActiveMQ (http://activemq.apache.org/) as our message broker, which is very easy to install and configure.

5. In the preceding sendMail() method, you first create JMS-specific ConnectionFactory.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class FrontDeskImpl implements FrontDesk {
	public void sendMail(Mail mail) {
		ConnectionFactory cf =
		new ActiveMQConnectionFactory("tcp://localhost:61616");
		Destination destination = new ActiveMQQueue("mail.queue");
		Connection conn = null;
		try {
			conn = cf.createConnection();
			Session session =
			conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(destination);
			MapMessage message = session.createMapMessage();
			message.setString("mailId", mail.getMailId());
			message.setString("country", mail.getCountry());
			message.setDouble("weight", mail.getWeight());
			producer.send(message);
			session.close();
			} catch (JMSException e) {
				throw new RuntimeException(e);
			} finally {
				if (conn != null) {
				try {
				conn.close();
				} catch (JMSException e) {
				}
			}
		}
	}
}

a) True
b) False
View Answer

Answer: a
Explanation: In the preceding sendMail() method, you first create JMS-specific ConnectionFactory and Destination objects with the classes provided by ActiveMQ.
advertisement

6.In JMS, there are two types of destinations:-

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class FrontDeskImpl implements FrontDesk {
	public void sendMail(Mail mail) {
		ConnectionFactory cf =
		new ActiveMQConnectionFactory("tcp://localhost:61616");
		Destination destination = new ActiveMQQueue("mail.queue");
		Connection conn = null;
		try {
			conn = cf.createConnection();
			Session session =
			conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(destination);
			MapMessage message = session.createMapMessage();
			message.setString("mailId", mail.getMailId());
			message.setString("country", mail.getCountry());
			message.setDouble("weight", mail.getWeight());
			producer.send(message);
			session.close();
			} catch (JMSException e) {
				throw new RuntimeException(e);
			} finally {
				if (conn != null) {
				try {
				conn.close();
				} catch (JMSException e) {
				}
			}
		}
	}
}

a) topic
b) queue
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: c
Explanation: The message broker URL is the default for ActiveMQ if you run it on localhost. In JMS, there are two types of destinations: queue and topic. As explained before, a queue is for the point-to-point communication model, while topic is for the publish-subscribe communication model.
advertisement

7. There are several types of messages defined in the JMS API, including:-
a) TextMessage
b) MapMessage
c) BytesMessage
d) All of the mentioned
View Answer

Answer: d
Explanation: There are several types of messages defined in the JMS API, including TextMessage, MapMessage, BytesMessage, ObjectMessage, and StreamMessage.

8. To send a JMS message with this template, you simply call:-
a) send
b) sendTo
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: a
Explanation: To send a JMS message with this template, you simply call the send() method and provide a message destination, as well as a MessageCreator object, which creates the JMS message you are going to send.

9. The MessageCreator interface declares method:-
a) createMessage()
b) create()
c) createMsg()
d) none of the mentioned
View Answer

Answer: a
Explanation: The MessageCreator interface declares only a createMessage() method for you to implement.

10. A JMS template helps you to obtain and release the JMS connection and session.
a) True
b) False
View Answer

Answer: a
Explanation: A JMS template helps you to obtain and release the JMS connection and session, and it sends the JMS message created by your MessageCreator object.

11. JMS sender and receiver classes can also extend to retrieve a JMS template:-
a) JmsGatewaySupport
b) JmsGateway
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: a
Explanation: Just like your DAO class can extend JdbcDaoSupport to retrieve a JDBCtemplate, your JMS sender and receiver classes can also extend JmsGatewaySupport to retrieve a JMS template.

12. When you need access to the JMS template.
a) setJmsTemplate
b) getJmsTemplate
c) getJms
d) none of the mentioned
View Answer

Answer: b
Explanation: When you need access to the JMS template, you just make a call to getJmsTemplate().

13. Spring provides an implementation of SimpleMessageConvertor to handle the translation of a JMS message received.
a) True
b) False
View Answer

Answer: a
Explanation: Spring provides an implementation of SimpleMessageConvertor to handle the translation of a JMS message received to a business object and the translation of a business object to a JMS message.

14. By default, the JMS template uses SimpleMessageConverter for converting TextMessage to or from a string.
a) True
b) False
View Answer

Answer: a
Explanation: By default, the JMS template uses SimpleMessageConverter for converting TextMessage to or from a string, BytesMessage to or from a byte array, MapMessage to or from a map, and ObjectMessage to or from a serializable object.

15. For your front desk and back office classes, you can send and receive a map using the:-
a) convertAndSend()
b) receiveAndConvert()
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: c
Explanation: For your front desk and back office classes, you can send and receive a map using the convertAndSend() and receiveAndConvert() methods, and the map will be converted to/from MapMessage.

Sanfoundry Global Education & Learning Series – Java Spring.
To practice all areas of Java Spring, 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.