Frequently asked Spring Integration Interview Questions and Answers (2024) | TechGeekNext


Frequently asked Spring Integration Interview Questions and Answers (2024)

In this post, questions from Spring Integration Interviews will be answered for Experienced and Freshers. We're trying to share our experience and learn how to help you make progress in your career.

  1. Q: What is Spring Integration?
  2. Q: What is "Pipes and Filters" architecture in Spring Integration?
  3. Q: What are main components of Spring Integration?
  4. Q: What is a Message in Spring Integration?
  5. Q: How to create Message in Spring Integration, write some example?
  6. Q: What is Message Channel in Spring Integration?
  7. Q: What is Message EndPoint in Spring Integration?
  8. Q: What are different types of Message EndPoints?
  9. Q: What is a Message transformer in Spring Integration?
  10. Q: What is a Message Filter in Spring Integration?
  11. Q: What is a Message Router in Spring Integration?
  12. Q: What is a Splitter in Spring Integration?
  13. Q: What is an Aggregator in Spring Integration?
  14. Q: What is Service Activator in Spring Integration?
  15. Q: How error handling works in synchronous communication?
  16. Q: How Spring Integration handle error/exceptions in asynchronous communication?
  17. Q: In case of exception, what channel is the error message sent to from spring integration?
  18. Q: What is Global error channel?

Q: What is Spring Integration?
Ans:

Spring Integration is an open source framework for enterprise application integration. It is a lightweight framework that builds upon the core Spring framework. It extends the Spring framework to provide:

  1. Facilitate asynchronous, message-driven behavior within a Spring-based application.
  2. Provides a wide selection of mechanisms to communicate with external systems.
  3. Channel adapters are one such mechanism used for one-way integration (send or receive).
  4. Gateways are used for request/reply scenarios (inbound or outbound).

Q: What is "Pipes and Filters" architecture in Spring Integration?
Ans:

The filters represent any Sub-systems/microservices which produce or consume messages, and the 'pipes' transport the messages between filters so that the microservices themselves remain loosely-coupled.

Q: What are the key components of Spring Integration?
Ans:

Below are the key components of Spring integration. Any types of integration flow can be achieved by using these components.

  • Message
  • Message Channel
  • Message EndPoint

Q: What is a Message in Spring Integration?
Ans:

In Spring Integration, a message is a generic wrapper, consists of a payload and headers.

Spring Integration Message

The org.springframework.integration.Message interface defines the spring Message

public interface Message<T> {
    T getPayload();
    MessageHeaders getHeaders();
}
  1. Message Headers hold commonly required information such as ID, correlation ID, timestamp and return address in akey-value pair.
  2. Payload can be of any type, can be any payload data to transfer.

Q: How to create Message in Spring Integration, write some example?
Ans:

Creating Message using a builder class (MessageBuilder).

Message<String> message = MessageBuilder
                .withPayload("test message payload")
                .setHeader("key1", "valueA")
                .setHeader("key2", "valueB")
                .build();
Creating Message by using the implementation provided by the framework:
Map<String, Object> headers = new HashMap<>();
headers.put("key1", "valueA");
headers.put("key2", "valueB");

Message<String> message = new GenericMessage<String>("test message payload", headers);


Q: What is Message Channel in Spring Integration?
Ans:

A message channel represents the pipe of a pipes-and-filters architecture. In Message Channel,Producers send messages to a channel, and consumers receive messages from a channel.
The message channel therefore decouples the Message Producers and Message Consumers. A message channel may follow either point-to-point or publish-subscribe.

Spring Integration Message Channel

Q: What is Message EndPoint in Spring Integration?
Ans:

The key role of Message Endpoint is to connect application code to the messaging framework and to do so in a non-invasive manner.

  1. Message Channel is the architecture of pipes and filters, the Message EndPoint is the filter in that architecture.
  2. Message EndPoint provide declarative configuration, which 'connect' your domain-specific code to the messaging infrastructure provided by Spring Integration.
  3. To simplify the concept, it is similar to the role of a controller in the MVC paradigm.
    • Like Controller handles HTTP requests, the Message Endpoint handles messages.
    • Like as controllers are mapped to URL patterns, Message Endpoints are mapped to message channels.
    • The purpose in both cases is the same, to separate the application code from the infrastructure.

Q: What are different types of Message EndPoints in Spring Integration?
Ans:

Below are the different types of Message EndPoints.

  1. Message Transformer
  2. Message Filter
  3. Message Router
  4. Splitter
  5. Aggregator
  6. Service Activator
  7. Channel Adapter

Q: What is a Message Transformer in Spring Integration?
Ans:

A Message Transformer is responsible for converting a message's content or structure and returning the modified message. Convert Message payload from one format to another. For example: XML to CSV , XML to JSON etc.

Take a look at our Suggested Posts :

Q: What is a Message Filter in Spring Integration?
Ans:

Message filters are used to decide whether a Message is to be passed or dropped depending on a number of criteria, like the message header value or message content itself.

