Java Spring Multiple Choice Questions & Answers (MCQs) 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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:
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
Answer: b
Clarification: 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.
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
Answer: d
Clarification: 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
Answer: a
Clarification: 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.
6. All errors thrown from Spring Integration components will be a subclass of:-
a) Messaging
b) MessagingException
c) Exception
d) None of the mentioned
Answer: b
Clarification: 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
Answer: c
Clarification: Sometimes, more specific error handling is required. One way to discriminate by Exception type is to use the org.springframework.integration.router.ErrorMessageExceptionTypeRouter class.
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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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.