Java Spring Multiple Choice Questions & Answers (MCQs) 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: To address different JMS APIs, Spring provides two JMS template classes, JmsTemplate and JmsTemplate102, for these two versions of JMS.
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
Answer: d
Clarification: 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.
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
Answer: a
Clarification: In the preceding sendMail() method, you first create JMS-specific ConnectionFactory and Destination objects with the classes provided by ActiveMQ.
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
Answer: c
Clarification: 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.
7. There are several types of messages defined in the JMS API, including:-
a) TextMessage
b) MapMessage
c) BytesMessage
d) All of the mentioned
Answer: d
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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.