Java Reference
In-Depth Information
Next is a
jms:message-driven-channel-adapter
configuration element that instructs
Spring Integration to send messages coming from the message queue destination
solution011
to Spring
Integration
inboundHelloJMSMessageChannel
. An
adapter
is a component that knows how to speak to a
specific type of subsystem and translate messages on that subsystem into something that can be used in
the Spring Integration bus. Adapters also do the same in reverse, taking messages on the Spring
Integration bus and translating them into something a specific subsystem will understand. This is
different from a
service-activator
(covered next) in that it's meant to be a general connection between
the bus and the foreign endpoint. A
service-activator
, however, only helps you invoke your
application's business logic on receipt of a message. What you do in the business logic, connecting to
another system or not, is up to you.
The next component, a
service-activator
, listens for messages coming into that channel and
invokes the bean referenced by the
ref
attribute, which in this case is the bean defined previously:
inboundHelloWorldJMSPingServiceActivator
.
As you can see, there's quite a bit of configuration, but the only custom Java code needed was the
inboundHelloWorldJMSPingServiceActivator
, which is the part of the solution that Spring can't infer
by itself.
package com.apress.springenterpriserecipes.springintegration;
import org.apache.log4j.Logger;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.Message;
import java.util.Map;
public class InboundHelloWorldJMSMessageProcessor {
private static final Logger logger =
Logger.getLogger(
➥
InboundHelloWorldJMSMessageProcessor.class);
@ServiceActivator
public void handleIncomingJmsMessage(
Message<Map<String, Object>> inboundJmsMessage
) throws Throwable {
Map<String, Object> msg = inboundJmsMessage.getPayload();
logger.debug(String.format(
"firstName: %s, lastName: %s, id:%s",
msg.get("firstName"), msg.get("lastName"),
➥
msg.get("id")));
// you can imagine what we could do here: put
// the record into the database, call a websrvice,
// write it to a file, etc, etc
}
}
Notice that there is an annotation,
@ServiceActivator
, that tells Spring to configure this
component, and this method as the recipient of the message payload from the channel, which
is passed to the method as
Message<Map<String, Object>> inboundJmsMessage
. In the previous
configuration,
extract-payload="true"
, which tells Spring Integration to take the payload of the
message from the JMS queue (in this case, a
Map<String,Object>
) and extract it and pass
that
as
the payload of the message that's being moved through Spring Integration's channels as a
org.springframework.integration.core.Message
. The Spring Integration
Message
is not to be confused
Search WWH ::
Custom Search