Spring Questions and Answers – Transformation and Error handling

This set of Java Spring Multiple Choice Questions & Answers (MCQs) focuses on “Transformation and Error handling”.

1. To send a message into the bus and transform it before working with it further.
a) adding extra headers or augmenting the payload
b) transformer
c) all of the mentioned
d) none of the mentioned
View Answer

Answer: c
Explanation: You might also want to transform a message by enriching it—adding extra headers or augmenting the payload so that components downstream in the processing pipeline can benefit from it. Use a transformer component to take a Message of a payload and send the Message out with a payload of a different type.

2. Spring Integration provides a transformer message endpoint to permit the augmentation of the message headers.
a) True
b) False
View Answer

Answer: a
Explanation: Spring Integration provides a transformer message endpoint to permit the augmentation of the message headers or the transformation of the message itself. In Spring Integration, components are chained together, and output from one component is returned by way of the method invoked for that component.

3.The output is constructed dynamically using MessageBuilder to create a message that has the same payload as the input message as well as copy the existing headers and adds an extra header:

advertisement
advertisement
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageBuilder;
import java.util.Map;
public class InboundJMSMessageToCustomerWithExtraMetadataTransformer {
	@Transformer
	public Message<Customer> transformJMSMapToCustomer(
	Message<Map<String, Object>> inboundSpringIntegrationMessage) {
	Map<String, Object> jmsMessagePayload =
	inboundSpringIntegrationMessage.getPayload();
	Customer customer = new Customer();
	customer.setFirstName((String) jmsMessagePayload.get("firstName"));
	customer.setLastName((String) jmsMessagePayload.get("lastName"));
	customer.setId((Long) jmsMessagePayload.get("id"));
	return MessageBuilder.withPayload(customer)
	.copyHeadersIfAbsent( inboundSpringIntegrationMessage.getHeaders())
	.setHeaderIfAbsent("randomlySelectedForSurvey", Math.random() > .5)
	.build();
	}
}

a) randomlySelected
b) randomlySelectedForSurvey
c) randomly
d) none of the mentioned
View Answer

Answer: b
Explanation: As before, this code is simply a method with an input and an output. The output is constructed dynamically using MessageBuilder to create a message that has the same payload as the input message as well as copy the existing headers and adds an extra header: randomlySelectedForSurvey.
Note: Join free Sanfoundry classes at Telegram or Youtube

4. Spring Integration provides the ability to catch exceptions and send them to an error channel of your choosing. By default, it’s a global channel called :-
a) error
b) exceptionChannel
c) exception
d) errorChannel
View Answer

Answer: d
Explanation: You can have components subscribe to messages from this channel to override the exception handling behavior.

5. The errorChannel doesn’t need to be a service-activator.
a) True
b) False
View Answer

Answer: a
Explanation: We just use it for convenience here. The code for the following service-activator depicts some of the machinations you might go through to build a handler for the errorChannel.
advertisement

6. All errors thrown from Spring Integration components will be a subclass of:-
a) Messaging
b) MessagingException
c) Exception
d) None of the mentioned
View Answer

Answer: b
Explanation: MessagingException carries a pointer to the original Message that caused an error, which you can dissect for more context information.

7. One way to discriminate by Exception type is to use:-
a) org.springframework.integration.router.ErrorMessageExceptionType
b) org.springframework.integration.router.ErrorMessageException
c) org.springframework.integration.router.ErrorMessageExceptionTypeRouter
d) none of the mentioned
View Answer

Answer: c
Explanation: Sometimes, more specific error handling is required. One way to discriminate by Exception type is to use the org.springframework.integration.router.ErrorMessageExceptionTypeRouter class.
advertisement

8. Sending all the errors to the same channel can eventually lead to a large switch-laden class that’s too complex to maintain.
a) True
b) False
View Answer

Answer: a
Explanation: Instead, it’s better to selectively route error messages to the error channel most appropriate to each integration.

9. You can explicitly specify on what channel errors for a given integration should go.
a) True
b) False
View Answer

Answer: a
Explanation: A component (service-activator) that upon receiving a message, adds a header indicating the name of the error channel.

10. Spring Integration will use that header and forward errors encountered in the processing of this message to that channel.
a) True
b) False
View Answer

Answer: a
Explanation: The following example shows a component (service-activator) that upon receiving a message, adds a header indicating the name of the error channel. Spring Integration will use that header and forward errors encountered in the processing of this message to that channel.

import org.apache.log4j.Logger;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.message.MessageBuilder;
public class ServiceActivatorThatSpecifiesErrorChannel {
private static final Logger logger = Logger.getLogger(
	ServiceActivatorThatSpecifiesErrorChannel.class);
	@ServiceActivator
	public Message<?> startIntegrationFlow(Message<?> firstMessage)
	throws Throwable {
	return MessageBuilder.fromMessage(firstMessage).
	setHeaderIfAbsent( MessageHeaders.ERROR_CHANNEL,
	"errorChannelForMySolution").build();
	}
}

11. All errors that come from the integration in which this component is used will be directed to:-

import org.apache.log4j.Logger;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.message.MessageBuilder;
public class ServiceActivatorThatSpecifiesErrorChannel {
private static final Logger logger = Logger.getLogger(
	ServiceActivatorThatSpecifiesErrorChannel.class);
	@ServiceActivator
	public Message<?> startIntegrationFlow(Message<?> firstMessage)
	throws Throwable {
	return MessageBuilder.fromMessage(firstMessage).
	setHeaderIfAbsent( MessageHeaders.ERROR_CHANNEL,
	"errorChannelForMySolution").build();
	}
}

a) customErrorChannel
b) customError
c) errorChannel
d) none of the mentioned
View Answer

Answer: a
Explanation: Thus, all errors that come from the integration in which this component is used will be directed to customErrorChannel, to which you can subscribe any component you like.

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.