LibraryToggle FramesPrintFeedback

How the handleMessage() method completes its message processing has a direct impact on how message processing proceeds. It can complete by doing one of the following actions:

  1. Return true—Returning true signals to the Fuse Services Framework runtime that message processing should continue normally. The next handler, if any, has its handleMessage() invoked.

  2. Return false—Returning false signals to the Fuse Services Framework runtime that normal message processing must stop. How the runtime proceeds depends on the message exchange pattern in use for the current message.

    For request-response message exchanges the following happens:

    For one-way message exchanges the following happens:

  3. Throw a ProtocolException exception—Throwing a ProtocolException exception, or a subclass of this exception, signals the Fuse Services Framework runtime that fault message processing is beginning. How the runtime proceeds depends on the message exchange pattern in use for the current message.

    For request-response message exchanges the following happens:

    For one-way message exchanges the following happens:

  4. Throw any other runtime exception—Throwing a runtime exception other than a ProtocolException exception signals the Fuse Services Framework runtime that message processing is to stop. All previously invoked message handlers have the close() method invoked and the exception is dispatched. If the message is part of a request-response message exchange, the exception is dispatched so that it is returned to the consumer that originated the request.

Example 21.7 shows an implementation of handleMessage() message for a logical message handler that is used by a service consumer. It processes requests before they are sent to the service provider.

Example 21.7. Logical Message Handler Message Processing

public class SmallNumberHandler implements LogicalHandler<LogicalMessageContext>
{
    public final boolean handleMessage(LogicalMessageContext messageContext)
    {
        try
        {
            boolean outbound = (Boolean)messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

            if (outbound) 1
            {
                LogicalMessage msg = messageContext.getMessage(); 2

                JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
                Object payload = msg.getPayload(jaxbContext); 3
                if (payload instanceof JAXBElement)
                {
                    payload = ((JAXBElement)payload).getValue();
                }

                if (payload instanceof AddNumbers) 4
                {
                    AddNumbers req = (AddNumbers)payload;

                    int a = req.getArg0();
                    int b = req.getArg1();
                    int answer = a + b;

                    if (answer < 20) 5
                    {
                      AddNumbersResponse resp = new AddNumbersResponse(); 6
                      resp.setReturn(answer);
                      msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp),
                                                      jaxbContext);

                      return false; 7
                    }
                }
                else
                {
                   throw new WebServiceException("Bad Request"); 8
                }
            }
            return true; 9
        }
        catch (JAXBException ex) 10
        {
            throw new ProtocolException(ex);
        }
    }
...
}

The code in Example 21.7 does the following:

1

Checks if the message is an outbound request.

If the message is an outbound request, the handler does additional message processing.

2

Gets the LogicalMessage representation of the message payload from the message context.

3

Gets the actual message payload as a JAXB object.

4

Checks to make sure the request is of the correct type.

If it is, the handler continues processing the message.

5

Checks the value of the sum.

If it is less than the threshold of 20 then it builds a response and returns it to the client.

6

Builds the response.

7

Returns false to stop message processing and return the response to the client.

8

Throws a runtime exception if the message is not of the correct type.

This exception is returned to the client.

9

Returns true if the message is an inbound response or the sum does not meet the threshold.

Message processing continues normally.

10

Throws a ProtocolException if a JAXB marshalling error is encountered.

The exception is passed back to the client after it is processed by the handleFault() method of the handlers between the current handler and the client.

Comments powered by Disqus
loading table of contents...