Search in sources :

Example 1 with MetricsContext

use of org.springframework.integration.support.management.MetricsContext in project spring-integration by spring-projects.

the class AbstractMessageHandler method handleMessage.

@Override
public void handleMessage(Message<?> message) {
    Assert.notNull(message, "Message must not be null");
    // NOSONAR - false positive
    Assert.notNull(message.getPayload(), "Message payload must not be null");
    if (this.loggingEnabled && this.logger.isDebugEnabled()) {
        this.logger.debug(this + " received message: " + message);
    }
    MetricsContext start = null;
    boolean countsEnabled = this.countsEnabled;
    AbstractMessageHandlerMetrics handlerMetrics = this.handlerMetrics;
    SampleFacade sample = null;
    if (countsEnabled && this.metricsCaptor != null) {
        sample = this.metricsCaptor.start();
    }
    try {
        if (this.shouldTrack) {
            message = MessageHistory.write(message, this, getMessageBuilderFactory());
        }
        if (countsEnabled) {
            start = handlerMetrics.beforeHandle();
            handleMessageInternal(message);
            if (sample != null) {
                sample.stop(sendTimer());
            }
            handlerMetrics.afterHandle(start, true);
        } else {
            handleMessageInternal(message);
        }
    } catch (Exception e) {
        if (sample != null) {
            sample.stop(buildSendTimer(false, e.getClass().getSimpleName()));
        }
        if (countsEnabled) {
            handlerMetrics.afterHandle(start, false);
        }
        throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, () -> "error occurred in message handler [" + this + "]", e);
    }
}
Also used : SampleFacade(org.springframework.integration.support.management.metrics.SampleFacade) MetricsContext(org.springframework.integration.support.management.MetricsContext) AbstractMessageHandlerMetrics(org.springframework.integration.support.management.AbstractMessageHandlerMetrics) MessagingException(org.springframework.messaging.MessagingException)

Example 2 with MetricsContext

use of org.springframework.integration.support.management.MetricsContext in project spring-integration by spring-projects.

the class AbstractMessageChannel method send.

/**
 * Send a message on this channel. If the channel is at capacity, this
 * method will block until either the timeout occurs or the sending thread
 * is interrupted. If the specified timeout is 0, the method will return
 * immediately. If less than zero, it will block indefinitely (see
 * {@link #send(Message)}).
 * @param message the Message to send
 * @param timeout the timeout in milliseconds
 * @return <code>true</code> if the message is sent successfully,
 * <code>false</code> if the message cannot be sent within the allotted
 * time or the sending thread is interrupted.
 */
@Override
public boolean send(Message<?> message, long timeout) {
    Assert.notNull(message, "message must not be null");
    Assert.notNull(message.getPayload(), "message payload must not be null");
    if (this.shouldTrack) {
        message = MessageHistory.write(message, this, this.getMessageBuilderFactory());
    }
    Deque<ChannelInterceptor> interceptorStack = null;
    boolean sent = false;
    boolean metricsProcessed = false;
    MetricsContext metrics = null;
    boolean countsEnabled = this.countsEnabled;
    ChannelInterceptorList interceptors = this.interceptors;
    AbstractMessageChannelMetrics channelMetrics = this.channelMetrics;
    SampleFacade sample = null;
    try {
        if (this.datatypes.length > 0) {
            message = this.convertPayloadIfNecessary(message);
        }
        boolean debugEnabled = this.loggingEnabled && logger.isDebugEnabled();
        if (debugEnabled) {
            logger.debug("preSend on channel '" + this + "', message: " + message);
        }
        if (interceptors.getSize() > 0) {
            interceptorStack = new ArrayDeque<>();
            message = interceptors.preSend(message, this, interceptorStack);
            if (message == null) {
                return false;
            }
        }
        if (countsEnabled) {
            metrics = channelMetrics.beforeSend();
            if (this.metricsCaptor != null) {
                sample = this.metricsCaptor.start();
            }
            sent = doSend(message, timeout);
            if (sample != null) {
                sample.stop(sendTimer(sent));
            }
            channelMetrics.afterSend(metrics, sent);
            metricsProcessed = true;
        } else {
            sent = doSend(message, timeout);
        }
        if (debugEnabled) {
            logger.debug("postSend (sent=" + sent + ") on channel '" + this + "', message: " + message);
        }
        if (interceptorStack != null) {
            interceptors.postSend(message, this, sent);
            interceptors.afterSendCompletion(message, this, sent, null, interceptorStack);
        }
        return sent;
    } catch (Exception e) {
        if (countsEnabled && !metricsProcessed) {
            if (sample != null) {
                sample.stop(buildSendTimer(false, e.getClass().getSimpleName()));
            }
            channelMetrics.afterSend(metrics, false);
        }
        if (interceptorStack != null) {
            interceptors.afterSendCompletion(message, this, sent, e, interceptorStack);
        }
        throw IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, () -> "failed to send Message to channel '" + this.getComponentName() + "'", e);
    }
}
Also used : SampleFacade(org.springframework.integration.support.management.metrics.SampleFacade) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) MetricsContext(org.springframework.integration.support.management.MetricsContext) AbstractMessageChannelMetrics(org.springframework.integration.support.management.AbstractMessageChannelMetrics) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Aggregations

MetricsContext (org.springframework.integration.support.management.MetricsContext)2 SampleFacade (org.springframework.integration.support.management.metrics.SampleFacade)2 AbstractMessageChannelMetrics (org.springframework.integration.support.management.AbstractMessageChannelMetrics)1 AbstractMessageHandlerMetrics (org.springframework.integration.support.management.AbstractMessageHandlerMetrics)1 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)1 MessagingException (org.springframework.messaging.MessagingException)1 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)1