In comparison to the router, it does not decide which message channel it will send to but decides whether it will send the message at all.

Configuring a Filter with Annotations
The following example shows how to configure a filter by using annotations in Spring Integration:
public class EmployeeFilter {
    ...
    @Filter
    public boolean adminOnly(String input) {
        ...
    }
}

An annotation @Filter indicates that this method is to be used as a filter. Decision is taken based on Boolean value returned from a custom method (adminOnly) with our custom logic provided. We can return the response based on custom logic conditions like:

  1. If payload exists or not.
  2. If payload is of any particular type or not etc.
  3. If a file with specific extension exists or not.
If the method returns false for a message, it will either drop the message or raise an exception, based on how we configured it.
Note: This is different from the Filter concept of Pipes and Filter architecture.

Q: What is a Message Router in Spring Integration?
Ans:

A Message Router decides which would be next channels (if any) to receive the next message depending on a set of conditions. The decision is generally based on the content of the message or the metadata available in the message headers.

Spring Integration Message Router

Spring Integration provides the following routers:

PayloadTypeRouter

Depending on the payload type mapping, it routes the incoming messages coming from incoming channels to different output channels.
The following example shows the payload router configured in Java:

@ServiceActivator(inputChannel = "routingChannel")
@Bean
public PayloadTypeRouter router() {
    PayloadTypeRouter payloadRouter = new PayloadTypeRouter();
    payloadRouter.setChannelMapping(String.class.getName(), "stringChannel");
    payloadRouter.setChannelMapping(Integer.class.getName(), "integerChannel");
    return payloadRouter;
}

HeaderValueRouter

It decides the output channel based upon one of the header value. The value of the header could be one of two things:

  1. An arbitrary value
  2. A channel name
@ServiceActivator(inputChannel = "routingChannel")
@Bean
public HeaderValueRouter router() {
    HeaderValueRouter headerRouter = new HeaderValueRouter("testHeaderRouter");
    headerRouter.setChannelMapping("someHeaderValue", "channelA");
    headerRouter.setChannelMapping("someOtherHeaderValue", "channelB");
    return headerRouter;
}

RecipientListRouter

RecipientListRouter sends each incoming or received messages to all the configured output channels, like static broadcasting.

@ServiceActivator(inputChannel = "routingChannel")
@Bean
public RecipientListRouter router() {
    RecipientListRouter recipientRouter = new RecipientListRouter();
    recipientRouter.setSendTimeout(124L);
    recipientRouter.setIgnoreSendFailures(true);
    recipientRouter.setApplySequence(true);
    recipientRouter.addRecipient("channelA");
    recipientRouter.addRecipient("channelB");
    recipientRouter.addRecipient("channelC");
    return recipientRouter;
}

Q: What is a Splitter in Spring Integration?
Ans:

A Splitter is another kind of message endpoint which accept a message from its input channel, split that message into multiple messages, and send each of those to its appropriate output channel.
This is usually used for dividing a "composite" payload object into a group of messages containing the subdivided payloads.

Q: What is an Aggregator in Spring Integration?
Ans:

Aggregator is totally opposite to Splitter. An Aggregator receives multiple messages and combines them into a single message.

Q: What is Service Activator in Spring Integration?
Ans:

A Service Activator is a common endpoint for connecting a service instance to the messaging system.
The input message channel must be configured, and, if the service method produce/return a value, an output message Channel may also be provided. Note: The output channel is optional.
The Service Activator is any POJO which defines the @ServiceActivator annotation on a provided method. It allows us to execute any method on our POJO when a message is received from an inbound channel, and it allows us to write messages to an output channel.


@ServiceActivator(inputChannel = "input", outputChannel = "output")
public Message<String> consume(String data) {

	return MessageBuilder.withPayload(data)
			.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
}

Q: How error handling works in synchronous communication?
Ans:

In a synchronous communication, the sender blocks while the message is sent to the messaging system using the same thread. If an exception is raised, definitely it will go up reaching the application.

Q: How Spring Integration handle error/exceptions in asynchronous communication?
Ans:

In asynchronous communication, Spring Integration handles exceptions by publishing them to message channels. If an exception is raised, it will not reach the application. To handle this error channel comes in.

The exception thrown will be wrapped into a MessagingException,and becomes the payload of a new message.

Q: In case of exception, what channel is the error message sent to from spring integration?
Ans:

This message is send to:

  1. An error channel: First, it will check if the request message contains a header called "errorChannel". If found, the error message will be sent to error channel.
  2. A global error channel: If no error channel is specified in the message header, then it is sent to a global error channel. This channel is specified by default by Spring Integration.

Q: What is Global error channel?
Ans:

A global error channel called "errorChannel" is created by default Spring Integration. This channel is a publish-subscribe channel, it means that we can subscribe many endpoints to this channel. In general, by default there is already an endpoint subscribed to it, called a logging handler.
Logging handler will log the payload of messages coming to the channel, however it can be configured to respond differently.








Recommendation for Top Popular Post :